HDU3037 Saving Beans (Lucas theorem + multiplicative inverse)

Source: Internet
Author: User

The topic probably asks that the items that are less than or equal to M are placed in n places in several ways.

That is, the number of nonnegative integer solutions of the N-ary equation $x_1+x_2+x_3+\dots+x_n=y$, which is 0<=y<=m.

The number of nonnegative integer solutions of this equation is a classical problem, and the number of solutions of the positive integers of +1 can be solved by inserting plate method: $C _{y+n-1}^{n-1}=c_{y+n-1}^y$.

And 0<=y<=m, the final result is--

$$\sum_{i=0}^m c_{i+n-1}^i$$

$ $C _{n-1}^0+c_{n}^1+c_{n+1}^2+\dots+c_{n-1+m}^m$$

$ $C _{n}^0+c_{n}^1+c_{n+1}^2+\dots+c_{n-1+m}^m$$

$ $C _{n+1}^1+c_{n+1}^2+\dots+c_{n-1+m}^m\ \tag{$C _{n+1}^1=c_{n}^0+c_{n}^1$}$$

$ $C _{n+2}^2+\dots+c_{n-1+m}^m\ \tag{$C _{n+2}^2=c_{n+1}^1+c_{n+1}^2$}$$

$$\vdots$$

$ $C _{n+m}^m$$

So the result is $c_{n+m}^m$. So is the calculation $c_{n+m}^m\ mod \ p$, where 1<=n,m<=1000000000,1<p<100000 and p are prime numbers.

This is then used to calculate the modulus of this large combination of Lucas theorem: $Lucas (n,m) \equiv c_{n\%p}^{m\%p}\times Lucas (n/p,m/p) \pmod p$.

In addition, the multiplication inverse of modulo p is used to $C _n^m\equiv\frac {n!} When calculating the combined number. {(n-m)!m!} \equiv N!\times ((n-m)!m!) ^{-1} \pmod p$

And it is not necessary to calculate the inverse of the Euclidean algorithm, because P is a prime number, the use of the Ma Xiao theorem can be introduced n in modulo p multiplication inverse of the $n^{p-2}\ mod\ p$.

1#include <cstdio>2#include <cstring>3 using namespacestd;4 Long LongIneLong LongNLong Longp) {5     Long Longres=1, m=p-2;6      while(m) {7         if(m&1) res=res*n%p;8n=n*n%p;9m>>=1;Ten     } One     returnRes; A } - Long Longfact[100000]={1}; - Long LongLucasLong LongNLong LongMLong Longp) {   the     Long Longres=1;  -      while(n&&m) {   -         Long Longa=n%p,b=m%p;  -         if(A&LT;B)return 0;  +Res=res*fact[a]*ine (fact[b]*fact[a-b]%p,p)%p;  -N/=p; M/=p; +     } A     returnRes; at } - intMain () { -     Long Longn,m,p; -     intT; -scanf"%d",&t); -      while(t--){ inscanf"%lld%lld%lld",&n,&m,&p); -          for(intI=1; i<p; ++i) fact[i]=fact[i-1]*i%p; toprintf"%lld\n", Lucas (n+m,m,p)); +     } -     return 0; the}

HDU3037 Saving Beans (Lucas theorem + multiplicative inverse)

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.