问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. Mongodb compass 介绍

    参考官方文档:https://docs.mongodb.com/compass/current/install/#install-on-red-hat-enterprise-linux-rhel Mo ...

  2. Centos7 FPM 安装mysql8

    参考http://tn.51cto.com/article/647 http://www.jb51.net/article/138787.htm 转载: 卸载centos自带的mariadb rpm ...

  3. Pytest - 进阶功能fixture

    1. 概述 Pytest的fixture功能灵活好用,支持参数设置,便于进行多用例测试,简单便捷,颇有pythonic.如果要深入学习pytest,必学fixture. fixture函数的作用: 完 ...

  4. http和https的优缺点,区别与工作原理

    文章内容 超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可 ...

  5. vue 组件 模板input双向数据数据

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>T ...

  6. 微信 小程序布局 scroll-view

      //滚动触底事件 <scroll-view scroll-y lower-threshold="0" bindscrolltolower="scrollBott ...

  7. PHP导出sql文件

    发现自己之前写的php导出sql数据为Excel文件在导出一些数据的时候出现了精度的问题,比如导出身份证号的时候会把后面变成0000.暂时先把这个问题留下,有空去看看到底是什么问题. 写了一个导出sq ...

  8. CentOS 6.5以上版本安装mysql 5.7 完整版教程(修订版)

    转载自:https://codeday.me/collect/20170524/21861.html 1: 检测系统是否自带安装mysql # yum list installed | grep my ...

  9. Linux环境下安装配置Mysql

    首先我们的使用的是linux的 centOS6 版本 安装mysql: 1.查看有没有安装包 yum list mysql* 2.进行安装mysql:一般我们在服务器端安装的都是服务端( mysql- ...

  10. 通过jmap查看jvm采用的垃圾收集器

    1  tomcat 的PID获得 ps -ef|grep tomcat [root@iZ2zeapch8kbaw4bxnz8vxZ tomcat7]# ps -ef|grep tomcat root ...