• HDU 1695 GCD


    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.

    题目大意:
    求出[1,b],[1,d]中gcd为k的数对的个数

    解题报告:
    比较简单,转化为求[1,b/k],[1,d/k]中互质的数对个数,就是莫比乌斯反演的板子题了.
    (F_i)表示gcd(a,b)i的数对个数 (G_i)表示gcd(a,b)%i0的数对个数,那么答案就是F[1]

    显然
    (G_n=(a/n)*(b/n))
    (F[1]=sum_{i=1}^{max(a,b)}mu_iG_i)

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    typedef long long ll;
    const int N=100005;
    int CNT=0,num=0,phi[N],prime[N];bool vis[N];
    void prework(){
    	phi[1]=1;
    	for(int i=2;i<N;i++){
    		if(!vis[i]){
    			prime[++num]=i;
    			phi[i]=-1;
    		}
    		int to;
    		for(int j=1;j<=num && prime[j]*i<N;j++){
    			to=i*prime[j];
    			vis[to]=true;
    			if(i%prime[j])
    				phi[to]=-phi[i];
    			else{
    				phi[to]=0;
    				break;
    			}
    		}
    	}
    }
    void work()
    {
    	int a,b,c,d,k;
    	scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
    	if(k==0){
    		printf("Case %d: 0
    ",CNT);
    		return ;
    	}
    	b/=k;d/=k;
    	if(b>d)swap(b,d);
    	ll ans=0,cnt=0;
    	for(int i=1;i<=b;i++){
    		ans+=(ll)phi[i]*(b/i)*(d/i);
    	}
    	for(int i=1;i<=b;i++){
    		cnt+=(ll)phi[i]*(b/i)*(b/i);
    		//减去重复部分
    	}
    	ans-=cnt>>1;
    	printf("Case %d: %lld
    ",CNT,ans);
    }
    
    int main()
    {
    	int T;cin>>T;
    	prework();
    	while(T--)
    		CNT++,work();
    	return 0;
    }
    
    
  • 相关阅读:
    Delphi 简体 繁体 转换
    简单地为DBNavigator填加Caption
    TEdit的 Clear 和 赋值 ''
    SSH服务端
    动态模块导入示例、断言
    异常处理
    反射、getattr
    类的各种自带方法,静态方法,属性方法,类方法等
    类的继承,深度优先于广度优先
    类变量与实例变量、析构函数、私有属性与私有方法
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7465095.html
Copyright © 2020-2023  润新知