zoukankan      html  css  js  c++  java
  • ZOJ 1202 Divide and Count

    Divide and Count
    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=202

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

      Jack has several beautiful diamonds, each of them is unique thus precious. To keep them safe, he wants to divide and store them in different locations. First, he has bought some coffers. These boxes are identical except that their capacities (the number of the diamonds that a box can contain) may diverse. Before he puts his diamonds in, he wants to figure out how many different ways he can store these diamonds into the coffers. He recruits you, a young programmer from Zhejiang University, to give him the answer.

    Input:
    The input consists of several test cases. Each test case begins with a positive integer N, which indicates the number of the coffers Jack has bought. Then the following N integers are the capacities of each box. The total number of the diamonds Jack has equals to the sum of the capacities of all the coffers.

    All the integers given are no greater than 12, you can be assured that a result always fits into a 32-bit integer.

    Output:
    For every test case, print out an integer representing the number of the different ways Jack can store his diamonds. Each integer is to be printed on a single line.

    Sample input:

    2
    3 3
    3
    1 2 3
    


    Sample output:

    10
    60

    题意:
    n个箱子,每个箱子能容纳ai个钻石,求m个不同的钻石全放到这些箱子里的方案数
    两个箱子如果容积相同,那么认为他们是一模一样的
    m=Σ ai

    假设k个箱子分别有 s1、s2、s3 个相同的
    C(tot,ai)*C(tot-a[1],a[2])*C(tot-a[1]-a[2],a[3])*……
    ans=---------------------------------------------------
    s1!*s2!*s3!

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[13],pre[13],rest[13],repeat[13];
    int sum[150];
    long long ans;
    long long pow(long long a,long long b)
    {
        long long r=1;
        while(b)
        {
            if(b&1) r*=a;
            b>>=1; a*=a;
        }
        return r;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            memset(repeat,0,sizeof(repeat)); 
            memset(sum,0,sizeof(sum));
            for(int i=1;i<=n;i++) scanf("%d",&a[i]),repeat[a[i]]++;
            sort(a+1,a+n+1);
            for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i];
            for(int i=1;i<=n;i++) rest[i]=pre[n]-pre[i-1];
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=rest[i];j++) sum[j]++;
                for(int j=1;j<=a[i];j++) sum[j]--;
                for(int j=1;j<=rest[i]-a[i];j++) sum[j]--;
                if(a[i]!=a[i+1] || i==n) 
                for(int j=1;j<=repeat[a[i]];j++) sum[j]--; 
            }
            ans=1;
            for(int i=2;i<=144;i++) 
             if(!sum[i]) continue;
             else if(sum[i]>0) ans=ans*pow(i,sum[i]);
            for(int i=2;i<=144;i++)
             if(sum[i]<0) ans=ans/pow(i,-sum[i]);
            printf("%lld
    ",ans);
         }
    }
  • 相关阅读:
    小米9一直无限重启是怎么办
    发现一个大神做了一个ROS-ROUTEROS的中文手册中文使用说明书
    浅谈CN2 GIA和CN2 GT线路的区别
    本地ROS多线访问同一个服务器的IP,比如阿里云的IP,创建冗余线路
    syslog之三:建立Windows下面的syslog日志服务器
    增值税专用发票“抵扣联”和“发票联”丢失怎么办
    在线播放 4K 内容的需要多少带宽?
    戴尔R640服务器用H740P配置阵列
    搞微服务用阿里开源的 Nacos 真香啊!
    保持ssh不自动断开
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7353590.html
Copyright © 2011-2022 走看看