zoukankan      html  css  js  c++  java
  • hdu 3037Saving Beans(卢卡斯定理)

    Saving Beans

    Saving Beans

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


    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
     
    /*
    题目相当于求n个数的和不超过m的方案数。
    如果和恰好等于m,那么就等价于方程x1+x2+...+xn = m的解的个数,利用插板法可以得到方案数为:
    (m+1)*(m+2)...(m+n-1)  = C(m+n-1,n-1) = C(m+n-1,m)
    现在就需要求不大于m的,相当于对i = 0,1...,m对C(n+i-1,i)求和,根据公式C(n,k) = C(n-1,k)+C(n-1,k-1)得
    C(n-1,0)+C(n,1)+...+C(n+m-1,m)
    = C(n,0)+C(n,1)+C(n+1,2)+...+C(n+m-1,m)
    = C(n+m,m)
    现在就是要求C(n+m,m) % p,其中p是素数。
    然后利用Lucas定理的模板就可以轻松的求得C(n+m,m) % p的值
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    #define N 100007
    
    using namespace std;
    long long f[N];
    
    long long Mi(long long a,long long b,long long p)
    {
        long long res=1;
        while(b)
        {
            if(b&1) res=res*a%p;
            b>>=1;a=a*a%p;
        }return res;
    }
    
    long long C(long long n,long long m,long long p)
    {
        if(m>n)return 0;
        return  f[n]*Mi(f[m]*f[n-m]%p,p-2,p)%p;
    }
    
    long long Lcs(long long n,long long m,long long p)
    {
        if(m==0)return 1;
        return (C(n%p,m%p,p)*Lcs(n/p,m/p,p))%p;
    }
    
    int main()
    {
        long long n,m,p;long long t;        
        cin>>t;
        while(t--)
        {
            cin>>n>>m>>p;
            f[0]=1;
            for(long long i=1;i<=p;i++)
              f[i]=f[i-1]*i%p;
            printf("%lld
    ",Lcs(n+m,m,p));
        }
        return 0;
    }
    折花枝,恨花枝,准拟花开人共卮,开时人去时。 怕相思,已相思,轮到相思没处辞,眉间露一丝。
  • 相关阅读:
    Java 中最常见的 5 个错误
    在 Java EE 组件中使用 Camel Routes
    virtualenv 环境下 Nginx + Flask + Gunicorn+ Supervisor 搭建 Python Web
    [译]如何使用 Docker 组件开发 Django 项目?
    7 天玩转 ASP.NET MVC
    如何开发一个自己的 RubyGem?
    [译] 提高日志质量的 5 大技巧
    Ruby Profiler 详解之 stackprof
    总结 | 如何测试你自己的 RubyGem
    第十一节:讲述类的继承,数据库,文件的读写,图形绘制
  • 原文地址:https://www.cnblogs.com/L-Memory/p/7352542.html
Copyright © 2011-2022 走看看