• hdu 5768 Lucky7 中国剩余定理+容斥+快速乘


    Lucky7

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


    Problem Description
    When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7 candles when he faced a extremely difficult problem, and always solve it in seven minutes.
    ?? once wrote an autobiography, which mentioned something about himself. In his book, it said seven is his favorite number and he thinks that a number can be divisible by seven can bring him good luck. On the other hand, ?? abhors some other prime numbers and thinks a number x divided by pi which is one of these prime numbers with a given remainder ai will bring him bad luck. In this case, many of his lucky numbers are sullied because they can be divisible by 7 and also has a remainder of ai when it is divided by the prime number pi.
    Now give you a pair of x and y, and N pairs of ai and pi, please find out how many numbers between x and y can bring ?? good luck.
     
    Input
    On the first line there is an integer T(T≤20) representing the number of test cases.
    Each test case starts with three integers three intergers n, x, y(0<=n<=15,0<x<y<1018) on a line where n is the number of pirmes.
    Following on n lines each contains two integers pi, ai where pi is the pirme and ?? abhors the numbers have a remainder of ai when they are divided by pi.
    It is guranteed that all the pi are distinct and pi!=7.
    It is also guaranteed that p1*p2*…*pn<=1018 and 0<ai<pi<=105for every i∈(1…n).
     
    Output
    For each test case, first output "Case #x: ",x=1,2,3...., then output the correct answer on a line.
     
    Sample Input
    2 2 1 100 3 2 5 3 0 1 100
     
    Sample Output
    Case #1: 7 Case #2: 14
    Hint
    For Case 1: 7,21,42,49,70,84,91 are the seven numbers. For Case2: 7,14,21,28,35,42,49,56,63,70,77,84,91,98 are the fourteen numbers.
     
    Author
    FZU
     
    Source

    2016 Multi-University Training Contest 4

    思路:套一个中国剩余定理两两不互质的模版;

        容斥一发。。。由于犯了一个sb错,wa一天,不想说什么了;

      

    1005  Lucky7

    因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题。当我们选定了一系列pi和ai后,题意转化为求 [x,y]中被7整除余0,且被这一系列pi除余ai的数的个数,可以看成若干个同余方程联立成的一次同余方程组。然后我们就可以很自然而然的想到了中国 剩余定理。需要注意的是,在处理中国剩余定理的过程中,可能会发生超出LongLong的情况,需要写个类似于快速幂的快速乘法来处理。

    二进制枚举:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll __int64
    #define esp 0.00000000001
    const int N=1e2+10,M=1e6+10,inf=1e9+10,mod=1000000007;
    ll a[N];
    ll b[N];
    ll p[N];
    ll m[N];
    ll mulmod(ll x,ll y,ll m)
    {
        ll ans=0;
        while(y)
        {
            if(y%2)
            {
                ans+=x;
                ans%=m;
            }
            x+=x;
            x%=m;
            y/=2;
        }
        ans=(ans+m)%m;
        return ans;
    }
    void exgcd(ll a, ll b, ll &x, ll &y)
    {
        if(b == 0)
        {
            x = 1;
            y = 0;
            return;
        }
        exgcd(b, a % b, x, y);
        ll tmp = x;
        x = y;
        y = tmp - (a / b) * y;
    }
    ll CRT(ll a[],ll m[],ll n)
    {
        ll M = 1;
        ll ans = 0;
        for(ll i=0; i<n; i++)
            M *= m[i];
        for(ll i=0; i<n; i++)
        {
            ll x, y;
            ll Mi = M / m[i];
            exgcd(Mi, m[i], x, y);
            //ans = (ans + Mi * x * a[i]) % M;
            ans = (ans +mulmod( mulmod( x , Mi ,M ), a[i] , M ) ) % M;
        }
        ans=(ans + M )% M;
        return ans;
    }
    int main()
    {
        ll x,y,z,i,t;
        int T,cas=1;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%I64d%I64d%I64d",&x,&y,&z);
            for(i=1;i<=x;i++)
            scanf("%I64d%I64d",&b[i],&a[i]);
            ll ans=0;
            for(i=0;i<(1<<x);i++)
            {
                ll cnt=0,mul=7;
                p[cnt]=0;
                m[cnt++]=7;
                for(int ji=1,t=i;t>0;t>>=1,ji++)
                if(t&1)mul*=b[ji],p[cnt]=a[ji],m[cnt++]=b[ji];
                ll pp=CRT(p,m,cnt);
                if(cnt&1)
                ans+=z/mul+(z%mul>=pp)-((y-1)/mul+(((y-1)%mul)>=pp));
                else
                ans-=z/mul+(z%mul>=pp)-((y-1)/mul+(((y-1)%mul)>=pp));
            }
            printf("Case #%d: %I64d
    ",cas++,ans);
        }
        return 0;
    }

    dfs写法:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll __int64
    #define esp 0.00000000001
    const int N=1e2+10,M=1e6+10,inf=1e9+10,mod=1000000007;
    ll a[N];
    ll b[N];
    ll ji;
    ll mulmod(ll x,ll y,ll m)
    {
        ll ans=0;
        while(y)
        {
            if(y%2)
            {
                ans+=x;
                ans%=m;
            }
            x+=x;
            x%=m;
            y/=2;
        }
        ans=(ans+m)%m;
        return ans;
    }
    void exgcd(ll a, ll b, ll &x, ll &y)
    {
        if(b == 0)
        {
            x = 1;
            y = 0;
            return;
        }
        exgcd(b, a % b, x, y);
        ll tmp = x;
        x = y;
        y = tmp - (a / b) * y;
    }
    ll CRT(ll p,ll m,ll k,ll l)
    {
        ll M = m * l;
        ll ans = 0;
        ll x, y;
        ll Mi = l;
        exgcd(Mi, m, x, y);
        ans = (ans + mulmod( mulmod( x%M, Mi%M, M) , p%M , M))% M;
        Mi = m;
        exgcd(Mi, l, x, y);
        ans = (ans + mulmod( mulmod(x%M ,Mi%M , M) , k%M, M))% M;
        if(ans < 0) ans += M;
        return ans;
    }
    void dfs(ll p,ll m,ll pos,ll step,ll x,ll &ans)
    {
        if(pos==ji)
        {
            if(step%2)
            {
                ans-=x/m;
                if(x%m>=p)
                    ans--;
            }
            else
            {
                ans+=(x/m);
                if(x%m>=p)
                    ans++;
            }
            return;
        }
        dfs(CRT(p,m,a[pos],b[pos]),m*b[pos],pos+1,step+1,x,ans);
        dfs(p,m,pos+1,step,x,ans);
    }
    int main()
    {
        ll x,y,z,i,t;
        int T,cas=1;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%I64d%I64d%I64d",&x,&y,&z);
            ji=x;
            for(i=0; i<x; i++)
            scanf("%I64d%I64d",&b[i],&a[i]);
            ll ansr=0,ansl=0;
            dfs(0,7,0,0,z,ansr);
            dfs(0,7,0,0,y-1,ansl);
            printf("Case #%d: %I64d
    ",cas++,ansr-ansl);
        }
        return 0;
    }
  • 相关阅读:
    Datawhale文化运营 —— 推文排版
    Datawhale文化运营 —— 策划活动
    Datawhale文化运营 —— 选题
    Datawhale文化运营 —— 分析公众号运营
    前端面试——记一次于某司的经历
    Win10+Cent7双系统安装
    梳理 Opengl ES 3.0 (五)shader运行原理
    梳理 Opengl ES 3.0 (三)顶点坐标变换
    梳理 Opengl ES 3.0 (二)剖析一个GLSL程序
    梳理 Opengl ES 3.0 (一)宏观着眼
  • 原文地址:https://www.cnblogs.com/jhz033/p/5718325.html
Copyright © 2020-2023  润新知