zoukankan      html  css  js  c++  java
  • HDU 1695(GCD)

                                                                                                                                               GCD
    Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

    []   [Go Back]   [Status]  

    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

    2008 “Sunline Cup” National Invitational Contest
     
     
     
    【思路分析】
    本题考察如何处理数据的问题。由于我们要求一对数字x,y使得两者的最大公约数为k。那么我们首先可以让b,d同时除以k然后在[0,b/k]和[0,d/k]中寻找互质数字的个数。我们首先处理b和d的关系,如果b>d,那么我们把b和d的值进行交换。下面看图:
     
    对于容斥原理而言,我们先看一个简单的例子:我们要求在1-210之间与210不互素的数字个数。
    那么个数为:
     
    下面我们便可以运用容斥原理(加加减减)解决问题了。
    但是容斥原理如何写呢?我们可以运用一个神奇的递归写法:
     
    下面给出AC代码:
    #include<iostream>
    using namespace std;
    #define max  100005
    long  long uler[max];
    int num[max];
    int p[max][30];
    void init()
    {
        uler[1]=1;
        for(int i=2;i<max;i++)
        {
            
        if(!uler[i])
        {
            for(int j=i;j<max;j+=i)
            {
                if(!uler[j]) uler[j]=j;
                uler[j]=uler[j]*(i-1)/i;
                p[j][num[j]++]=i;
            }
            
        }
        uler[i]+=uler[i-1];
       }
    }
    int dfs(int k,int b,int now)
    {
        int ans=0;
        for(int i=k;i<num[now];i++)
        ans+=b/p[now][i]-dfs(i+1,b/p[now][i],now);
        return ans;
    }
    int main()
    {
        int cas,cas1=1;
        init();
        cin>>cas;
        while(cas--)
        {
            int a,b,c,d,k;
           scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
            if(k==0) {cout<<"Case "<<cas1++<<": "<<0<<endl;continue;}
            if(b>d) swap(b,d);
            int m=b/k,n=d/k;
               long long ans=0;
            ans+=uler[m];       
            for(int i=m+1;i<=n;i++)
            ans+=m-dfs(0,m,i);
            cout<<"Case "<<cas1++<<": "<<ans<<endl;  
        }
        return 0;
    }
  • 相关阅读:
    策略模式
    Properties类学习笔记
    System类学习笔记
    一个反射的妙用案例
    new 对象时的暗执行顺序
    常用数据库默认端口号
    java对日开发常用语(词汇)总结
    java开发中常用语(词汇)含义
    MyBatis 常用词汇含义
    java SE,EE,ME区别
  • 原文地址:https://www.cnblogs.com/khbcsu/p/3911733.html
Copyright © 2011-2022 走看看