http://acm.hdu.edu.cn/showproblem.php?pid=1695

要求[L1, R1]和[L2, R2]中GCD是K的个数。那么只需要求[L1, R1 / K]  和 [L2, R2 / K]中GCD是1的对数。

由于(1, 2)和(2, 1)是同一对。

那么我们枚举大区间,限制数字一定是小于等于枚举的那个数字就行。

比如[1, 3]和[1, 5]

我们枚举大区间,[1, 5],在[1, 3]中找互质的时候,由于又需要要小于枚举数字,那么直接上phi

对于其他的,比如4这样,在[1, 3]中找有多少个数字和它互质。

这需要容斥。先求不互质的个数,因为有个明显的道理,12 = 2 * 2 * 3,那么2的倍数可以排除,3的倍数可以排除,但是排除多了一部分。

区间中与i不互质的个数 = (区间中i的每个质因数的倍数个数)-(区间中i的每两个质因数乘积的倍数)+(区间中i的每3个质因数的成绩的倍数个数)-(区间中i的每4个质因数的乘积)

这个容斥好想但是不好写。我的dfs写的很烂。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int prime[maxn];//这个记得用int,他保存的是质数,可以不用开maxn那么大
bool check[maxn];
int total;
bool is_prime[maxn];
int his[maxn][ + ];
int Size[maxn];
void initprime() {
for (int i = ; i <= maxn - ; i++) {
if (!check[i]) { //是质数了
prime[++total] = i; //只能这样记录,因为后面要用
is_prime[i] = true;
}
for (int j = ; j <= total; j++) { //质数或者合数都进行的
if (i * prime[j] > maxn - ) break;
check[i * prime[j]] = ;
if (i % prime[j] == ) break;
//关键,使得它只被最小的质数筛去。例如i等于6的时候。
//当时的质数只有2,3,5。6和2结合筛去了12,就break了
//18留下等9的时候,9*2=18筛去
}
}
// Size[1] = 1;
// his[1][1] = 1;
for (int i = ; i <= maxn - ; ++i) {
if (is_prime[i]) {
Size[i] = ;
his[i][] = i;
continue;
}
int t = i;
for (int j = ; j <= total; ++j) {
if (prime[j] > t) break;
if (t % prime[j] == ) {
his[i][++Size[i]] = prime[j];
t /= prime[j];
while (t % prime[j] == ) {
t /= prime[j];
}
}
}
}
return ;
}
int phi[maxn];
void init_phi() {
phi[] = ;
for (int i = ; i <= maxn - ; i++) {
if (!phi[i]) {
for (int j = i; j <= maxn - ; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - );
}
}
}
return ;
}
LL calc(int up, int cur, int number, int tobuild, int flag) {
LL ans = ;
for (int i = cur; i <= Size[number]; ++i) {
if (flag == ) {
ans += up / (his[number][i] * tobuild) + calc(up, i + , number, tobuild * his[number][i], !flag);
} else ans += -up / (his[number][i] * tobuild) + calc(up, i + , number, tobuild * his[number][i], !flag);
}
return ans;
}
int f;
void work() {
int L1, R1, L2, R2, k;
scanf("%d%d%d%d%d", &L1, &R1, &L2, &R2, &k);
if (k == ) {
printf("Case %d: 0\n", ++f);
return;
}
R1 /= k;
R2 /= k;
if (R1 < R2) swap(R1, R2);
LL ans = ;
for (int i = ; i <= R2; ++i) {
ans += phi[i];
}
for (int i = R2 + ; i <= R1; ++i) {
ans += R2 - calc(R2, , i, , );
}
printf("Case %d: %I64d\n", ++f, ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
initprime();
init_phi();
// cout << 24 - calc(24, 1, 12, 1, 0) << endl;
// cout << 12 - calc(12, 1, 12, 1, 0) << endl;
// for (int i = 1; i <= Size[10]; ++i) {
// printf("%d ", his[10][i]);
// }
int t;
scanf("%d", &t);
while (t--) {
work();
}
return ;
}

hdu 1695 GCD 欧拉函数 + 容斥的更多相关文章

  1. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. HDU 1695 GCD 欧拉函数+容斥定理

    输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...

  3. HDU 1695 GCD (欧拉函数,容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. hdu 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

  6. 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( ...

  7. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  8. hdu1695(莫比乌斯)或欧拉函数+容斥

    题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...

  9. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

随机推荐

  1. 【 D3.js 进阶系列 — 1.0 】 CSV 表格文件的读取

    在入门系列的教程中.我们经常使用 d3.json() 函数来读取 json 格式的文件.json 格式非常强大.但对于普通用户可能不太适合,普通用户更喜欢的是用 Microsoft Excel 或 O ...

  2. Failed to load resource: the server responded with a status of 404 (Not Found)

    Failed to load resource: the server responded with a status of 404 (Not Found) 报错情况:图标加载失败 原因分析:路径错误 ...

  3. crm使用soap删除下拉框

    //C# 代码: //DeleteOptionSetRequest request = new DeleteOptionSetRequest(); //request.Name = "new ...

  4. 每天复习Shell—ls

    ls命令是linux下最经常使用的命令.ls命令就是list的缩写缺省下ls用来打印出当前文件夹的清单假设ls指定其它文件夹那么就会显示指定文件夹里的文件及文件夹清单. 通过ls 命令不仅能够查 ...

  5. 《C++ Primer Plus》学习笔记3

    <C++ Primer Plus>学习笔记3 第8章 函数探幽 ============================================================== ...

  6. HTML的高富帅

    1,前端的内容(组成部分有以下三部分) HTML                         CSS                                JS 裸体的人 穿上好看的衣服 ...

  7. 不等式数列 DP

    度度熊最近对全排列特别感兴趣,对于1到n的一个排列,度度熊发现可以在中间根据大小关系插入合适的大于和小于符号(即 '>' 和 '<' )使其成为一个合法的不等式数列.但是现在度度熊手中只有 ...

  8. Java 解析excel2003和2007区别和兼容性问题(POI操作)

    最近在使用POI对excel操作中发现一些问题,2003和2007的区别还是蛮大的: 2007相关的包: poi-3.9.jar poi-examples-3.8.jar poi-excelant-3 ...

  9. 更改android studio AVD 位置

  10. ISA总线

    ISA总线: (Industry Standard Architecture:工业标准体系结构)是为PC/AT电脑而制定的总线标准,为16位体系结构,只能支持16位的I/O设备,数据传输率大约是16M ...