zoukankan      html  css  js  c++  java
  • GCD HDU

    GCD

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


    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).
     
    Source
     
    就是求 [1, b / k]  和 [1, d / k] 互质数的对数
     先用欧拉函数求出 相同区间的互质的对数 多出来的 用容斥去求
    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 110000, INF = 0x7fffffff;
    int ans;
    LL tot[maxn + 10];
    int prime[maxn+10], phi[maxn+10];
    bool vis[maxn+10];
    void getphi()
    {
        ans = 0;
        phi[1] = 1;
        for(int i=2; i<=maxn; i++)
        {
            if(!vis[i])
            {
                prime[++ans] = i;
                phi[i] = i - 1;
            }
            for(int j=1; j<=ans; j++)
            {
                if(i * prime[j] > maxn) break;
                vis[i * prime[j]] = 1;
                if(i % prime[j] == 0)
                {
    
                    phi[i * prime[j]] = phi[i] * prime[j]; break;
                }
                else
                    phi[i * prime[j]] = phi[i] * (prime[j] - 1);
            }
        }
    }
    
    int get_cnt(int n, int m)
    {
        int ans = 0;
        for(int i = 2; i * i <= n; i++)
        {
            if(n % i) continue;
            while(n % i == 0) n /= i;
            prime[ans++] = i;
        }
        if(n != 1) prime[ans++] = n;
        int res = 0;
        for(int i = 1; i < (1 << ans); i++)
        {
            int tmp = 1, cnt2 = 0;
            for(int j = 0; j < ans; j++)
            {
                if(((i >> j) & 1) == 0) continue;
                tmp *= prime[j];
                cnt2++;
            }
            if(cnt2 & 1) res += m / tmp;
            else res -= m / tmp;
        }
        return m - res;
    }
    
    int main()
    {
        getphi();
        int a, b, c, d, k;
        for(int i = 1; i < maxn; i++)
        {
            tot[i] = tot[i - 1] + phi[i];
    
        }
        int T, kase = 0;
        cin >> T;
        while(T--)
        {
            scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
            if(k == 0)
            {
                printf("Case %d: 0
    ", ++kase);
                continue;
            }
            int n = b / k, m = d / k;
            LL sum = tot[n > m ? m : n];
           // cout << sum << endl;
            if(m > n) swap(n, m);
            for(int i =m + 1; i <= n; i++)
            {
                sum += get_cnt(i, m);
            }
            printf("Case %d: %lld
    ", ++kase, sum);
        }
    
        return 0;
    }

    GCD

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


    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).
     
    Source
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    关于自动分裂的思考 | Solrex 杨文博的博客,记录我的生活、技术、思想和梦想
    在STL中,map按值来排序的实现方法_永不言弃是生命的基调!_百度空间
    C/C++学习路线(教材推荐)_Hello World!_百度空间
    Google C++ Style中允许使用的Boost库(1) 程序即人生 博客频道 CSDN.NET
    STL中map按值(value)排序
    程序即人生 » 移动平台现在可用的C++ 11特性
    开发者应该了解的 12 款 Eclipse 插件 编程语言 ITeye资讯
    Lisp的给力特性(V.S. Python3) 第二篇 程序即人生 博客频道 CSDN.NET
    Solidot | 地球上有多少Java程序员?
    在STL中,map按值来排序的实现方法
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10324294.html
Copyright © 2011-2022 走看看