HDU 1695 GCD 容斥+约数枚举

本文介绍了一种解决特定GCD问题的有效算法。该问题要求找出所有满足GCD(x,y)=k的整数对(x,y),其中x和y分别位于两个指定区间内。通过将问题转化为寻找互质数对,利用预处理技术和容斥原理来计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GCD

 
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


        
  

思路: gcd(m,n) = k 即 gcd(m/k, n/k) = 1

         问题变成{gcd(s,t)=1| s∈(1,b/k)且t∈(1,d/k)}

         可以转化为 枚举每一个s∈(1,b/k) 计算 1~d/k中有多少与其互质的数. 互质数的个数不好求, 但不互质的数可以枚举质因数容斥来计算.

        由于Case较多, 所以对1e5以下的每个数的质因数进行预处理.

代码如下:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<iomanip>
#include<stdlib.h>
#include<cstdio>
#include<string>
#include<string.h>
#include<set>
#include<map>
using namespace std;

typedef long long ll;
typedef  pair<int,int> P;
const int INF = 0x7fffffff;
const int MAX_N = 1e5+5;
const int MAX_V = 0;
const int MAX_M = 0;
const int MAX_Q = 0;
const int M = 100000;

void show(string a, int val){
	cout<<a<<":       "<<val<<endl;
}

//Define array zone
int a, b,c ,d, k;
bool used[MAX_N]; 
vector<int> G[MAX_N];
int p = 0, x;
int prime[MAX_N];

//Cut-off rule

ll gcd(ll a, ll b){
	if(b==0) return a;
	return gcd(b, a%b);
}

void init(){
	// seek prime factor
	for(int i=2; i<=M; i++){
		if(!used[i]){
			prime[p++] = i;
			for(int j=2; i*j<=M; j++){
				used[i*j] = true;
			}
		}
	}
	prime[p] = INF;
	// find the prime factor of intergers not larger than M
	for(int i=2; i<=M; i++){
		int tmp = i;
		for(int j=0; prime[j]<=tmp&&tmp!=1; j++){
			int pr = prime[j];
			if(tmp%pr==0) G[i].push_back(pr);
			while(tmp%pr==0) tmp/=pr;
		}
	}
}

ll calc(int t){
	int k = G[t].size();
	ll res = 0;
	for(int i=1; i<1<<k; i++){
		int num = 0;
		for(int j=i; j!=0; j>>=1) num += j&1;
		ll lcm = 1;
		for(int j=0; j<k; j++){
			if(i>>j&1) lcm = lcm/ gcd(lcm, G[t][j]) *G[t][j];
			if(lcm>d) break;
		}
		int sym = num&1? 1: -1;
		res += sym * (d-t) /lcm;
	}
	return res;
}

void solve(){
	ll res = 0;
	if(k!=0){
		b /= k; d /= k;
		if(b>d) swap(b,d);
		if(b>0)
			res += d;
		for(int i=2; i<=b; i++){
			res += (d - i) - calc(i);
		}
	}
	cout<<"Case "<<++x<<": "<<res<<endl;
}

int main(){
	init();
	int T; scanf("%d",&T);
	while(T--){
		scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);	
		solve();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值