HDU 1695 GCD (容斥 + 莫比烏斯反演)
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7450 Accepted Submission(s): 2731
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.
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.
For each test case, print the number of choices. Use the format in the example.
2
1 3 1 5 1
1 11014 1 14409 9
Case 1: 9
Case 2: 736427
Hint
For 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).2008 “Sunline Cup” National
Invitational Contest
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1695
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1695
題目分析:HDU的資料太水了,這題用分塊求和優化和不做優化跑出來都是15ms,類似BZOJ 2301,比那個簡單,不過比那個坑多了,不懂這個k為什麼要從0開始。。。特判答案為0兩種情況,一個是k=0,另一個是max(b, d) < k,把區間化成(1, b/k),(1, d/k),就是求兩個區間各取出一個數使它們gcd為1的個數,剩下來的用莫比烏斯反演求就行了,然後就是這題的(a,b)和(b,a)算一種,因此要去重,方法很簡單,比如第一個樣例,區間1 5包含了1 3,因此要減去重複的部分,重複的部分就是cal(3, 3) / 2了,最後要用long long,算了下,極限資料答案是3e9左右,就超這麼一點點,還真看不出來。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
int mob[MAX], p[MAX], sum[MAX];
bool prime[MAX];
void Mobius()
{
int pnum = 0;
memset(sum, 0, sizeof(sum));
memset(prime, true, sizeof(prime));
mob[1] = 1;
sum[1] = 1;
for(int i = 2; i < MAX; i++)
{
if(prime[i])
{
p[pnum ++] = i;
mob[i] = -1;
}
for(int j = 0; j < pnum && i * p[j] < MAX; j++)
{
prime[i * p[j]] = false;
if(i % p[j] == 0)
{
mob[i * p[j]] = 0;
break;
}
mob[i * p[j]] = -mob[i];
}
sum[i] = sum[i - 1] + mob[i];
}
}
ll cal(int l, int r)
{
if(l > r)
swap(l, r);
ll ans = 0;
for(int i = 1, last = 0; i <= l; i = last + 1)
{
last = min(l / (l / i), r / (r / i));
ans += (ll) (l / i) * (r / i) * (sum[last] - sum[i - 1]);
}
return ans;
}
int main()
{
Mobius();
int T;
scanf("%d", &T);
for(int ca = 1; ca <= T; ca++)
{
int a, b, c, d, k;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
if(k == 0 || max(b, d) < k)
{
printf("Case %d: 0\n", ca);
continue;
}
int mi = min(b / k, d / k);
printf("Case %d: %I64d\n", ca, cal(b / k, d / k) - cal(mi, mi) / 2);
}
}
相關文章
- ZOJ 3868 GCD Expectation (容斥+莫比烏斯反演)GC
- HDU 1695-GCD(容斥原理+尤拉函式)GC函式
- HDU 5212 Code (容斥 莫比烏斯反演基礎題)
- hdu 1695 GCDGC
- POJ 3904 Sky Code (容斥+莫比烏斯反演)
- Hackerrank GCD Product(莫比烏斯反演)GC
- Codeforces 548E Mike and Foam (容斥+莫比烏斯反演)
- lg容斥與反演
- HDU 4746 Mophues (莫比烏斯反演應用)
- BZOJ 2818 Gcd (莫比烏斯反演 或 尤拉函式)GC函式
- 洛谷 P2257 YY的GCD(莫比烏斯反演)GC
- 莫比烏斯反演
- HDU 4059 The Boss on Mars ( 容斥原理)
- HDU4390Number Sequence(容斥原理)
- HDU4407Sum ( 容斥原理)
- SPOJ PGCD - Primes in GCD Table (好題! 莫比烏斯反演+分塊求和優化)GC優化
- HDU 5468 Puzzled Elena(DFS序+容斥原理)
- BZOJ 2301 [HAOI2011]Problem b (容斥+莫比烏斯反演+分塊優化 詳解)優化
- FZU 1969 && UVA 11426 GCD Extreme (尤拉函式 或 莫比烏斯反演)GCREM函式
- HDU2841 Visible Trees (容斥原理)
- 莫比烏斯反演學習筆記筆記
- 比較典的莫比烏斯反演
- 有標號DAG計數 [容斥原理 子集反演 組合數學 fft]FFT
- 容斥
- HDU 4135 Co-prime(容斥原理+分解質因數)
- 狄利克雷卷積 & 莫比烏斯反演卷積
- 反射容斥反射
- 容斥原理
- 狄利克雷卷積與莫比烏斯反演卷積
- HDU 2504 又見GCDGC
- HDU 4135——Co-prime(容斥原理&&二進位制列舉)
- 演算法隨筆——數論之莫比烏斯反演演算法
- 【模板】容斥原理
- HDU 5072 Coprime (單色三角形問題+容斥原理)
- 容斥原理講解
- Min-Max 容斥
- HDU 5726-GCD(暴力+map)GC
- HDU2588GCD(尤拉函式)GC函式