1. 程式人生 > >poj1286 Necklace of Beads—— Polya定理

poj1286 Necklace of Beads—— Polya定理

main printf polya nec i++ 感覺 ostream http clu

題目:http://poj.org/problem?id=1286

真·Polya定理模板題;

寫完以後感覺理解更深刻了呢。

代碼如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int n;
ll ans;
ll pw(ll a,int b)
{
    ll ret=1;
    for(;b;b>>=1,a*=a)
        if(b&1)ret*=a;
    return ret;
}
int gcd(int a,int b){return (a%b==0)?b:gcd(b,a%b);} ll rot(int n) { ll ret=0; for(int i=0;i<n;i++) ret+=pw(3,gcd(i,n)); return ret; } ll d(int n) { if(n%2)return (ll)n*pw(3,(n-1)/2+1); return (ll)n/2*(pw(3,n/2)+(ll)pw(3,(n-2)/2+2)); } int main() { while(1) { scanf(
"%d",&n); if(!n) { printf("0\n"); continue;//! } if(n==-1)return 0; ans=(rot(n)+d(n))/(2*n); printf("%lld\n",ans); } }

poj1286 Necklace of Beads—— Polya定理