[容斥原理] hdu 4135 Co-prime
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4135
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1176 Accepted Submission(s): 427
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
1 10 2
3 15 5
Case #2: 10
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
题目意思:
给一个a,b,n求在[a,b]内有多少个与n互质的数。
解题思路:
简单的容斥原理。
反面思考。先求出与n不互质的,也就是包括n的质因数的。然后就能够想到先把n分解质因数。
然后能够想到先预处理1000000内的质数。
然后求出1~b满足要求的个数,减去1~a-1满足要求的个数,答案即为最后结果。
代码:
//#include<CSpreadSheet.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define N 1000000
int prime[130000],cnt;
bool isprime[N+10];
int pp[110000],cnt0;
ll a,b,n,ans1,ans2;
void init() //预处理出1~1000000内的质数
{
cnt=0;
for(int i=1;i<=N;i++)
isprime[i]=true;
for(int i=2;i<=N;i++)
{
if(!isprime[i])
continue;
prime[++cnt]=i;
for(int j=i*2;j<=N;j+=i)
isprime[j]=false;
}
//printf("cnt:%d\n",cnt);
}
void Cal(ll cur) //分解出cur的质因数
{
cnt0=0;
for(int i=1;prime[i]*prime[i]<=cur;i++)
{
if(cur%prime[i]==0)
{
pp[++cnt0]=prime[i];
while(!(cur%prime[i]))
cur/=prime[i];
}
}
if(cur!=1)
pp[++cnt0]=cur;
}
void dfs(ll hav,int cur,int num) //容斥原理求出互质的
{
if(cur>cnt0||(hav>a&&hav>b))
return ;
for(int i=cur;i<=cnt0;i++)
{
ll temp=hav*pp[i];
if(num&1)
{
ans1-=b/temp;
ans2-=a/temp;
}
else
{
ans1+=b/temp;
ans2+=a/temp;
}
dfs(temp,i+1,num+1);
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
init();
//system("pause");
int t;
scanf("%d",&t);
for(int ca=1;ca<=t;ca++)
{
scanf("%I64d%I64d%I64d",&a,&b,&n);
Cal(n);
//printf("cnt0:%d\n",cnt0);
a--;
ans1=b,ans2=a;
for(int i=1;i<=cnt0;i++)
{
ans1-=b/pp[i];
ans2-=a/pp[i];
dfs(pp[i],i+1,2);
}
printf("Case #%d: %I64d\n",ca,ans1-ans2);
}
return 0;
}
[容斥原理] hdu 4135 Co-prime的更多相关文章
- HDU 4135 容斥
问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...
- hdu 4135 Co-prime (素数打表+容斥原理)
题目链接 题意:问从A到B中与N互素的个数. 题解: 利用容斥原理:先求出与n互为素数的个数. 可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数, ...
- 容斥原理学习(Hdu 4135,Hdu 1796)
题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 4135 Co-prime(容斥原理)
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...
- HDU 4135 容斥原理
思路: 直接容斥 //By SiriusRen #include <cstdio> using namespace std; #define int long long ; int cas ...
- HDU 4135
http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这 ...
- 容斥 - HDU 4135 Co-prime
Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...
- hdu 5901 count prime & code vs 3223 素数密度
hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...
- hdu 4135 Co-prime(容斥)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- Java基础IO流(四)序列化与反序列化
对象的序列化与反序列化: 对象的序列化,就是将Object转换成byte序列,反之叫对象的反序列化. 序列化流(ObjectOutInputStream),是过滤流 -------writeObjec ...
- linux服务器重启指令
一.Linux 的五个重启命令 1.shutdown 2.poweroff 3.init 4.reboot 5.halt 二.五个重启命令的具体说明 shutdown reboot 在linux下一些 ...
- Chrome 开发者工具
打开开发工具 (1)在Chrome菜单中选择 更多工具 > 开发者工具. (2)在页面元素上右键点击,选择 "检查". (3)使用快捷键 Ctrl+Shift+I (Wind ...
- #WEB安全基础 : HTML/CSS | 0x1初识CSS
"我受够这些难看的网页了,我怎么才能让它变得好看点?"你说. 我答道:"看来你得学点CSS了" 学习这些东西只有一个原则,就是用你的脑袋想,用你的眼睛看,用的你 ...
- 洛谷P3245 [HNOI2016]大数(莫队)
题意 题目链接 Sol 莫队板子题.. 维护出每个位置开始的字符串\(mod P\)的结果,记为\(S_i\) 两个位置\(l, r\)满足条件当且仅当\(S_l - S_r = 0\),也就是\(S ...
- Java NIO 学习
Java NIO提供了与标准IO不同的IO工作方式: Channels and Buffers(通道和缓冲区):标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(B ...
- 在 Apex 中使用合并统计查询
SOQL 中的合并统计查询 在 SOQL 中,我们可以使用一系列函数来进行合并统计查询.它们的功能和标准 SQL 中的 SUM(),COUNT() 等函数类似. 官方文档 Apex 中使用合并统计查询 ...
- JVM调优日志解析分析
一.调优参数设置 JVM的GC日志的主要参数包括如下几个: -XX:+PrintGC 输出GC日志 -XX:+PrintGCDetails 输出GC的详细日志 -XX:+PrintGCTimeStam ...
- 性能测试 基于Python结合InfluxDB及Grafana图表实时采集Linux多主机或Docker容器性能数据
基于Python结合InfluxDB及Grafana图表实时采集Linux多主机性能数据 by:授客 QQ:1033553122 实现功能 1 测试环境 1 环境搭建 3 使用前提 3 使用方法 ...
- Nginx filebeat+logstash+Elasticsearch+kibana实现nginx日志图形化展示
filebeat+logstash+Elasticsearch+kibana实现nginx日志图形化展示 by:授客 QQ:1033553122 测试环境 Win7 64 CentOS-7- ...