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
| #include<bits/stdc++.h> using namespace std; typedef long long ll;
const ll mod=1e9+7; ll qpow(ll a,ll b){ assert(a<mod); ll res=1; while(b){ if(b&1) res=res*a%mod; a=a*a%mod; b>>=1; }return res; } const ll inv2=qpow(2,mod-2),inv3=qpow(3,mod-2); inline ll reduce(ll x){return x<0?x+mod:x;} inline ll A(ll a,ll b){assert(a<mod&&b<mod); return reduce(a+b-mod);} inline ll M(ll a,ll b){assert(a<2*mod&&b<2*mod); return a*b%mod;} inline ll M(ll a,ll b,ll c){return M(M(a,b),c);}
const ll maxn=2.5e7; bitset<maxn>vis; int siiphi[maxn]; ll p[1565927+100]; void f_ini(){ siiphi[1]=1; for (ll i=2;i<maxn;i++){ if(!vis[i]) p[++p[0]]=i,siiphi[i]=i-1; for (ll j=1;i*p[j]<maxn;j++){ vis[i*p[j]]=true; if(i%p[j])siiphi[i*p[j]]=siiphi[i]*(p[j]-1); else{siiphi[i*p[j]]=siiphi[i]*p[j];break;} } } for(ll i=1;i<maxn;i++) siiphi[i]=A(siiphi[i-1],M(i,i,siiphi[i])); }
const ll sqr=3e5; ll id1[sqr],id2[sqr],w[sqr],idn,idm; inline ll& id(ll x){return x<sqr?id1[x]:id2[idn/x];} void ini(ll n){ idn=n;idm=0; for(ll l=1,r;l<=n;l=r+1){ r=n/(n/l); id(n/l)=++idm; w[idm]=n/l; } }
namespace min25shai{ ll g[sqr],sp[sqr]; ll getsum(ll x,ll n){ static ll prepre[1000],suf[1000],r[1000]={1,1},y[1000],*pre=prepre+1; if(y[999]!=++n) { y[999]=n; for(ll i=1;i<=n;i++) y[i]=A(y[i-1],qpow(i,n-1)); for(ll i=2;i<=n;i++) r[i]=M(mod-mod/i,r[mod%i]); for(ll i=2;i<=n;i++) r[i]=M(r[i],r[i-1]); } pre[-1]=suf[n+1]=1; for(ll i=0;i<=n;++i) pre[i]=M(pre[i-1],x%mod-i+mod); for(ll i=n;i>=0;i--) suf[i]=M(suf[i+1],i-x%mod+mod); ll b=0; for(ll i=0;i<=n;++i) { ll up=M(pre[i-1],suf[i+1]); ll down=M(r[i],r[n-i]); b=A(b,M(y[i],up,down)); } return b; } void min25(ll*g,ll n,ll k,ll(*f)(ll,ll),ll(*s)(ll,ll)){ for(ll i=1;i<=idm;++i) g[i]=A(s(w[i],k),mod-1); for(ll j=1;p[j]*p[j]<=n;j++){ ll t=f(p[j],k); sp[j]=A(sp[j-1],t); for(ll i=1;w[i]>=p[j]*p[j];++i) g[i]=A(g[i],M(sp[j-1]-g[id(w[i]/p[j])]+mod,t)); } } }
namespace dujiaoshai{ ll s[sqr]; inline ll s1(ll n){return M(n,n+1,inv2);} inline ll s2(ll n){return M(s1(n),2*n+1,inv3);} inline ll s3(ll n){return M(s1(n),s1(n));} void ini(){for(ll i=1;i<=idm;i++)s[i]=0;} ll dujiao(ll n){ if(n<maxn) return siiphi[n]; if(s[id(n)]!=0) return s[id(n)]; s[id(n)]=s3(n%mod); for(ll l=2,r;l<=n;l=r+1){ r=n/(n/l); s[id(n)]-=(s2(r%mod)-s2((l-1)%mod))*dujiao(n/l)%mod; } return s[id(n)]=(s[id(n)]%mod+mod)%mod; } }
ll solve(ll n,ll k){ ini(n); dujiaoshai::ini(); #define F(M) [](ll n,ll k){return ll(M);} min25shai::min25(min25shai::g,n,k+1,F(qpow(n%mod,k)),F(min25shai::getsum(n,k))); #undef F ll res=0; for(ll l=1,r;l<=n;l=r+1){ r=n/(n/l); ll t1=dujiaoshai::dujiao(n/l); ll t2=min25shai::g[id(r)]; if(l!=1) t2+=mod-min25shai::g[id(l-1)]; res+=M(t1,t2); } return res%mod; }
inline ll read(){ll x;cin>>x;return x;} int main() { f_ini(); for(ll t=read();t>=1;t--){ ll n=read(),k=read(); cout<<solve(n,k)<<endl; } }
|