HDU 4135 容斥
问a,b区间内与n互质个数,a,b<=1e15,n<=1e9
n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高。然后差分就好了。
/** @Date : 2017-09-28 16:52:30
* @FileName: HDU 4135 容斥.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; LL pri[N];
bool vis[N];
int c;
void prime()
{
MMF(vis);
for(int i = 2; i < N; i++)
{
if(!vis[i]) pri[c++] = i;
for(int j = 2; j < c && i * pri[j] < N; j++)
{
vis[i * pri[j]] = 1;
if(i % pri[j] == 0) break;
}
}
//19个最小的不同素数乘积即已大于2^63 - 1
/*LL t = 1;
for(int i = 0; i < 100; i++)
t *= pri[i], cout << t <<"~" <<i<<endl;*/
} int main()
{
prime();
int T;
cin >> T;
int icas = 0;
while(T--)
{
LL n, a, b;
scanf("%lld%lld%lld", &a, &b, &n); int cnt = 0;
vector<LL>q;
for(int i = 0; i < c && pri[i] <= n / pri[i]; i++)
{
if(n % pri[i] == 0)
{
while(n % pri[i] == 0)
n /= pri[i];
q.PB(pri[i]);
cnt++;
}
}
if(n > 1)
q.PB(n), cnt++;
LL ansa = 0;
LL ansb = 0;
for(int i = 1; i < (1 << cnt); i++)
{
LL t = 1;
int f = -1;
for(int j = 0; j < cnt; j++)
if((i & (1 << j)))
t *= q[j], f *= -1;
//cout << t << endl;
ansb += f * (b / t);
ansa += f * ((a - 1) / t);
}
//cout << ansa <<"~" <<ansb<<endl;
LL ans = b - ansb - (a - 1 - ansa);
printf("Case #%d: %lld\n", ++icas, ans);
}
return 0;
}
HDU 4135 容斥的更多相关文章
- HDU 2841 容斥 或 反演
$n,m <= 1e5$ ,$i<=n$,$j<=m$,求$(i⊥j)$对数 /** @Date : 2017-09-26 23:01:05 * @FileName: HDU 284 ...
- HDU 1695 容斥
又是求gcd=k的题,稍微有点不同的是,(i,j)有偏序关系,直接分块好像会出现问题,还好数据规模很小,直接暴力求就行了. /** @Date : 2017-09-15 18:21:35 * @Fil ...
- HDU 4059 容斥初步练习
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...
- hdu 1220 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1220 Cube Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Co-prime HDU - 4135_容斥计数
Code: #include<cstdio> #include<cstring> #include<cmath> #include<iostream> ...
- How many integers can you find HDU - 1796_容斥计数
Code: #include<cstdio> using namespace std; typedef long long ll; const int R=13; ll a[R]; ll ...
- hdu 4135 [a,b]中n互质数个数+容斥
http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...
- 容斥 - HDU 4135 Co-prime
Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...
- 【容斥】HDU 4135 Co-prime
acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b) ...
随机推荐
- HDU 4405 Aeroplane chess 期望dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...
- diliucizuoye
NABCD N(Need 需求) 互联网的高速发展,造就了二十一世纪这个追求高品质.高体验的信息时代,随其发展改变的是信息记录与分享方式,从传统的面对面交流.手机通话.写日记本,到现如今的社交平台.信 ...
- Swift-KVC构造函数中数据类型和私有属性
- 2D变换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- net user 修改密码的坑
不多说 直接上图 自己偷懒修改 admin的密码.. 结果没注意 这个地方 能够输入全角字符. 造成密码 实质上是全角的 标点符号 ... 以后一定注意一些. 里面的坑..说多了 都是浪费时间 另外 ...
- true和false
下面这些值在JavaScript中都是falsy: false 0 (数字零) "" (空字符串) null undefined NaN (一个特殊的Number值,意为Not-a ...
- 【Python】python 2 map() reduce()
利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']. ...
- 【Java并发编程】之六:Runnable和Thread实现多线程的区别
Java中实现多线程有两种方法:继承Thread类.实现Runnable接口,在程序开发中只要是多线程,肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下 ...
- Python教程:丛入门到实践
一.特殊用法的函数 name = "python very good" print(name.title()) 方法是python可对数据执行的操作.每个方法后面都跟着一对括号. ...
- 【大数据】MapTask工作机制
1.MapTask工作机制 整个map阶段流程大体如上图所示.简单概述:input File通过getSplits被逻辑切分为多个split文件,通通过RecordReader(默认使用lineRec ...