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
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    【CSS3】响应式布局
    【jQuery插件】pagepiling滚屏插件使用
    【README.md】Markdown语言常用语法
    【页面架构】水平居中+垂直居中
    【页面架构】垂直居中
    【页面架构】水平居中
    【转载】css3动画简介以及动画库animate.css的使用
    【前端学习笔记】登录验证案例
    bzoj 3569 DZY Loves Chinese II 随机算法 树上倍增
    bzoj 1018 堵塞的交通traffic 线段树
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10324294.html
Copyright © 2011-2022 走看看