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): 4315    Accepted Submission(s): 1687


    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, 2 … and 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
     
     
     
    题目大意:把不超过m颗种子放到n颗树上的方案数。
     
    哼!一直不明白为什么是C(n+m,m),网上的题解我也看不懂...(垃圾
    好久终于明白了。ono
     
    题解:假设就是m颗种子,那么原题就相当于
    x1+x2+x3+x4+....+xn=m
    然后用插板法我们知道,方案数为C(m-1,n-1)。
    但是题目是不超过m,那么就是把0颗豆子放到n个树,1颗豆子放到n个树,2
    颗豆子...m颗豆子。
    那么我们再造一颗树标号为n+1,那么如果把k个豆子放入n颗树,然后再把m-k个
    豆子放入第n+1颗树。
    因为树可以为空,所以我们设b[i]=x[i]+1,
    因为x[1]+x[2]+x[3]+...+x[n+1]=m,
    那么b[1]+b[2]+b[3]+...+b[n+1]=m+n+1..
    那么就相当于把n个挡板放入m+n个空中,就是C(m+n,n)=C(m+n,m)。
     
    代码:hdu炸了没测
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define LL long long
    using namespace std;
    
    int T;
    LL n,m,p,f[100009];
    
    void pre(){
        f[0]=1;
        for(int i=1;i<=p;i++)f[i]=f[i-1]*i%p;    
    }
    
    LL ksm(LL x,LL y){
        LL ret=1%p;
        while(y){
            if(y&1)ret=ret*x%p;
            x=x*x%p;
            y>>=1;
        }
        return ret;
    }
    
    LL C(LL n,LL m){
        if(m>n)return 0;//*******
        return f[n]*ksm(f[m],p-2)%p*ksm(f[n-m],p-2)%p;
    }
    
    LL Lucas(LL n,LL m){
        if(!m)return 1;
        return C(n%p,m%p)*Lucas(n/p,m/p)%p;
    }
    
    int main(){
        scanf("%d",&T);
        while(T--){
            scanf("%lld%lld%lld",&n,&m,&p);
            pre();
            printf("%lld
    ",Lucas(n+m,m)%p);
        }        
        return 0;
    }
  • 相关阅读:
    Python 十七天 成员
    python学习17——字典。
    python学习16——列表。
    python学习15——Random。
    python学习14——分支和函数。
    python学习13——while循环。
    python学习12——循环和列表
    python学习8——整理第一部分。
    Flask HTTP请求与响应
    Falsk 路由简析
  • 原文地址:https://www.cnblogs.com/zzyh/p/7668043.html
Copyright © 2011-2022 走看看