zoukankan      html  css  js  c++  java
  • Hdu3037 Saving Beans

    Saving Beans

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6236    Accepted Submission(s): 2518


    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
     
    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  3033 3038 3036 3035 3034 
    题目大意:有n个不同的盒子,在每个盒子中放一些球(可以不放),使得总球数≤m,求方案数(mod p)
    分析:设放了k个球,那么利用隔板法很容易就能求出方案数为C(k+n-1,n-1).总球数≤m,答案就是C(n-1,n-1) + C(n,n-1) + C(n+1,n-1)+......+
    C(n+m-1,n-1).画一个杨辉三角,可以发现这个式子表示的是从第n-1列的第1个数往下加m个的和,正好等于最下面的右下角的那一个数.其实也很好证明,C(i,j) = C(i-1,j-1) + C(i,j-1),将得到的那个数反着退回去就行了,所以答案为C(n+m,n).
              注意到这个n,m都特别大,又是多组数据,不可能用一个数组存下来的,直接乘也会T掉,p比较小,还是个素数,那么可以用lucas定理:lucas(n,m,p) = lucas(n/p,m/p,p) * c(n%p,m%p).其中c(a,b) = a! * (b! * (a-b)!) ^ (p-2) mod p. 当m递归到0的时候,返回1.
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long ll;
    
    ll n, m, p, T, sum[100010];
    
    ll qpow(ll a, ll b)
    {
        ll res = 1;
        while (b)
        {
            if (b & 1)
                res = (res * a) % p;
            a = (a * a) % p;
            b >>= 1;
        }
        return res;
    }
    
    ll getc(ll x, ll y, ll p)
    {
        ll temp1 = 1, temp2 = 1;
        if (x < y)
            return 0;
        return sum[x] * qpow(sum[y], p - 2) % p * qpow(sum[x - y], p - 2) % p;
    }
    
    ll lucas(ll x, ll y, ll p)
    {
        if (y == 0)
            return 1;
        return getc(x % p, y % p, p) * lucas(x / p, y / p, p) % p;
    }
    
    int main()
    {
        scanf("%lld", &T);
        while (T--)
        {
            scanf("%lld%lld%lld", &n, &m, &p);
            sum[0] = 1;
            for (ll i = 1; i <= p; i++)
                sum[i] = (sum[i - 1] * i) % p;
            printf("%lld
    ", lucas(n + m, n, p));
        }
    
        return 0;
    }
  • 相关阅读:
    自动化测试成功11333
    自动化测试成功11222
    自动化测试成功1111
    自动化测试成功112
    自动化测试成功18
    自动化测试成功1
    自动化测试成功
    富文本测试
    关于VMware虚拟机磁盘收缩的几种方法
    8个让你更有效率的简单方法
  • 原文地址:https://www.cnblogs.com/zbtrs/p/7953165.html
Copyright © 2011-2022 走看看