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) ...
随机推荐
- Head First Java & 重载 覆盖
- iOS完整学习路线图-对知识的回顾/整理
第一阶段:语言基础 Mac系统使用.常用UNIX指令.C语言.Objective-C语言.Foundation框架. 第二阶段:iOS基础 AppDelegate & UIApplicatio ...
- Appium 服务关键字(转)
来源: https://github.com/appium/appium/blob/master/docs/cn/writing-running-appium/caps.cn.md#appium-服务 ...
- SQLSERVER 修改实例名以及架构信息
1. GUI的方式 后者是 alter database 的方式修改 instance的名字 2. 在全局的安全性下面创建用户. 3. 在实例的安全性下面创建架构和用户(架构名与用户名一直, 使用新实 ...
- java异常处理-finally中使用return和throw语句
java异常语句中的finally块通常用来做资源释放操作,如关闭文件.关闭网络连接.关闭数据库连接等.正常情况下finally语句中不应该使用return语句也不应该抛出异常,以下讨论仅限于java ...
- C# QR二维码DEMO
QR二维码 二维码的一种 相关类库 ThoughtWorks.QRCode 第三方类库 DEMO功能 Encode 生成二维码图片 Encoding 编码 Correction Level 等级 Ve ...
- 【Python】python 2 map() reduce()
利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']. ...
- URAL 1969. Hong Kong Tram
有一个trick就是没想到,枚举第二段时间后,要检测该火车能否继续跑一圈来判断,不能先检测前半圈能不能跑加进去后在检测后半段: // **** 部分不能放在那个位置: 最近代码导致的错误总是找不出,贴 ...
- Huge Mods UVA - 10692(指数循环节)
题意: 输入正整数a1,a2,a3..an和模m,求a1^a2^...^an mod m 解析: #include <iostream> #include <cstdio> # ...
- SAM
后缀自动机能识别字符串S的所有子串,是一个DAG. http://blog.csdn.net/huanghongxun/article/details/51112764 https://blog.xe ...