题目大意:给定n颗珠子,用k种颜色对其进行染色,问染成项链(旋转相同,反转不同)和珠子(旋转相同,翻转相同)分别有多少种不同方法
旋转1..n颗数字,有n种置换。旋转i颗,有gcd(n,i)个循环, 该置换不动点数目为t^gcd(n,i)
n为奇数时,有n条对称轴,即n种置换,每种置换分为循环节为(n - 1) / 2的循环 和 循环节为1的循环
每种置换不动点的数目为t^((n + 1)/2)
n为偶数时,有n/2条对称轴不穿珠子分为(n/2个)循环节为2的置换,有n/2条对称轴穿过珠子分为((n-2)/2个)循环节为2的循环和(2个)循环为1的循环
每种置换不动点数目为t^(n/2) + t^(n/2 + 1)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <cmath> 9 #define min(a, b) ((a) < (b) ? (a) : (b)) 10 #define max(a, b) ((a) > (b) ? (a) : (b)) 11 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 12 inline void swap(long long &a, long long &b) 13 { 14 long long tmp = a;a = b;b = tmp; 15 } 16 inline void read(long long &x) 17 { 18 x = 0;char ch = getchar(), c = ch; 19 while(ch < ‘0‘ || ch > ‘9‘) c = ch, ch = getchar(); 20 while(ch <= ‘9‘ && ch >= ‘0‘) x = x * 10 + ch - ‘0‘, ch = getchar(); 21 if(c == ‘-‘) x = -x; 22 } 23 24 const long long INF = 0x3f3f3f3f; 25 26 long long n,t; 27 28 long long pow(long long a, long long b) 29 { 30 long long r = 1, base = a; 31 for(;b;b >>= 1) 32 { 33 if(b & 1) r *= base; 34 base *= base; 35 } 36 return r; 37 } 38 39 long long gcd(long long a, long long b) 40 { 41 return !b ? a : gcd(b, a % b); 42 } 43 44 int main() 45 { 46 while(scanf("%lld %lld", &n, &t) != EOF) 47 { 48 long long a = 0,b = 0; 49 for(register long long i = 1;i <= n;++ i) a += pow(t, gcd(i, n)); 50 if(n&1) b = n * pow(t, (n + 1) >> 1); 51 else b = n/2 * (pow(t, n/2) + pow(t, n/2 + 1)); 52 printf("%lld %lld\n", a/n, (a + b) / (n << 1)); 53 } 54 return 0; 55 }