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. iOS中3种正则表达式的使用

    1.利用NSPredicate(谓词)匹配 例如匹配有效邮箱: ? 1 2 3 4 NSString *email = @“nijino_saki@163.com”:  NSString *regex ...

  2. leetcode_Repeated DNA Sequences

    描写叙述: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ...

  3. MySQL服务无法启动(1067)问题

    关于这个问题网上的帖子和说法多如牛毛,是在难以分辨真假,或者是否与自己的出错情况相同. 有了前车之鉴,就有必要提前声明,这篇是我在计算机--管理--服务中启动mysql服务时出现的错误,如下: 最后的 ...

  4. Deep Learning 30: 卷积理解

    一.深度卷积神经网络学习笔记(一): 1. 这篇文章以贾清扬的ppt说明了卷积的实质,更说明了卷积输出图像大小应该为: 假设输入图像尺寸为W,卷积核尺寸为F,步幅(stride)为S(卷积核移动的步幅 ...

  5. YTU 2960: 改错题--小鼠标,你要干什吗?

    2960: 改错题--小鼠标,你要干什吗? 时间限制: 1 Sec  内存限制: 128 MB 提交: 118  解决: 62 题目描述 鼠标双击不同的图标产生不同的效果,比如双击文档(documen ...

  6. 区块链共识算法 PBFT(拜占庭容错)、PAXOS、RAFT简述

    共识算法 区块链中最重要的便是共识算法,比特币使用的是POS(Proof of Work,工作量证明),以太币使用的是POS(Proof of Stake,股权证明)使得算理便的不怎么重要了,而今PO ...

  7. python dns server开源列表 TODO

    基于dns lib的,https://github.com/andreif/dnslib 有:https://www.cnblogs.com/anpengapple/p/5664500.html ht ...

  8. bash编程 将一个目录里所有文件存为一个array 并分割为三等分——利用bash array切片

    files=(a b c d e f g h i j k l m n o p)cnt="${#files[@]}"let cnt1="($cnt+2)/3"le ...

  9. 淘淘商城项目_同步索引库问题分析 + ActiveMQ介绍/安装/使用 + ActiveMQ整合spring + 使用ActiveMQ实现添加商品后同步索引库_匠心笔记

    文章目录 1.同步索引库问题分析 2.ActiveM的介绍 2.1.什么是ActiveMQ 2.2.ActiveMQ的消息形式 3.ActiveMQ的安装 3.1.安装环境 3.2.安装步骤 4.Ac ...

  10. <hr />标签的颜色和粗细设定

    设置<hr />的颜色和粗细,不需要用到style,直接用标签的color和size属性: <hr color="red" size="0.5" ...