[Burnside引理] HDU5868. Different Circle Permutation

题解:http://blog.csdn.net/kg20006/article/details/52550561

#include 
#include 
#include 

using namespace std;

const int P=1e9+7;

int t,n;

struct mat{
  int a[3][3];
  friend mat operator *(mat a,mat b){
    mat ret; ret.a[1][1]=ret.a[1][2]=ret.a[2][1]=ret.a[2][2]=0;
    ret.a[1][1]=(1LL*a.a[1][1]*b.a[1][1]+1LL*a.a[1][2]*b.a[2][1])%P;
    ret.a[1][2]=(1LL*a.a[1][1]*b.a[1][2]+1LL*a.a[1][2]*b.a[2][2])%P;
    ret.a[2][1]=(1LL*a.a[2][1]*b.a[1][1]+1LL*a.a[2][2]*b.a[2][1])%P;
    ret.a[2][2]=(1LL*a.a[2][1]*b.a[1][2]+1LL*a.a[2][2]*b.a[2][2])%P;
    return ret;
  }
};

inline int f(int n){
  mat a,b;
  if(n==1) return 1;
  n-=2;
  a.a[1][1]=a.a[1][2]=a.a[2][1]=1; a.a[2][2]=0;
  b.a[1][1]=3; b.a[1][2]=b.a[2][1]=1; b.a[2][2]=0;
  for(;n;n>>=1,a=a*a) if(n&1) b=a*b;
  return b.a[1][1];
}

inline int Pow(int x,int y){
  int ret=1;
  for(;y;y>>=1,x=1LL*x*x%P) if(y&1) ret=1LL*ret*x%P;
  return ret;
}

inline int phi(int x){
  int ret=x;
  for(int i=2;i*i<=x;i++){
    if(x%i) continue;
    ret-=ret/i;
    while(x%i==0) x/=i;
  }
  if(x^1) ret-=ret/x;
  return ret;
}

int main(){
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
  while(~scanf("%d",&n)){
    int ans=0;
    if(n==1){
      puts("2"); continue;
    }
    for(int i=1;i*i<=n;i++)
      if(n%i==0){
    ans=(ans+1LL*f(i)*phi(n/i))%P;
    if(n/i!=i) ans=(ans+1LL*f(n/i)*phi(i))%P;
      }
    cout<<1LL*ans*Pow(n,P-2)%P<<endl;
  }
  return 0;
}

你可能感兴趣的:(Burnside引理)