zoukankan      html  css  js  c++  java
  • [HDU] 3037 Saving Beans

    Saving Beans
    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6279    Accepted Submission(s): 2537
    
    
    Problem Description
    Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They suppose that they will save beans in n different trees. However, since the food is not sufficient nowadays, they will get no more than m beans. They want to know that how many ways there are to save no more than m beans (they are the same) in n trees.
    
    Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.
    
    
    Input
    The first line contains one integer T, means the number of cases.
    
    Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime. 
    
    
    Output
    You should output the answer modulo p.
    
    
    Sample Input
    2
    1 2 5
    2 1 5
    
    
    Sample Output
    3
    3
    Hint
    Hint
    
    For sample 1, squirrels will put no more than 2 beans in one tree. Since trees are different, we can label them as 1, 2and so on. 
    The 3 ways are: put no beans, put 1 bean in tree 1 and put 2 beans in tree 1. For sample 2, the 3 ways are:
     put no beans, put 1 bean in tree 1 and put 1 bean in tree 2.
    
    
    
    Source
    2009 Multi-University Training Contest 13 - Host by HIT 
    
    
    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  3033 3038 3036 3035 3034 
    

    题目可以转化为 n个盒子,m个球,允许空着不放,有多少种放法。
    用神器的挡板法可以求得
    当放了k(k<=m)个球,有C(k-n+1,n-1)种放法。
    求和,利用杨辉三角递推式化简。
    ANS=C(n-1,n-1)+C(n,n-1)+C(n+1,n-1)+……….+C(n+m-2,n-1)+C(n+m-1,n-1)
    =C(n+m,n)

    再用Lucas定理解决模p即可。
    (原来的快速幂好像。。出现了问题?)

    #include<iostream>
    #include<cstdio>
    using namespace std;
    
    long long  t;
    
    long long poww2(long long x,long long m,long long p){
        if(m==0) return 1;
        long long tmp=poww2(x,m/2,p);
        tmp=(tmp*tmp)%p;
        if(m%2==1) tmp=(tmp*x)%p;
        return tmp;
    }
    
    
    long long poww(long long a,long long b) {
        long long ans=1,base=a;
        if(b==0) return 1;
        while(b!=0) {
            if(b&1!=0)
                ans*=base;
            base*=base;
            b>>=1;
        }
        return ans;
    }
    
    long long c(long long m,long long n,long long p) {
        if(m<n) return 0;
        if(n>m-n) n=m-n;
        long long s1=1,s2=1;
        for(int i=1; i<=n; i++) {
            s1=s1*(m-i+1)%p;
            s2=s2*i%p;
        }
        return s1*poww2(s2,p-2,p)%p;
    }
    
    long long zc(long long x,long long y) {
        if(y==1) return x;
        return 1.0*x/y*zc(x-1,y-1);
    }
    
    long long lucas(long long m,long long n,long long p) {
        if(n==0) return 1;
        return c(m%p,n%p,p)*lucas(m/p,n/p,p)%p;
    }
    
    int main() {
        long long m,n,r;
        scanf("%lld",&t);
    //  cin>>t;
        while(t--) {
        scanf("%lld%lld%lld",&m,&n,&r);
    //  cin>>m>>n>>r;
    //  cout<<"C: "<<zc(m,n);
    //  cout<<endl<<"%: ";
    //  cout<<m<<" "<<n<<endl;
        printf("%lld
    ",lucas(m+n,m,r));
    //  cout<<lucas(m+n,m,r)<<endl;
    //  cout<<"STD:"<<zc(m+n,n)<<endl;;
        }
        return 0;
    }

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247520.html

  • 相关阅读:
    写时复制集合 —— CopyOnWriteArrayList 源码原理阅读笔记
    初步整合vue-element-admin和GitDataV两个Vue开源框架方案实现大数据可视化
    IOS苹果登录sign in with apple后端校验
    IOS审核被拒:IOS苹果授权登录(Sign in with Apple)/Apple登录/苹果登录集成教程
    ios安装自定义基座失败问题
    IOS APP上架App Store及提交审核详细教程
    IOS APP报错:SyntaxError: Invalid regular expression: invalid group specifier name __ERROR
    Apple Pay苹果支付IOS in-App Purchase内购项目服务端校验
    浅析浏览器是如何工作的(一):V8引擎、JIT机制、JS代码解释执行与编译执行
    ApplePay苹果支付内购项目配置及代码实现及沙箱测试
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9247520.html
Copyright © 2011-2022 走看看