Advertisement
yicongli

LG4980

Mar 30th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define gc c=getchar()
  6. #define r(x) read(x)
  7. #define ll long long
  8.  
  9. template<typename T>
  10. inline void read(T&x){
  11.     T k=1;char gc;x=0;
  12.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  13.     while(isdigit(c))x=x*10+c-'0',gc;x*=k;
  14. }
  15.  
  16. const int p=1e9+7;
  17. const int N=1e6+7;
  18.  
  19. inline int add(int a,int b){
  20.     return (a+=b)>=p?a-p:a;
  21. }
  22.  
  23. inline ll qpow(ll a,ll b){
  24.      ll ans=1;
  25.      while(b){
  26.         if(b&1)(ans*=a)%=p;
  27.         (a*=a)%=p;
  28.         b>>=1;
  29.      }
  30.      return ans;
  31. }
  32.  
  33. int tot;
  34. int pri[N];
  35. int phi[N];
  36. bool mark[N];
  37.  
  38. inline void pre(){
  39.     phi[1]=1;
  40.     for(int i=2;i<N;++i){
  41.         if(!mark[i])pri[++tot]=i,phi[i]=i-1;
  42.         for(int j=1,tmp;j<=tot&&(tmp=i*pri[j])<N;++j){
  43.             mark[tmp]=1;
  44.             if(i%pri[j])phi[tmp]=phi[i]*phi[pri[j]];
  45.             else {
  46.                 phi[tmp]=phi[i]*pri[j];
  47.                 break;
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53. map<int,int>mp;
  54.  
  55. inline int PHI(int x){
  56.     if(x<N)return phi[x];
  57.     if(mp.count(x))return mp[x];
  58.     int &ret=mp[x];
  59.     ret=x;
  60.     for(ll i=2;i*i<=x;++i){
  61.         if(x%i==0){
  62.             ret=ret/i*(i-1);
  63.             while(x%i==0)x/=i;
  64.         }
  65.     }
  66.     if(x>1)ret=ret/x*(x-1);
  67.     return ret;
  68. }
  69.  
  70. int main(){
  71.     // freopen(".in","r",stdin);
  72.     // freopen(".out","w",stdout);
  73.     int T;r(T);
  74.     pre();
  75.     while(T--){
  76.         int n;r(n);
  77.         int ans=0;
  78.         for(ll i=1;i*i<=n;++i){
  79.             if(n%i)continue;
  80.             int j=n/i;
  81.             ans=add(ans,qpow(n,i-1)*PHI(j)%p);
  82.             if(i==j)continue;
  83.             ans=add(ans,qpow(n,j-1)*PHI(i)%p);
  84.         }
  85.         printf("%d\n",ans);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement