hdu1695 GCD

15 篇文章 0 订阅
8 篇文章 0 订阅

GCD

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


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
 

Recommend

wangye   |   We have carefully selected several similar problems for you:  1689 1690 1693 1691 1692 


题解:

点击打开链接

分析:题目是要求a<=x<=b,c<=y<=d(其中a,c题目中已经说了必为1),中有多少对(x,y)(无序的)满足GCD(x,y)=k,那么可以转化成求(x,y)分别在区间1<=x<=b/k,1<=y<=d/k中有多少对满足GCD(x,y)=1; 转换b/=k,d/=k,之后,假设b<=d,那么我们可以分两步来求值:

1、求[1,b]与[1,b]之间有多少对数互质,显然就是phi[1]+phi[2]+...phi[b]即可,用线性求欧拉函数的算法即可!

2\求[1,b]与[b+1,d]之间有多少对数互质,可以先求出有多少对数不互质,然后减去即可! 针对于区间[b+1,d]中得某一个数n求区间[1,b]中有多少个数与他不互质(即有公约数)那么可以用容斥原理较好的解决!

两步求值之和即为题意所求!



代码:

#include<bits/stdc++.h>
using namespace std;
long long ph[200001],a[1001],g[200001];
void oula(){
    long long i,j;
    for(i=1;i<=100001;i++)ph[i]=i;
    for(i=2;i<=100001;i++){
        if(!g[i]){
            ph[i]=i-1;
            for(j=i*2;j<=100001;j+=i){
                g[j]=1;
                ph[j]=ph[j]/i*(i-1);
            } 
        }
    }
}
long long rc(int n,int m){
    long long i,tot=0,j,flag;
    long long sum=0,t;
    for(i=2;i*i<=n;i++){
        if(!(n%i)){
            tot++;
            a[tot]=i;
            while(!(n%i))n/=i;
        }
    }
    if(n>1)a[++tot]=n;
    for(i=1;i<(1<<tot);i++){
        t=1;flag=0;
        for(j=0;j<tot;j++)
         if(i&(1<<j)){
             flag=1-flag;
             t*=a[j+1];
         }
        if(flag==1)sum+=m/t;
         else sum-=m/t; 
    }
    //printf("%lld %d\n",sum,flag); 
    return sum;
}
int main(){
    long long t1,ii,a22,b22,n,m,d,i;
    long long ans,sum;
    oula();
    scanf("%lld",&t1);
    for(ii=1;ii<=t1;ii++){
        scanf("%lld %lld %lld %lld %lld",&a22,&n,&b22,&m,&d);
        if(!d){
            printf("Case %lld: 0\n",ii);
            continue;
        }
        n/=d;m/=d;
        if(n>m)swap(n,m);
        ans=0;sum=n*(m-n);
        for(i=1;i<=n;i++)ans+=ph[i];
        for(i=n+1;i<=m;i++)sum-=rc(i,n);
        printf("Case %lld: %lld\n",ii,ans+sum);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值