zoukankan      html  css  js  c++  java
  • Hdu 3037 Saving Beans(Lucus定理+乘法逆元)

    Saving Beans
    Time Limit: 3000 MS Memory Limit: 32768 K
    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
    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.
    题意:
    由n个不同的盒子,在每个盒子中放一些球(可以不放),使得总球数<=m,求方案数模p后的值.
    1<=n,m<=10^9,1< p < 10^5,保证p是素数.
    题解(第一次用数学编辑器2333)
    这里写图片描述

    #include<iostream>
    #include<cstdio>
    #define MAXN 100001
    #define LL long long
    using namespace std;
    LL M[MAXN];
    LL mi(LL a,LL b,LL p)
    {
        LL tot=1;
        while(b)
        {
            if(b&1) tot=tot*a%p;
            a=a*a%p;
            b>>=1;
        }
        return tot;
    }
    LL C(LL n,LL m,LL p)
    {
        if(m>n) return 0;
        LL tot=1;
        return M[n]*mi(M[n-m],p-2,p)%p*mi(M[m],p-2,p)%p;
    }
    LL lucus(LL n,LL m,LL p)
    {
        if(!m) return 1;
        return lucus(n/p,m/p,p)*C(n%p,m%p,p)%p;
    }
    int main()
    {
        LL n,m,p,t;
        cin>>t;
        while(t--)
        {
            cin>>n>>m>>p;
            M[0]=1;
            for(int i=1;i<=p;i++) M[i]=M[i-1]*i%p;
            printf("%lld
    ",lucus(n+m,m,p));
        }
        return 0;
    }
  • 相关阅读:
    JavaScript模态对话框类
    事件模块的演变(1)
    html5中可通过document.head获取head元素
    How to search for just a specific file type in Visual Studio code?
    What do 'lazy' and 'greedy' mean in the context of regular expressions?
    正则非获取匹配 Lookahead and Lookbehind ZeroLength Assertions
    regex length 正则长度问题
    Inversion of Control vs Dependency Injection
    How to return View with QueryString in ASP.NET MVC 2?
    今天才发现Google Reader
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10067986.html
Copyright © 2011-2022 走看看