zoukankan      html  css  js  c++  java
  • HDU1695(容斥原理)

    GCD

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 9811    Accepted Submission(s): 3682


    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.

    Yoiu 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
     
    思路:题意可转化为求[1,b/k]与[1,d/k]组成数对(x,y)。x,y互质的对数。当x与y均不大于min(b/k,d/k)时,需要将答案除以2。
    #include <cstdio>
    #include <algorithm> 
    #include <vector>
    using namespace std;
    const int MAXN=100001;
    typedef long long LL;
    LL b,d,k;
    vector<LL> divisor[MAXN];
    void prep()
    {
        for(LL e=1;e<MAXN;e++)
        {
            LL x=e;
            for(LL i=2;i*i<=x;i++)
            {
                if(x%i==0)
                {
                    divisor[e].push_back(i);
                    while(x%i==0)    x/=i;
                }
            }
            if(x>1)    divisor[e].push_back(x);
        }
    }
    LL sieve(LL m,LL n)
    {
        LL ans=0;
        for(LL mark=1;mark<(1<<divisor[n].size());mark++)
        {
            LL mul=1;
            LL odd=0;
            for(LL i=0;i<divisor[n].size();i++)
            {
                if(mark&(1<<i))
                {
                    mul*=divisor[n][i];
                    odd++;
                }
            }
            LL cnt=m/mul;
            if(odd&1)    ans+=cnt;
            else    ans-=cnt;
        }
        return m-ans;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        prep();
        for(int cas=1;cas<=T;cas++)
        {
            scanf("%*d%lld%*d%lld%lld",&b,&d,&k);
            printf("Case %d: ",cas);
            if(k==0)
            {
                printf("%d
    ",0);
                continue;
            } 
            b/=k;
            d/=k;
            if(b>d)    swap(d,b);
            LL res=0;
            for(LL i=1;i<=b;i++)
            {
                LL cnt=sieve(b,i);
                res+=cnt;
            }
            res=(res+1)/2;
            for(LL i=b+1;i<=d;i++)
            {
                LL cnt=sieve(b,i);
                res+=cnt;
            }
            printf("%lld
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    ScrollView 字典
    centos 6.x 安装redis
    Linux 添加epel源
    Linux 关于解压
    Linux 删除文件夹
    Linux sz rz
    让div 实现 input效果
    解决js浮点数计算bug
    键盘绑定事件和焦点处理
    npm的镜像替换成淘宝
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5813331.html
Copyright © 2011-2022 走看看