zoukankan      html  css  js  c++  java
  • hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。

    GCD
    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 10992    Accepted Submission(s): 4157
    
    
    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
    
    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).
    
    /**
    题目:hdu1695 GCD2
    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
    题意:求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。
    思路:
    gcd(x,y)=k => gcd(x/k,y/k) = 1;
    
    则求x/k与y/k互质对数。
    
    即求:[1,b/k]与[1,d/k]之间互质的对数
    
    设x属于[1,b/k], y属于[1,d/k];
    枚举x,求x与y互质的对数。所以要预处理所有x的质因子。然后容斥处理。由于(x,y)=>(5,7),(7,5)是同一组。
    所以:答案为ans += (d/k) - x在d/k中不互质的数 - (x-1);
    */
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #define LL long long
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 1e5+10;
    vector<int> prime[maxn];
    int flag[maxn];
    void init()
    {
        memset(flag, 0, sizeof flag);
        for(ll i = 2; i < maxn; i++){
            if(flag[i]==0){
                prime[i].push_back(i);
                for(ll j = 2*i; j < maxn; j+=i){
                    prime[j].push_back(i);
                    flag[j] = 1;
                }
            }
        }
    }
    ll rc(int pos,int n)
    {
        ll sum = 0;
        ll mult, ones;
        ll len = prime[pos].size();
        ll m = 1<<len;
        for(int i = 1; i < m; i++){
            ones = 0;
            mult = 1;
            for(int j = 0; j < len; j++){
                if(i&(1<<j)){
                    ones++;
                    mult = mult*prime[pos][j];
                    if(mult>n) break;
                }
            }
            if(ones%2==0){
                sum -= n/mult-(pos-1)/mult;
            }else
            {
                sum += n/mult-(pos-1)/mult;
            }
        }
        return n-(pos-1)-sum;
    }
    int main()
    {
        init();
        int T;
        int cas = 1;
        int a, b, c, d, k;
        cin>>T;
        while(T--)
        {
            scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
            if(k==0){
                printf("Case %d: 0
    ",cas++);continue;
            }
            if(b>d) swap(b,d);///for b<=d;
            b = b/k;
            d = d/k;
            ll ans = 0;
            if(b>=1){
                ans += d;
            }
            for(int i = 2; i <= b; i++){
                ans += rc(i,d);
            }
            printf("Case %d: %lld
    ", cas++,ans);
        }
        return 0;
    }
  • 相关阅读:
    替换OSD操作的优化与分析
    Centos7下Jewel版本radosgw服务启动
    如何统计Ceph的RBD真实使用容量
    Ceph中的Copyset概念和使用方法
    Proftp最简匿名访问配置
    Windows could not set the offline local information.Error code:0X80000001解决方法
    《一百岁感言》 杨绛
    取扑克牌的问题
    马云的懒人理论
    明代地图总目
  • 原文地址:https://www.cnblogs.com/xiaochaoqun/p/6844882.html
Copyright © 2011-2022 走看看