zoukankan      html  css  js  c++  java
  • hdu 1695 欧拉函数+容斥原理

    GCD

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

    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).

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int T;
    int a,b,c,d;
    int k;
    vector<int> v[100050];
    void factor(int n)
    {
        int temp,i;int now;
        temp=(int)((double)sqrt(n)+1);
        now=n;
        for(i=2;i<=temp;++i)
        if(now%i==0){
                v[n].push_back(i);
            while(now%i==0)
            {
                now/=i;
            }
        }
        if(now!=1){
            v[n].push_back(now);
        }
    }
    ll euler[100005];
    ll sumeuler[100005];
    void euler_phi2()
    {
        for(int i=0;i<100005;i++)euler[i]=i;
        for(int i=2;i<100005;i++)
        {
            if(euler[i]==i){
                for(int j=i;j<100005;j+=i)euler[j]=euler[j]/i*(i-1);
            }
        }
        sumeuler[1]=1;
        for(int i=2;i<100005;i++)
            sumeuler[i] = sumeuler[i-1]+euler[i];
    }
    void init()
    {
        euler_phi2();
        for(int i=1;i<=100000;i++)factor(i);
    }
    int id=1;
    int main()
    {
        #ifndef ONLINE_JUDGE
            freopen("in.txt","r",stdin);
        #endif // ONLINE_JUDGE
        init();
        scanf("%d",&T);
        int x,y;
        while(T--)
        {
            scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
            if(k==0||k>b||k>d){cout<<"Case "<<id++<<": "<<0<<endl;continue;}
            x=b/k;//区间一右端点
            y=d/k;//区间二右端点
            if(x>y) swap(x,y);
            ll ans=sumeuler[x];
            ll S=0;
            for(int i=x+1;i<=y;i++)
            {
                int num=v[i].size();
                for(int j=1;j<(1<<num);j++)
                {
                    ll fac=1;int cnt=0;
                    for(int k=0;k<num;k++)
                    {
                        if(j&(1<<k)){cnt++;fac*=v[i][k]; }
                    }
                    if(cnt&1)S+=x/fac;else S-=x/fac;
                }
            }
            S=1ll*x*(y-x)-S;
            ans+=S;
            cout<<"Case "<<id++<<": "<<ans<<endl;
        }
    }
    
    
    
  • 相关阅读:
    request相关
    C#请求接口
    qml_base
    web
    entry
    listbox
    Canvas
    pickle
    c#枚举
    数据结构——树
  • 原文地址:https://www.cnblogs.com/linruier/p/9485151.html
Copyright © 2011-2022 走看看