zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    android图片特效处理之光晕效果
    Android 给图片加边框
    Android学习笔记进阶十三获得本地全部照片
    startActivityForResult()的用法
    Android学习笔记进阶十二之裁截图片
    Android学习笔记进阶十一图片动画播放(AnimationDrawable)
    Android控件开发之Gallery3D效果
    android中图片倒影、圆角效果重绘
    两种方法求最大公约数最小公倍数
    Nginx 负载均衡配置和策略
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7465095.html
Copyright © 2011-2022 走看看