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;
    }
  • 相关阅读:
    JavaScript 中的undefined and null 学习
    html5 file upload and form data by ajax
    openresty + lua-resty-weedfs + weedfs + graphicsmagick动态生成缩略图(类似淘宝方案)
    ubuntu10.04 安装oracle server 版 笔记
    windows xp + mysql5.5 + phpmyadmin insert 中文繁體
    (原创)ubuntu 10.04+ruby1.9.2+rails3 安装记录
    ruby簡單的代碼行統計工具
    Ruby中如何复制对象 (deep clone)(转载)
    vi 常用命令使用說明
    一个小公司老板的日常管理日记,希望能让创业的朋友学到东西(转载)
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5813331.html
Copyright © 2011-2022 走看看