问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 容斥的更多相关文章

  1. HDU 2841 容斥 或 反演

    $n,m <= 1e5$ ,$i<=n$,$j<=m$,求$(i⊥j)$对数 /** @Date : 2017-09-26 23:01:05 * @FileName: HDU 284 ...

  2. HDU 1695 容斥

    又是求gcd=k的题,稍微有点不同的是,(i,j)有偏序关系,直接分块好像会出现问题,还好数据规模很小,直接暴力求就行了. /** @Date : 2017-09-15 18:21:35 * @Fil ...

  3. HDU 4059 容斥初步练习

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  4. hdu 1220 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1220 Cube Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  5. Co-prime HDU - 4135_容斥计数

    Code: #include<cstdio> #include<cstring> #include<cmath> #include<iostream> ...

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

  7. hdu 4135 [a,b]中n互质数个数+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...

  8. 容斥 - HDU 4135 Co-prime

    Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...

  9. 【容斥】HDU 4135 Co-prime

    acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b) ...

随机推荐

  1. Hibernate利用纯sql

    String hql = "select * from shop where shop.strid in(select strid from moneythreeshop where mon ...

  2. 索引超出了数组界限。 在 System.Collections.Generic.Dictionary`2.Resize

    博问:Dictionary 超出了数组界限 异常: Exception type: IndexOutOfRangeException Exception message: 索引超出了数组界限. 在 S ...

  3. Unity3D游戏开发——显示物品的仓库UI

    访问仓库物品列表的方法 为了在UI中显示物品列表,我们需要给InventoryManager添加两个能够访问它的公有方法: 代码: ··· public List<string> GetI ...

  4. sprintf函数 %6.2f

    %6.2f6表示数据表示至少6位,后面的.2表示小数点后保留两位 比如2342.123415用这个表示的话,结果就是2342.12如果不足六位就会在前面补空格超过六位的话正常显示 代码例子:int m ...

  5. \0 的ASCII码值是多少

    \0 的ASCII码值是多少 #include<iostream> using namespace std; void main() { char c = '\0'; cout<&l ...

  6. 6/8 sprint2 看板和燃尽图的更新

  7. [转帖]nvidia nvlink互联与nvswitch介绍

    nvidia nvlink互联与nvswitch介绍 https://www.chiphell.com/thread-1851449-1-1.html 差不多在一个月前在年度gtc会议上,老黄公开了d ...

  8. DotNetty 跨平台的网络通信库

    长久以来,.Net开发人员都非常羡慕Java有Netty这样,高效,稳定又易用的网络通信基础框架.终于微软的Azure团队,使用C#实现的Netty的版本发布.不但使用了C#和.Net平台的技术特点, ...

  9. MySql的多存储引擎架构, 默认的引擎InnoDB与 MYISAM的区别(滴滴)

    1.存储引擎是什么? MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术, ...

  10. iOS 一些常见问题

    1.屏幕横屏时 xib上拖拉的控件不会跟着横过来: 是因为在主文件面里的 main interface 方框里的main 没有删除: 2.运行出现你没有权限 : 清理一下: 3.将对象转成字符串: / ...