Title Address: http://acm.hdu.edu.cn/showproblem.php?pid=5446
Idea: Lucas find out all A[i]=c (n,m)%m[i], the Chinese remainder theorem to find the final result X (Ll*ll will explode, handwritten multiplication).
Chinese remainder theorem:
Set m1,m2,....mn is a positive integer of 22 coprime, for any given integer A1 , a2,....an must have integers to satisfy
x ≡a1 (mod m1), x≡ a2 (mod m2) ,x≡a 3 (mod m3) ...
and satisfies the solution of the above equation group x(mod m1m2m3.....mn) is the only one that exists.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;typedef long Long ll;int K; LL n,m; LL p[15],a[15]; LL fac[100005]; ll Qmul (ll x,ll y,ll MoD) {x= (x%mod+mod)%mod; Y= (y%mod+mod)%mod; LL ret=0; while (y) {if (y&1) ret= (ret+x)%mod; X=x*2%mod; y>>=1; } return ret;} ll EXTGCD (ll a,ll b,ll& x,ll& y) {ll d,t; if (!b) {x=1; y=0; return A; } d=extgcd (B,a%b,x,y); T=x; X=y; Y=t-a/b*y; return D;} ll CRT () {ll m=1,ans=0,x,y; for (int i=1; i<=k; i++) m*=p[i]; for (int i=1; i<=k; i++) {LL mi=m/p[i]; EXTGCD (Mi,p[i],x,y); Ans= (Ans+qmul (Qmul (mi,x,m), a[i],m))%M; } if (ans<0) ans=ans+m; return ans;} ll Powmod (ll a,ll b,ll MoD) {ll ret=1; while (b) {if (b&1) ret= (ret*a)%mod; A= (a*a)%mod; b>>=1; } return ret;} void Get_fac (LL mod) {fac[0]=1; For (inT i=1;i<=mod;i++) fac[i]= (fac[i-1]*i)%mod;} ll Lucas (ll n,ll m,ll p) {ll ret=1; while (n&&m) {LL a=n%p,b=m%p; if (a<b) return 0; Ret= (Ret*fac[a]*powmod (fac[b]*fac[a-b]%p,p-2,p))%p; N/=p,m/=p; } return ret;} int main () {int t; Ios::sync_with_stdio (0); cin>>t; while (t--) {cin>>n>>m>>k; memset (A,0,sizeof (a)); for (int i=1; i<=k; i++) {cin>>p[i]; GET_FAC (P[i]); A[i]=lucas (N,m,p[i]); } cout<<crt () <<endl; } return 0;}
Hdu 5446 Unknown Treasure (lucas+ China remainder theorem)