Hdu 5768 (Chinese remainder theorem + tolerance)

Source: Internet
Author: User

Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=5768;

Topic Analysis:

Because it satisfies any group of pi and Ai, a "lucky number" can be "contaminated" and we could think of dealing with this problem through the repulsion. When we select a series of pi and AI, test instructions is converted to [x, Y] in order to be 7 divisible by 0, and by this series of Pi in addition to the number of AI, can be considered as a number of congruence equations of a single congruence equation set. Then we can naturally think of the Chinese remainder theorem. It should be noted that in the process of dealing with the Chinese remainder theorem, more than longlong may occur, and a fast multiplication similar to the fast power should be written to deal with. Of course there are a lot of principles when counting, for example, the number of less than 100mod3=2 is 100/3; the formula that satisfies the Chinese remainder theorem only needs to be divided by this number and then added one;

The code is as follows:

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream > #include <algorithm> #include <map> #include <queue> #include <vector>using namespace std; typedef long LONG ll;const int N = 20;const int INF = 0x3f3f3f3f;const ll mod = 1e9+7;void exgcd (LL a,ll b,ll &AMP;D,LL&A mp        x,ll& y) {if (!b) {d=a;        X=1;    y=0;        } else{EXGCD (b,a%b,d,y,x);    Y-=x* (A/b); }}ll A[20],M[20];    ll Qpow (ll a,ll b,ll MoD) {a%=mod;    LL ret=0;        while (b) {if (b&1) ret= (ret+a)%mod;        b>>=1;    A= (a+a)%mod; } return ret;}    Fast multiplication ll China (int n,ll *a,ll *m) {ll m=1,d,y,x=0;    for (int i=0;i<n;++i) m*=m[i];        for (int i=0;i<n;++i) {LL w=m/m[i];        EXGCD (M[i],w,d,d,y);    X= (X+qpow (Qpow (y,w,m), a[i],m))%M; } return (X+m)%M;}    China remainder theorem ll P[n],yu[n];int main () {int kase=0,n,t;    scanf ("%d", &t);        while (t--) {LL l,r; ScANF ("%d", &n);        cin>>l>>r;        for (int i=0;i<n;++i) cin>>p[i]>>yu[i];        int len= (1&LT;&LT;N);        LL ret=r/7-(L-1)/7;            for (int i=1;i<len;++i) {int cnt=0;            LL cur=1;                    for (int j=0;j<n;++j) {if (i& (1<<j)) {m[cnt]=p[j];                    A[CNT]=YU[J];                    cnt++;                CUR*=P[J];            }} m[cnt]=7;            a[cnt]=0;            cur*=7;            cnt++;            LL Tmp=china (cnt,a,m);            LL sub=0;                if (tmp>=l&&tmp<=r) {LL cha=r-tmp;                sub=cha/cur+1;//Count Method} else if (tmp<l) {LL cha=r-tmp;                sub+=cha/cur+1;                cha=l-tmp;            sub-=cha/cur+1;            } if (cnt&1) ret+=sub;        else ret-=sub; } printf ("Case #%d:%i64d\n", ++kasE,ret); } return 0;}



Hdu 5768 (Chinese remainder theorem + tolerance)

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.