首页 > 代码库 > hdu 1817 Necklace of Beads(Polya定理)

hdu 1817 Necklace of Beads(Polya定理)

题目链接:hdu 1817 Necklace of Beads

这题的弱化版:传送门

技术分享
 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=a;i<=b;++i)
 3 using namespace std;
 4 typedef long long ll;
 5 
 6 int t,n,m,cas;
 7 
 8 ll mypow(ll a,ll k){
 9     ll an=1;
10     while(k){
11         if(k&1)an=an*a;
12         k>>=1,a=a*a;
13     }
14     return an;
15 }
16 
17 int main(){
18     n=3;
19     while(cin>>m)
20     {
21         if(m==0){puts("0");continue;}
22         if(m==-1)break;
23         ll ans=0;
24         F(i,1,m)ans+=mypow(n,__gcd(i,m));
25         if((m&1))ans=(ans+m*mypow(n,m/2+1));
26         else ans=(ans+m/2*mypow(n,m/2+1)+m/2*mypow(n,m/2));
27         ans=ans/m/2;
28         cout<<ans<<endl;
29     }
30     return 0;
31 }
View Code

 

hdu 1817 Necklace of Beads(Polya定理)