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





若在 hdu oj 要将 long long 改为 __int64(对应输出格式也要改下),hdu oj 不支持 long long(其实只要输入改一下就可以,前面还是可以兼容的)

题意:已知给定k,x,y求 1<=a<=x 1<=b<=y 中满足 gcd(a,b)=k 的(a,b)对数。(注意数对是无序的)。 1<=x,y<=1e5, 0<=k<=1e5

用到了欧拉函数,素因子分解,筛选法,组合数学上的容斥原理等,不失为一道好题!!!

 

大体思路:

有一个小小的变形:在[1...b/k]中选x,在[1....d/k]中选y,使gcd(x,y)=k,求不重复的对数

我们让d>=b;  然后在[1....d/k]进行枚举,对于每一个i,我们只要在1...min(i-1,b)中找到与i互质数,记录个数,然后累加就得到结果了

当i<=b/k时,我们可以直接用欧拉函数计算出与i互质的个数 (当然要先进行因子分解,才能求欧拉函数)

当b/k<i<=d/k时,就比较难求了,我们用b/k减去与i不互质的数的个数得到,求与i不互质的数的个数时就用到容斥原理,设i的素因子分别的p1,p2...pk,则1..b/k中p1的倍数组成集合A1,p2的倍数组成集合A2,p3到A3.....pk到Ak, 由于集合中会出现重复的元素, 所以用容斥原理来求A1并A2并A3.....并Ak的元素的数的个数.

容斥原理的具体如下:

如果i因子个数num[i]为0,即i为素数,则区间中与i不互质的个数是0

否则,区间中与i不互质的个数 = (区间中i的每个质因数的倍数个数)-(区间中i的每两个质因数乘积的倍数)+(区间中i的每3个质因数的乘积的倍数个数)-(区间中i的每4个质因数的乘积)+...

    于是问题变成了统计每个数的不同质因数的个数而忽略次数。这个可以用筛法。具体做法如下:

    对每个数保存一个真质因数的列表。初始每个列表的长度为0。然后从2开始,分别检查每个数的列表长度,如果列表长度不为0,则这个数是合数,跳过;如果这个长度为0,则我们找到了一个质数,同时再把这个数的倍数(不包含本身)的列表里加入这个数。




代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100000+5;
__int64 phi[maxn];//或者long long phi[maxn];
int p[maxn][20],num[maxn];//num[maxn]存放数的素因子个数;p[maxn][20]筛选法得到数的素因子及每个数的欧拉函数值//存放数的素因子
void init()//筛选法得到数的素因子及每个数的欧拉函数值
{
    int i,j;
    phi[1]=1;
    for(i=2;i<maxn;i++)
    {
        if(!phi[i])
            for(j=i;j<maxn;j+=i)
            {
                if(!phi[j])
                phi[j]=j;
                phi[j]=phi[j]*(i-1)/i;
                p[j][num[j]++]=i;
            }
       phi[i]+=phi[i-1];
    }
}


int dfs(int index,int b,int now)//求不大于b的数中,与now不互质的数的个数;dfs()写的容斥原理(惊呆了!!!好好体会)
{
   int ans=0,i;
   for(i=index;i<num[now];i++)//容斥原理来求A1并A2并A3.....并Ak的元素的数的个数.
   ans+=b/p[now][i]-dfs(i+1,b/p[now][i],now);
   return ans;
}


int main()
{
    int a,b,c,d,t,i,j,k,kase;
    scanf("%d",&t);
    init();
    for(kase=1;kase<=t;kase++)
    {
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        printf("Case %d: ",kase);
        if(k==0)  {printf("0\n");continue;}
        //b/=k,d/=k;
        if(b>d) swap(b,d);
        b/=k;d/=k;
        long long ans=phi[b];
        for(i=b+1;i<=d;i++)
        {
            ans+=b-dfs(0,b,i);//求不大于b的数中,与i不互质的数的个数
        }
        printf("%I64d\n",ans);//不能写lld
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值