题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3923
题意大概就是有条n长度的项链,m种不同的颜色,问可以组成多少种不同的项链(翻转与旋转后相同的都算是同一条项链)
用polya 定理
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
typedef __int64 LL;
const int N=1;
const LL mod=1000000007LL;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
LL powmod(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1) ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ans;
}
LL ext_gcd(LL a,LL b,LL &x,LL &y)
{
LL t,ret;
if(!b)
{
x=1,y=0;
return a;
}
ret=ext_gcd(b,a%b,x,y);
t=x,x=y,y=t-a/b*y;
return ret;
}
LL euler(LL n)
{
LL ans=n;
for(LL i=2;i*i<=n;i++)
if(n%i==0)
{
ans=ans/i*(i-1);
while(n%i==0)
n/=i;
}
if(n>1)
ans=ans/n*(n-1);
return ans;
}
int main()
{
int i,j,t,ca=0;
scanf("%d",&t);
while(t--)
{
LL n,c;
scanf("%I64d%I64d",&c,&n);
LL xh=0;
for(LL i=1;i<=n;i++)
if(n%i==0)
xh=(xh+powmod(c,i)*euler(n/i))%mod;
if(n&1)
xh+=n*powmod(c,n/2+1);
else
xh+=n/2*(powmod(c,n/2)*(c+1));
xh=xh%mod;
LL x,y;
ext_gcd(2*n,mod,x,y);
x=(x%mod+mod)%mod;
xh=(xh*x)%mod;
printf("Case #%d: %I64d\n",++ca,xh);
}
return 0;
}
AC代码2:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long LL;
const int N=21;
const LL II=1000000007;
LL m,n;
LL ans,k;
LL gcdm[99999];
LL gcd(LL a,LL b)
{
while(b)
{
a=a%b;
swap(a,b);
}
return a;
}
LL Extended_Euclid(LL x,LL mod)
{
LL X1=1,X2=0,X3 = mod;
LL Y1=0,Y2=1,Y3 = x;
while(true)
{
if (Y3 == 0) return 0; //无逆元
if (Y3 == 1) return Y2; //Y2为逆元
LL Q = X3 / Y3;
LL T1 = X1 - Q*Y1, T2 = X2 - Q*Y2, T3 = X3 - Q*Y3;
X1 = Y1; X2 = Y2; X3 = Y3;
Y1 = T1; Y2 = T2; Y3 = T3;
}
}
int main()
{
int i,T,ci=0;
cin>>T;
while(T--)
{
scanf("%lld%lld",&m,&n);
gcdm[0]=1;
for(i=1;i<=n;i++)
gcdm[i]=(gcdm[i-1]*m)%II;
ans=0;
for(i=1;i<=n;i++)
{
k=gcdm[gcd(n,i)];
ans=(ans+k)%II;//不知道这个地方为什么不用乘逆元
}
if(n&1)
{
ans=(ans+n*gcdm[n/2+1])%II;//关于对角线的对称
ans=(ans*Extended_Euclid(2*n,II))%II;//乘逆元
}
else
{
ans=(ans+n/2*gcdm[n/2]*(m+1)%II)%II;
ans=(ans*Extended_Euclid(2*n,II))%II;//乘逆元
//n/2*pow((double)m,n/2)+n/2*pow((double)m,n/2+1);
//关于中线的对称 关于对角线的对称
}
ans=(ans+II)%II;//这个地方一定要加上
printf("Case #%d: %d\n",++ci,ans);
}
return 0;
}