HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
来源:互联网 发布:淘宝网折叠沙发床单人 编辑:程序博客网 时间:2024/05/21 09:42
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.
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.
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
21 3 1 5 11 11014 1 14409 9
Sample Output
Case 1: 9Case 2: 736427HintFor 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
Recommend
wangye
题目大意:
在[1,b]和[1,d]区间内分别取一个数,使得它们的最小公倍数为k。求出方案数(两数交换是同一种方案)
解题思路:
GCD(x, y) = k 等价于 GCD(x/k, y/k) = 1。原问题等价于,在[1,b/k]和[1,d/k]区间内分别取一个数,使得互质。
分两段讨论这个问题:
假设d/k < b/k
1. 在[1, d/k]区间内取第一个数x,第二个数y也在[1, d/k]内取。去除两数交换的重复情况,可以直接使用欧拉函数,求出[1, x]中与x互质的个数。累加即可。
2. 在[d/k+1, b/k]区间内取第一个数x,第二个数y在[1, d/k]内取。利用容斥原理,与x互质的个数 = y总个数 - 与x不互质的个数 = d/k - 含有x任意一个质因数的y个数 +含有x任意两个质因数的y个数 - ... +
参考代码:
#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int MAXN = 100010;int minDiv[MAXN], phi[MAXN], sum[MAXN], factorA[MAXN], factorB[MAXN], totFactor, prime[MAXN], totPrime, nCase, a, b, c, d, k, cnt;long long ans;bool isPrime[MAXN];void getPrime(int n) { memset(isPrime, true, sizeof(isPrime)); totPrime = 0; for (int i = 2; i <= n; i++) { if (isPrime[i]) { prime[++totPrime] = i; } for (int j = 1; j <= totPrime && i*prime[j] <= n; j++) { isPrime[i*prime[j]] = false; if (i % prime[j] == 0) break; } }}void getFactor(int n) { totFactor = 0; int now = n; for (int i = 1; i <= totPrime && prime[i]*prime[i] <= now; i++) { if (now % prime[i] == 0) { factorA[++totFactor] = prime[i]; factorB[totFactor] = 0; while (now % prime[i] == 0) { factorB[totFactor]++; now /= prime[i]; } } } if (now != 1) { factorA[++totFactor] = now; factorB[totFactor] = 1; }}void genPhi() { for (int i = 1; i < MAXN; i++) { minDiv[i] = i; } for (int i = 2; i*i < MAXN; i++) { if (minDiv[i] == i) { for (int j = i*i; j < MAXN; j += i) { minDiv[j] = i; } } } phi[1] = 1; for (int i = 2; i < MAXN; i++) { phi[i] = phi[i / minDiv[i]]; if ((i / minDiv[i]) % minDiv[i] == 0) { phi[i] *= minDiv[i]; } else { phi[i] *= minDiv[i] - 1; } }}void init() { ans = 0;}void solve() { cin >> a >> b >> c >> d >> k; a = c = 1; if (k == 0 || k > b || k > d) { ans = 0; return; } b /= k; d /= k; if (b < d) swap(b, d); for (int i = 1; i <= d; i++) { ans += phi[i]; } for (int i = d+1; i <= b; i++) { ans += d; getFactor(i); for (int j = 1; j < (1 << totFactor); j++) { int cnt = 0, x = 1; for (int k = 1; k <= totFactor; k++) { if (j & (1 << (k-1))) { cnt++; x = x * factorA[k]; } } if (cnt & 1) { ans -= d / x; } else { ans += d / x; } } }}void output() { cout << "Case " << ++cnt << ": " << ans << endl;}int main() { ios::sync_with_stdio(false); getPrime(400); genPhi(); cin >> nCase; while (nCase--) { init(); solve(); output(); } return 0;}
0 0
- HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
- HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)
- hdu-1695 GCD(容斥原理+欧拉函数)
- hdu 1695 GCD (欧拉函数、容斥原理)
- HDU 1695 GCD(欧拉函数+容斥原理)
- HDU 1695 GCD(欧拉函数+容斥原理)
- HDU 1695 GCD (欧拉函数+容斥原理)
- HDU 1695 GCD(容斥原理 + 欧拉函数)
- 【HDU】1695 - GCD(欧拉函数 & 容斥原理)
- HDU 1695-GCD(容斥原理+欧拉函数)
- HDU - 1695 GCD(欧拉函数+容斥原理)
- HDU/HDOJ 1695 GCD 欧拉函数+容斥原理
- HDU 1695 GCD 欧拉函数+容斥原理
- hdu 1695 GCD 欧拉函数+容斥原理
- HDU 1695 GCD(欧拉函数+容斥原理)
- hdu 1695 GCD(容斥原理+欧拉函数)
- hdu 1695 GCD(欧拉函数+容斥原理)
- 【HDU】1695 GCD 欧拉函数+容斥原理
- APC Silcon 60kv - What do all the abbreviations mean
- [Phonegap+Sencha Touch] 移动开发7、Sencha Touch图标的使用与自定义
- 颜色特征描述子的SIFT/SURF
- ZOJ Monthly, June 2014 F - First Digit
- ASP.NET MVC学习---(六)CRUD例子补充
- HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
- AUX/奥克斯 T8300 root教程_方法
- CentOS 6.5 安装 Xen 4.1.2
- [Phonegap+Sencha Touch] 移动开发8、store和proxy
- 某游戏公司面试全过程
- LeetCode-Path Sum II
- [Phonegap+Sencha Touch] 移动开发4、Sencha Touch初步使用
- 安装滚轮插件
- poj 3280 简单dp