UVA 11255 - Necklace (等价置换)

来源:互联网 发布:女士牛仔裤品牌 知乎 编辑:程序博客网 时间:2024/05/02 00:00
Problem C - Necklace


Once upon a time, three girls - Winnie, Grace and Bonnie - owned a large number of pearls. However, each of them only had a single color of pearls. Winnie had white pearls, Grace had grey pearls and Bonnie had black pearls. One day, after a long discussion, they decided to make necklaces using the pearls. They are interested in knowing how many patterns can be formed using a certain number of pearls of each color, and have asked you to solve this problem for them. 

Note that rotating or flipping over a necklace cannot produce a different kind of necklace. i.e. The following figure shows three equivalent necklaces. 

Rotating and turning over a necklace


The following figure shows all possible necklaces formed by 3 white pearls, 2 grey pearls and 1 black pearl. 

All necklaces formed by3 white pearls, 2 grey pearls and 1 black pearl


Input
The input begins with an integer N (  2500) which indicates the number of test cases followed. Each of the following test cases consists of three non-negative integers abc, where 3 a +b + c 40. 

Output
For each test case, print out the number of different necklaces that formed by a white pearls, b grey pearls and c black pearls in a single line. 

Sample input

3 2 1 
2 2 2 

Sample output

11 


Problem setter: Cho 
Special thanks: Michael (for making up the story) 

Source: Tsinghua-HKUST Programming Contest 2007 


题意:有一个项链要染上三种颜色,每种颜色的数量分别是a,b,c, 其中通过旋转和翻转得到的情况算为一种,问一共有多少种染色情况。


思路:可以直接用Burnside定理求。 训练指南上面似乎也有类似的讲解。。。所以就不多讲了。注意置换里面有(1,2,3,...n)这个置换,别漏了。


代码:

#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#include <queue>#include <set>using namespace std;#define eps 1e-8#define rep(i,a,b) for(int i = (a); i < (b); ++i)#define rrep(i,b,a) for(int i = (b); i >= (a); --i)#define clr(a,x) memset(a,(x),sizeof(a))#define mp make_pair#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long long#define ld long doubleconst int maxn = 42;ll C[maxn][maxn];int dp[maxn][maxn][maxn];int a,b,c,n;void pre_init(){    rep(i,0,maxn) {        C[i][0] = 1;        rep(j,1,i+1) C[i][j] = C[i-1][j] + C[i-1][j-1];    }}void solve(){    int fenmu = n * 2;    ll fenzi = 0;    //1 2 3 .... n    fenzi += C[n][a] * C[n-a][b];    //旋转    rep(d,1,n) {        int len = n / __gcd(d,n);        int num = n / len;        if (a % len || b % len || c % len) continue;        fenzi += C[num][a / len] * C[num - a / len][b / len];    }    //翻转    if (n % 2) {        int num = (n - 1) >> 1;        int cnt = a % 2 + b % 2 + c % 2;        if (cnt == 1) {            fenzi += C[num][a/2] * C[num-a/2][b/2] * n;        }    } else {        int num = n >> 1;        int cnt = a % 2 + b % 2 + c % 2;        if (cnt == 0) fenzi += C[num][a/2] * C[num-a/2][b/2] * (n / 2);        --num;        if (cnt == 2) fenzi += 2 * C[num][a / 2] * C[num - a/2][b/2] * (n / 2);        else if (cnt == 0) {            if (a >= 2) fenzi += C[num][(a - 2)/2] * C[num - (a - 2) / 2][b / 2] * (n / 2);            if (b >= 2) fenzi += C[num][(b - 2) / 2] * C[num - (b-2) / 2][a / 2] * (n / 2);            if (c >= 2) fenzi += C[num][(c - 2) / 2] * C[num - (c - 2) / 2][a / 2] * (n / 2);        }    }    printf("%lld\n",fenzi / fenmu);}int main(){    #ifdef ACM        freopen("in.txt","r",stdin);//        freopen("out.txt","w",stdout);    #endif // ACM    pre_init();    int T; cin >> T;    while (T--) {        scanf("%d%d%d",&a,&b,&c);        n = a + b + c;        solve();    }}


0 0
原创粉丝点击