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 同步 、锁 、 synchronized 、 Thread 、 Runnable
    装饰设计模式 Decorator
    Java File IO 字节字符流
    觉得 eclipse 不好用的,了解一下快捷键,辅助快捷输入 类创建方式
    Power builder PB 使用 相关记录
    java 多态 深入理解多态-隐藏-低调-伪装
    Idea 出现 Error:java: Compilation failed: internal java compiler error
    Access 导入到 SQLServer
    20200117 .net 001
    Android ConstraintLayout详解(from jianshu)
  • 原文地址:https://www.cnblogs.com/L-Memory/p/7352542.html
Copyright © 2011-2022 走看看