杭电1695 GCD(容斥原理三题)

GCD

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


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
2008 “Sunline Cup” National Invitational Contest 
/*
校赛的时候都犯了一次低级错误,没用总个数减去他的倍数,没搞出来,,,今天又是这种低级错误。。。。。。几个小时、、、、我到底怎么回事呢???我就纳闷了。。。。是好运气将要来临的标志吗??嘿嘿,还得加油啊!!
Time:2014-12-24 16:46
*/
//题中假设所有测试数据中a=c=1
//相当于求1--b/k中与1--d/k中互质的个数,并且2---3与3---2算一种,用欧拉函数求和求出来,比b小的那一部分,然后再用容斥原理求b+1--d的那一部分就不用考虑重复了
//f=x(1-1/p1)(1-1/p2)
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const int MAX= 100000+10;
vector<int>prime[MAX];
bool vis[MAX];
LL eular[MAX];
void Prime(){
    memset(eular,0,sizeof(eular));
        eular[1]=1;
    for(int i=0;i<MAX;i++)if(prime[i].size())prime[i].clear();
    for(int i=2;i<MAX;i++){
        if(eular[i]==0){
            for(int j=i;j<MAX;j+=i){
                if(eular[j]==0)eular[j]=j;
                eular[j]=eular[j]*(i-1)/i;
                prime[j].push_back(i);
            }
        }
        eular[i]+=eular[i-1];
    }
}
LL DFS(int pos,int b,int n){
    //pos表示素数位置,b表示1--b的个数,与n互质
    LL ret=0;
    int k=prime[n].size();
    for(int i=pos;i<k;i++){
        int temp=b/prime[n][i];
        //这儿求出与第一个质因子互质的个数
        //再除以下一个时,就是与第一个和第二个都互质的个数
        ret+=temp-DFS(i+1,temp,n);//容斥原理
    }
    return ret;
}
int main(){
    int T;
    Prime();
    int a,b,c,d,k;
    int nCase=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        printf("Case %d: ",nCase++);
        if(k==0){
            printf("0\n");
            continue;
        }
        b=b/k;d=d/k;
        if(b>d){swap(b,d);}
        LL ans=eular[b];
        /*
        for(int i=1;i<=10;i++){
            printf("%d ",eular[i]);
        }
        printf("ans=%lld\n",ans);
        */
        for(int i=b+1;i<=d;i++){
            ans+=b-DFS(0,b,i);
        }
        printf("%lld\n",ans);
    }
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值