对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n)。
那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n)。考虑枚举gcd。显然gcd(i,n)=x在该范围内解的个数是φ(n/x)。分解一下质因数即可。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define P 1000000007 #define N 100 char getc(){char c=getchar();while ((c<‘A‘||c>‘Z‘)&&(c<‘a‘||c>‘z‘)&&(c<‘0‘||c>‘9‘)) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<‘0‘||c>‘9‘) {if (c==‘-‘) f=-1;c=getchar();} while (c>=‘0‘&&c<=‘9‘) x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int m,T,prime[N],cnt[N],p[N][N],t,ans; int ksm(int a,int k) { int s=1; for (;k;k>>=1,a=1ll*a*a%P) if (k&1) s=1ll*s*a%P; return s; } void dfs(int k,int s,int phi) { if (k>t) {ans=(ans+1ll*ksm(m,s-1)*phi)%P;return;} for (int i=0;i<cnt[k];i++) dfs(k+1,1ll*s*p[k][i]%P,1ll*phi*(prime[k]-1)%P*p[k][cnt[k]-i-1]%P); dfs(k+1,1ll*s*p[k][cnt[k]]%P,phi); } int main() { #ifndef ONLINE_JUDGE freopen("a.in","r",stdin); freopen("a.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif T=read(); while (T--) { int n=read();m=n,t=0; for (int i=2;i*i<=n;i++) if (n%i==0) { prime[++t]=i,cnt[t]=1;n/=i; while (n%i==0) cnt[t]++,n/=i; } if (n>1) prime[++t]=n,cnt[t]=1; for (int i=1;i<=t;i++) { p[i][0]=1; for (int j=1;j<=cnt[i];j++) p[i][j]=1ll*p[i][j-1]*prime[i]%P; } ans=0;dfs(1,1,1); printf("%d\n",ans); } return 0; }
原文地址:https://www.cnblogs.com/Gloid/p/10164378.html
时间: 2024-11-08 17:03:08