Hdu 3027 Saving Beans, Lucas theorem

Source: Internet
Author: User

Saving Beans Time limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3112 Accepted Submission (s): 1177


Problem Description Although winter is far away, squirrels has 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 has to solve a problem. They suppose that they would save beans in n different trees. However, since the food was not sufficient nowadays, they would get no more than M beans. They want to know, how many ways there is to save no more than M beans (they is the same) in n trees.

Now they turn-to-do, 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 would save no more than M same beans In n different trees, 1 <= N, M <= 1000000000, 1 < p < 100000 and p are 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 would put no more than 2 beans in one tree. Since trees is 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 beans in tree 1 and put 1 beans in tree 2.
Source multi-university Training Contest 13-host by hit http://www.xuebuyuan.com/1756839.html reference blog.

Lucas theorem C (n,m)%p = C (n/p,m/p) *c (n%p,m%p)


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define MAXN 100007
int JIE[MAXN];
int NI[MAXN];
#define LL Long Long
int cal (int n,int t,int p) {
    int ans = 1;
    while (t) {
        if (t&1) ans = (ll) ans*n%p;
        n = (ll) n*n%p;
        t/=2;
    }
    return ans;
}

int Lucas (int n,int m,int p) {
    if (m = = 0) return 1;
    int a = n p, b = m% p;
    int ans = 0;
    if (a >= b) {
        int c=1,d=1;
        while (b) {
            c = (ll) c*a%p;
            D = (ll) d*b%p;
            A--, b--;
        }
        Ans = (ll) c*cal (d,p-2,p)%p;
    }
    else return 0;
    Return (LL) Lucas (n/p,m/p,p) *ans%p;

int main () {
    int t,n,m,p;
    cin>>t;
    while (t--) {
        scanf ("%d%d%d", &n,&m,&p);
        printf ("%d\n", Lucas (N+m,m,p));
    }
    return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.