1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| #include <set> #include <map> #include <queue> #include <stack> #include <deque> #include <cmath> #include <bitset> #include <cstdio> #include <vector> #include <string> #include <complex> #include <utility> #include <cstring> #include <iostream> #include <assert.h> #include <algorithm>
using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define com complex<db> #define mp(x,y) make_pair((x),(y)) #define fi first #define se second #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&(-(x))) #define bit(x,i) (((x)>>(i))&1) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) inline ll read() { ll x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); return f?-x:x; } #define CASET fo(___,1,read()) const ll mod=1e9+7; inline ll Add(ll x,ll y){x+=y; return (x<mod)?x:x-mod;} inline ll Dec(ll x,ll y){x-=y%mod; return (x<0)?x+mod:x;} inline ll Mul(ll x,ll y){return x*y%mod;} inline ll Pow(ll x,ll y) { y%=(mod-1); ll ans=1; for(;y;y>>=1,x=x*x%mod) if(y&1) ans=ans*x%mod; return ans; } const ll inv4=Pow(4,mod-2); const ll inv6=Pow(6,mod-2); const int N=4100000; const int M=500010; const int K=111; ll pri[N>>3],phi[N],sg[N]; int tot; bool vis[N]; void init_prime(int n) { phi[1]=1; ll t; fo(i,2,n) { if(!vis[i]) {pri[++tot]=i; phi[i]=i-1;} for(int j=1;j<=tot&&(t=(ll)pri[j]*i)<=n;j++) { vis[t]=1; if(i%pri[j]==0) {phi[t]=phi[i]*pri[j]; break;} phi[t]=phi[i]*(pri[j]-1); } } fo(i,1,n) sg[i]=Add(sg[i-1],1ll*i*i%mod*phi[i]%mod); } ll fac[K],inv[K]; void init_fac(int n) { fac[0]=1; fo(i,1,n) fac[i]=fac[i-1]*i%mod; inv[n]=Pow(fac[n],mod-2); fd(i,n,1) inv[i-1]=inv[i]*i%mod; } ll n,ans,Sqr; int k; map<ll,ll> ma; inline ll S3(ll n) { n%=mod; n=n*(n+1)/2%mod; return n*n%mod; } inline ll S2(ll n) { n%=mod; return n*(n+1)%mod*(n*2+1)%mod*inv6%mod; } inline ll S(ll n) { if(n<N) return sg[n]; if(ma[n]!=0) return ma[n]; ll ans=S3(n); for(ll i=2,j;i<=n;i=j+1) { j=n/(n/i); ans=Dec(ans,S(n/i)%mod*(S2(j)-S2(i-1)+mod)%mod); } return ma[n]=ans; } ll pre[K],suf[K],y[K]; inline ll Lagrange(ll n,int k,ll sum=0) { n%=mod; pre[0]=1; suf[k+1]=1; fo(i,1,k) pre[i]=Mul(pre[i-1],(n-i+mod)%mod); fd(i,k,1) suf[i]=Mul(suf[i+1],(n-i+mod)%mod); fo(i,1,k) sum=Add(sum,Mul(pre[i-1],suf[i+1])*Mul(Mul(y[i],inv[i-1]),Mul(inv[k-i],((k-i)&1)?mod-1:1))%mod); return sum; } int m; int id1[M],id2[M]; ll g[M],w[M]; void Min25() { m=0; fo(i,1,k+2) y[i]=Add(y[i-1],Pow(i,k)); for(ll i=1,j;i<=n;i=j+1) { j=n/(n/i); w[++m]=n/i; if(w[m]<=Sqr) id1[w[m]]=m; else id2[n/w[m]]=m; g[m]=Lagrange(w[m],k+2)-1; } ll qm=0; g[m+1]=0; fo(i,1,tot) { ll tmp=pri[i]*pri[i],p=Pow(pri[i],k),t; for(int j=1;j<=m&&tmp<=w[j];j++) { t=w[j]/pri[i]; t=(t<=Sqr)?id1[t]:id2[n/t]; g[j]=Dec(g[j],p*(g[t]-qm+mod)%mod); } qm=Add(qm,p); } } int main() { init_prime(N-1); init_fac(K-5); CASET { n=read(); k=read()+1; Sqr=sqrt(n); ans=0; ma.clear(); Min25(); for(ll i=1,j,l=m,h;i<=n;i=j+1,l--) { j=n/(n/i); h=(g[l]-g[l+1]+mod)%mod; ans=Add(ans,Mul(S(n/i),h)); } printf("%lld\n",ans%mod); } return 0; }
|