uva 11255 Necklace

本文介绍了一个关于项链图案组合的问题,三个女孩分别拥有不同颜色的珍珠,目标是计算使用特定数量的每种颜色珍珠可以形成的独特项链数量。文章通过具体的输入输出示例说明了问题,并提供了一段基于组合数学原理的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【题目】

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
ipping over a necklace cannot produce a different kind of necklace. i.e. The
following gure shows three equivalent necklaces.
The following gure shows all possible necklaces formed by 3 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 a, b, c, 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
2
3 2 1
2 2 2
Sample Output
6
11


【分析】

运用组合数学的知识,根据a+b+c的奇偶性大力讨论。


【代码】

//UVA 11255 Necklace
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 40
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=45;
int n,a,b,c;
int C[mxn][mxn];
inline int gcd(int x,int y) {return x%y==0?y:gcd(y,x%y);} 
int main()
{
    int i,j,T;
    fo(i,0,N) C[i][0]=1;
    fo(i,1,N)
      fo(j,1,i)
        C[i][j]=C[i-1][j-1]+C[i-1][j];
    scanf("%d",&T);
    while(T--)
    {
        ll ans=0;
        scanf("%d%d%d",&a,&b,&c);
        n=a+b+c;
        fo(i,1,n)
        {
            int x=n/gcd(n,i);  //循环节大小
            if(a%x==0 && b%x==0 && c%x==0)
              ans+=C[n/x][a/x]*C[n/x-a/x][b/x]; 
        }
        if((n&1) && (a&1)+(b&1)+(c&1)==1)
          ans+=C[n/2][a/2]*C[n/2-a/2][b/2];
        if(!(n&1))
        { 
            if((a&1)+(b&1)+(c&1)==0)
              ans+=C[n/2][a/2]*C[n/2-a/2][b/2]*n;
            else if((a&1)+(b&1)+(c&1)==2)
              ans+=C[n/2-1][a/2]*C[n/2-1-a/2][b/2]*n;
        }
        printf("%lld\n",ans/n/2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值