题目链接:
UVA 11255 Necklace
题意:
各有 a,b,c(a,b,c≥0,a+b+c≤40) 颗三种颜色,问这些珠子能串成的项链有多少种?考虑翻转和旋转。
分析:
令 ∑3i=1color[i]=n ,即珠子总数。
- 考虑旋转置换。
我们考虑旋转 i 颗珠子的间距,则
- 考虑翻转置换
当 n 为奇数时,则对称轴上必有一点,对称轴有
当
可以发现不论
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 42;ll C[MAX_N][MAX_N];
int color[5], b[5], n;void init() //组合数打表
{C[0][0] = 1;for(int i = 1; i < MAX_N; ++i) {C[i][0] = C[i][i] = 1;for(int j = 1; j < i; ++j) {C[i][j] = C[i - 1][j] + C[i - 1][j - 1];}}
}int gcd(int x, int y)
{return y == 0 ? x : gcd(y, x % y);
}ll CircleSection(int x)
{int sum = 0;for(int i = 1; i <= 3; ++i) {if(b[i] % x) return 0;b[i] /= x;sum += b[i];}ll res = 1;for(int i = 1; i <= 3; ++i) {res *= C[sum][b[i]];sum -= b[i];}return res;
}ll Rotate() //旋转置换
{ll res = 0;for(int i = 0; i < n; ++i) {int d = gcd(i, n);memcpy(b, color, sizeof(color));res += CircleSection(n / d); //n/d是循环元素个数}return res;
}ll Flip() //翻转置换
{ll res = 0;if(n & 1) {for(int i = 1; i <= 3; ++i) {memcpy(b, color, sizeof(color));b[i]--; if(b[i] < 0) continue;res += CircleSection(2) * n; //n条对称轴}}else {//穿过珠子for(int i = 1; i <= 3; ++i) {for(int j = 1; j <= 3; ++j) {memcpy(b, color, sizeof(color));b[i]--, b[j]--;if(b[i] < 0 || b[j] < 0) continue;res += CircleSection(2) * (n / 2);}}//不穿过珠子memcpy(b, color, sizeof(color));res += CircleSection(2) * (n / 2);}return res;
}int main()
{init();int T;scanf("%d", &T);while(T--) {n = 0;for(int i = 1; i <= 3; ++i) {scanf("%d", color + i);n += color[i];}ll ans = 0;ans += Rotate();ans += Flip();printf("%lld\n", ans / (2 * n));}return 0;
}