POJ 1286 Pólya定理
Necklace of Beads
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9162 | Accepted: 3786 |
Description
Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 24 ). If the repetitions that are produced by rotation around the center of the circular necklace or reflection to the axis of symmetry are all neglected, how many different forms of the necklace are there?
![](http://poj.org/images/1286_1.jpg)
![](http://poj.org/images/1286_1.jpg)
Input
The input has several lines, and each line contains the input data n.
-1 denotes the end of the input file.
-1 denotes the end of the input file.
Output
The output should contain the output data: Number of different forms, in each line correspondent to the input data.
Sample Input
4 5 -1
Sample Output
21 39
题意:n个珠子串成一个圆,用三种颜色去涂色,问一共有多少种不同的涂色方法(不同的涂色方法被定义为:如果这种涂色情况翻转,旋转不与其他情况相同就为不同。)
思路:这道题其实就是一个最简单的板子题。要想明白Polya定理首先要知道置换,置换群和轮换的概念,可以参考这里(用例子很好理解)。
项链可以进行旋转和翻转。
翻转:如果n是奇数,则存在n中置换,每种置换包含n/2+1种循环(即轮换)。
如果n是偶数,如果对称轴过顶点,则存在n/2种置换,每种置换包含n/2种循环(即轮换)
如果对称轴不过顶点,则存在n/2种置换,每种置换包含n/2+1种循环(即轮换)
旋转:n个点顺时针或者逆时针旋转i个位置的置换,轮换个数为gcd(n,i)
代码:
1 #include"bits/stdc++.h" 2 #define db double 3 #define ll long long 4 #define vec vector<ll> 5 #define mt vector<vec> 6 #define ci(x) scanf("%d",&x) 7 #define cd(x) scanf("%lf",&x) 8 #define cl(x) scanf("%lld",&x) 9 #define pi(x) printf("%d\n",x) 10 #define pd(x) printf("%f\n",x) 11 #define pl(x) printf("%lld\n",x) 12 //#define rep(i, x, y) for(int i=x;i<=y;i++) 13 #define rep(i, n) for(int i=0;i<n;i++) 14 const int N = 1e3+ 5; 15 const int mod = 1e9 + 7; 16 //const int MOD = mod - 1; 17 const int inf = 0x3f3f3f3f; 18 const db PI = acos(-1.0); 19 const db eps = 1e-10; 20 using namespace std; 21 ll gcd(ll x,ll y){ 22 return y==0?x:gcd(y,x%y); 23 } 24 ll qpow(ll x,ll n) 25 { 26 ll ans=1; 27 x%=mod; 28 while(n){ 29 if(n&1) ans=ans*x; 30 x=x*x; 31 n>>=1; 32 } 33 return ans; 34 } 35 36 int main() 37 { 38 ll n; 39 while(cin>>n&&n!=-1) 40 { 41 if(!n) puts("0"); 42 else 43 { 44 ll ans=0; 45 for(ll i=1;i<=n;i++) ans+=qpow(3ll,gcd(n,i)); 46 if(n&1) ans+=qpow(3ll,n/2+1)*n; 47 else ans+=qpow(3ll,n/2+1)*(n/2)+qpow(3ll,n/2)*(n/2); 48 pl(ans/2/n); 49 } 50 } 51 return 0; 52 }