Hdu 5446 Unknown Treasure (lucas+ China remainder theorem)

Source: Internet
Author: User

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

xa1 (mod m1), xa2 (mod m2) ,xa 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)

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.