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();
	}
}


好的,关于 HDU4992 求所有原根的问题,我可以给出以下解答: 1. 首先,我们需要知道什么是原根。原根是指模 n 意义下,所有与 n 互质的数 a 的最小正整数 k 次幂对 n 取模的值覆盖了所有与 n 互质的数。简单来说,就是如果一个数 a 是模 n 意义下的原根,那么 a 的任何正整数次幂对 n 取模的值都不相同,且能覆盖所有与 n 互质的数。 2. 为了求模 n 意义下的所有原根,我们需要先求出与 n 互质的数的个数 phi(n)。phi(n) 可以使用欧拉函数求出。 3. 然后,我们需要枚举模 n 意义下的所有数,判断它是否是原根。具体来说,对于每个 a,我们需要判断 a 的每个小于 phi(n) 的正整数次幂对 n 取模的值是否都不相同,且能覆盖所有与 n 互质的数。如果是,那么 a 就是模 n 意义下的原根。 4. 代码实现可以参考以下 Java 代码: ``` import java.util.*; public class Main { static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int phi(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res = res / i * (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res = res / n * (n - 1); } return res; } static int pow(int a, int b, int mod) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } static boolean check(int a, int n, int phi) { for (int i = 1, j = pow(a, i, n); i <= phi; i++, j = j * a % n) { if (j == 1) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int phi = phi(n); List<Integer> ans = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1 && check(i, n, phi)) { ans.add(i); } } Collections.sort(ans); for (int x : ans) { System.out.print(x + " "); } System.out.println(); } } } ``` 其中,gcd 函数用于求最大公约数,phi 函数用于求欧拉函数,pow 函数用于快速幂求模,check 函数用于判断一个数是否是原根。在主函数中,我们依次读入每个 n,求出 phi(n),然后枚举模 n 意义下的所有数,判断它是否是原根,将所有原根存入一个 List 中,最后排序输出即可。 希望我的回答能够帮到你,如果你有任何问题,欢迎随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值