Bzoj 2982 combination Lucas Theorem

Source: Internet
Author: User

Bzoj bzoj2982 Lucas theorem combination math

Question: I cannot review it after it is sent ...... In short, the general idea is to find C (n, m) mod 10007 m, n, [1, 2*10 ^ 8]

Lucas theorem: C (n, m) = C (n % P, M % P) * C (N/P, M/P) mod p requires P to be a prime number.

Where n % P may be less than M % P. In this case, 0 is returned directly.

Prove to ask Lucas. I don't know.

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define p 10007using namespace std;int fac[p],inv[p];void Linear_Shaker(){int i;fac[0]=1;for(i=1;i<p;i++)fac[i]=fac[i-1]*i%p;inv[1]=1;for(i=2;i<p;i++)inv[i]=(p-p/i)*inv[p%i]%p;inv[0]=1;for(i=1;i<p;i++)inv[i]=inv[i]*inv[i-1]%p;}int C(int n,int m){if(n<m)return 0;if(n<p&&m<p)return fac[n]*inv[m]%p*inv[n-m]%p;return C(n%p,m%p)*C(n/p,m/p)%p;}int main(){int T,n,m;Linear_Shaker();for(cin>>T;T;T--){scanf("%d%d",&n,&m);printf("%d\n",C(n,m));}}


Bzoj 2982 combination 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.