hdu3037 (Lucas theorem)

Source: Internet
Author: User

Given n,m,p means that <=m beans are placed on n trees, how many schemes are there, total proposal mod p

If you place the M beans on n trees, you can use the Insert plate method

Get program number is C (n+m-1,n-1)

So the number of programs that put 0<=i<=m beans on n Trees is C (n+i-1,n-1)

That

where C (n,k) =c (n-1,k) +c (n-1,k-1) is the meaning of extracting k from the n number of combinations, then for a number, it is either not taken, to C (n-1,k), or to C (n-1,k-1)

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <algorithm>5#include <iostream>6#include <queue>7#include <stack>8#include <vector>9#include <map>Ten#include <Set> One#include <string> A#include <math.h> - using namespacestd; - #pragmaWarning (disable:4996) thetypedefLong LongLL;  - Const intINF =1<< -; - /* -  + */ -  +LL fact[100001]; A  at ll Pow (ll A, LL K, ll P) - { -LL ret =1; -      while(k) -     { -         if(K &1) inRET = ret * A%p; -A = a * a%p; toK >>=1; +     } -     returnret; the } * ll C (ll N, ll M, ll P) $ {Panax Notoginseng     if(N < m | | m <0)return 0; -LL a = Fact[n], b = fact[n-m] * Fact[m]%p; the     returnA * POW (b, P-2, p)%p; + } ALL Lucas (intNintMintp) the { +     if(M = =0)return 1; -     returnC (n%p, m%p,p) * Lucas (n/p, m/p, p)%p; $ } $ intMain () - { -     intt, N, M, p; thescanf"%d", &t); -      while(t--)Wuyi     { thescanf"%d%d%d", &n, &m, &p); -fact[0] =1; Wu          for(inti =1; I <= p; ++i) -Fact[i] = fact[i-1] * I%p; Aboutprintf"%i64d\n", Lucas (n +m, n, p)); $  -     } -     return 0; -}

hdu3037 (Lucas theorem)

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.