zoukankan      html  css  js  c++  java
  • GCD

    GCD

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Problem Description
    Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs.
    Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.

    You can assume that a = c = 1 in all test cases.
     
    Input
    The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
    Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
     
    Output
    For each test case, print the number of choices. Use the format in the example.
     
    Sample Input
    2 1 3 1 5 1 1 11014 1 14409 9
     
    Sample Output
    Case 1: 9 Case 2: 736427
    Hint
    For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
    分析:gcd(x,y)=k,等价于gcd(x/k,y/k)=1;
       所以把区间缩小k倍,然后枚举小的数用容斥求出答案即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    const int maxn=1e5+10;
    using namespace std;
    inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline void umax(ll &p,ll q){if(p<q)p=q;}
    inline void umin(ll &p,ll q){if(p>q)p=q;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,cnt,fac[maxn],cas;
    int x,y;
    void init(int x)
    {
        cnt=0;
        if(x%2==0){
            fac[++cnt]=2;
            while(x%2==0)x/=2;
        }
        for(int i=3;(ll)i*i<=x;i+=2)
        {
            if(x%i==0)
            {
                fac[++cnt]=i;
                while(x%i==0)x/=i;
            }
        }
        if(x>1)fac[++cnt]=x;
    }
    ll gao(ll x)
    {
        ll ret=0;
        for(int i=1;i<(1<<cnt);i++)
        {
            ll num=0,now=1;
            for(int j=0;j<cnt;j++)
            {
                if(i&(1<<j))
                {
                    ++num;
                    now*=fac[j+1];
                }
            }
            if(num&1)ret+=x/now;
            else ret-=x/now;
        }
        return x-ret;
    }
    int main()
    {
        int i,j;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d%d%d",&x,&x,&y,&y,&k);
            if(!k)
            {
                printf("Case %d: 0
    ",++cas);
                continue;
            }
            x/=k,y/=k;
            if(x>y)swap(x,y);
            ll ret=0;
            rep(i,1,x)
            {
                init(i);
                ret+=gao(y)-gao(i-1);
            }
            printf("Case %d: %lld
    ",++cas,ret);
        }
        return 0;
    }
  • 相关阅读:
    夺命雷公狗---node.js---2node.js中的npm的常用命令
    nginx安装目录
    linux下安装nginx
    mui实现推送功能
    SpringBoot WebSocket实现
    发送短信验证码
    Vue在HTML页面中的脚手架
    SpringBoot邮件发送功能
    毕业设计功能
    毕业设计数据库
  • 原文地址:https://www.cnblogs.com/dyzll/p/6358259.html
Copyright © 2011-2022 走看看