hdu 1695 GCD 欧拉函数 + 容斥
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 欧拉函数 + 容斥的更多相关文章
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 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 和 ...
- HDU 1695 GCD (欧拉函数,容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...
- 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( ...
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
- hdu1695(莫比乌斯)或欧拉函数+容斥
题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
随机推荐
- MySQL Study之--MySQL用户及权限管理
MySQL Study之--MySQL用户及权限管理 MySQLserver通过MySQL权限表来控制用户对数据库的訪问.MySQL权限表存放在mysql数据库里.由mysql_install ...
- pycharm快捷键和一些常用的设置
http://blog.csdn.net/pipisorry/article/details/39909057 在PyCharm /opt/pycharm-3.4.1/help目录下可以找到Refer ...
- POI 的使用
POI 使用 一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能..NET的开发人员则 ...
- Android 性能优化探究
使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...
- showModalDialog参数问题
showModalDialog传递参数: 1.参数拼接放在url中,参数过长或带特殊字符时,容易出现问题. 2.参数放在showModalDialog属性里 <script type=" ...
- 嵌入式开发之davinci--- 8148/8168/8127 中的图像缩放sclr、swms之后出现图像视频卡顿、屏幕跳跃的问题
()问题原因 这边的case链路是这样的camera->sclr(yuv420sp cif)->dup->ipcframeoutm3<->ipcframerocess&l ...
- PL/SQL Developer导入导出Oracle数据库方法
前一篇博客介绍了Navicat工具备份Oracle的方法.这篇博客介绍一下使用PL/SQL Developer工具导入导出Oracle数据库的方法. PL/SQL Developer是Oracle数据 ...
- Django模板语言(二)
1,装饰器:在不改变原函数的调用方式情况下为原函数增加一些功能(遵循开放封闭的原则) def outter(fn): def inner(*args, **kwargs): # 可以在执行函数前执行一 ...
- (16)ServletContext详解
1,作用: ServletContext对象 ,叫做Servlet的上下文对象.表示一个当前的web应用环境.一个web应用中只有一 ...
- Enum的基本使用
package enum_test; public enum Shrubbery { GROUND, CRAWLING, HANGING } package enum_test; public cla ...