容斥原理:HDU-4135Co-prime
容斥原理公式:这里就需要用到容斥原理了,公式就是:n/2+n/3+n/5-n/(2*3)-n/(2*5)-n/(3*5)+n/(2*3*5).
求的是多个重合区间的里面的数字个数。
解题心得:
1、一开始很傻很天真,使用遍历然后调用__gcd()来直接怼,但是肯定要超时啊,a,b的范围太大了。
2、求一个数与另一个数是否互质还有一种算法,看这个数是否是另一个数的质因子的倍数(详细算法见:链接:求一个数的质因子),如果是则排除。这样就可以直接使用质因子来筛选就可以了,但是需要的是个数可以直接做除,这样使用的时间就大大的减少了。所以就可以将思路转换求a到b区间的互质数可以使用,0到b区间的互质数减去0-a-1区间的互质数。
3、详细过程:先将一个数的质因子全部放在一个数组之中,看是否是质因子的倍数,这个时候就需要使用到容斥原理,因为不只是简单的将每个互质数组合的倍数减去就是了,有可能有的数重复减去了。
题目:
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4738 Accepted Submission(s): 1894
Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
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.
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
Sample Input
2
1 10 2
3 15 5
Sample Output
Case #1: 5
Case #2: 10
Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
Source
The Third Lebanese Collegiate Programming Contest
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int prim[maxn];//用来存储质因子
int ch[maxn];//用来存储质因子的组合
int n,T;
long long a,b; //得到质因子
void prim_num(int N)
{
T = 0;
for(int i=2;i*i<=N;i++)
{
if(N%i == 0)
{
prim[T++] = i;
while(N%i == 0)
N /= i;
}
}
if(N != 1)
prim[T++] = N;
} long long Check(long long num)
{
memset(ch,0,sizeof(ch));
ch[0] = -1;
int t2 = 1;
for(int i=0;i<T;i++)
{
int now;
now = t2;
//这个循环很重要它是得到的质因子的组合,仔细理解(顺序并不是和公式上面的顺序一样)
for(int j=0;j<now;j++)
ch[t2++] = ch[j]*prim[i]*(-1);
} long long sum = 0;
for(int j=1;j<t2;j++)
sum = sum + num/ch[j];//虽然看起来都是加,但是有正有负,得到的就是最终的答案
return sum;
} int main()
{
int t;
scanf("%d",&t);
int z = t;
while(t--)
{
scanf("%lld%lld%d",&a,&b,&n);
prim_num(n);
printf("Case #%d: ",z-t);
long long now1 = b - Check(b);
long long now2 = a-1 - Check(a-1);
printf("%lld\n",now1 - now2);
}
}
容斥原理:HDU-4135Co-prime的更多相关文章
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- [容斥原理] hdu 4135 Co-prime
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- HDU 1016 Prime Ring Problem
在刚刚写完代码的时候才发现我以前交过这道题,可是没有过. 后来因为不理解代码,于是也就不了了之了. 可说呢,那时的我哪知道什么DFS深搜的东西啊,而且对递归的理解也很肤浅. 这道题应该算HDU 261 ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- HDU 1016 Prime Ring Problem 题解
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- subline 安装 package control 连接服务器失败,解决办法
解决办法: 打开C:\Windows\system32\drivers\etc\hosts文件 增加 50.116.34.243 sublime.wbond.net50.116.34.243 pack ...
- Validation failed for one or more entities. See ‘EntityValidationErrors’,一个或多个验证错误 解决方法
try{// 写数据库}catch (DbEntityValidationException dbEx){ }在 dbEx 里面中我们就可以看到
- hibernate课程 初探单表映射3-3 对象类型
本节简介: 1 简介对象类型(重点是音视频blob类型) 2 demo(对图片的写入数据库与读取) 1 简介对象类型 映射类型 java类型 标准sql类型 mysql类型 oracle类型 bina ...
- Python接入支付宝进行PC端支付
1. 支付宝开放平台登录,使用支付宝账号登录 https://open.alipay.com/platform/home.htm 2.选择沙箱模式 [支付宝提供了测试环境] https://docs ...
- Vue-mixins选项
Vue-mixins选项 Mixins用于: 1.已经写好了构造器,还要增加方法或者临时的活动时使用的方法,用混入能减少源代码的污染. 2.公用方法,用混入的方法可以减少代码量,实现代码重用.(使用全 ...
- css3动画-加载中...
写几个简单的加载中动画吧. 像前面三种都是相当于几个不同的点轮流来播放同一动画:变大变小.css3里面有一个用于尺度变换的方法:scale(x,y):定义 2D 缩放转换,改变元素的宽度和高度. 第四 ...
- 系统装更新补丁出现“正在此计算机上搜索更新”,有时等待N长时间也没有反应
系统装更新补丁出现“正在此计算机上搜索更新”,有时等待N长时间也没有反应 管理员身份运行 net stop wuauserv net stop CryptSvc ren %windir%\system ...
- 新建snmp模型总结
1.在DeviceType.xml中添加新的模块 2.在modellidx.json中添加路径关联 3.添加定义模型 4.定义model.xml注意: 5.定义collect.xml注意:
- Redis单机数据库
单机数据库 ·Redis服务器的所有数据库都保存在redisServer.db数组中,而数据库的数量则由redisServer.dbnum属性保存. ·客户端通过修改目标数据库指针,让它指向redis ...
- noip模拟赛#24
这套题我只会写第二题...我... T1:给出一个含有向边和无向边的混合图,如何确定无向边的方向使得图中不存在环.保证有解.多解情况输出任意解. =>我往最大流的残量网络的方向去想了...因为混 ...