容斥原理: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 ...
随机推荐
- Java hibernate 遇到的问题:could not read a hi value
问题: 解决办法:在网上看到一篇文章说是把数据库实体类的注解@GeneratedValue改成@GeneratedValue(strategy = GenerationType.IDENTITY) , ...
- KendoUI 自定义验证:
Html: <label>@LogicNameAttribute.GetLogicName(typeof(Reward).GetProperty("ExtraRewardMone ...
- UICollectionView笔记2
WWDC 2012 Session笔记——219 Advanced Collection Views and Building Custom Layouts 这是博主的WWDC2012笔记系列中的一篇 ...
- C/S框架设计经验小结
C/S架构程序应用广泛,比如常见的QQ.微信.Outlook,还有手机上的各种APP都是C/S架构的.C指的是Client,即客户端,S指的是Server,即服务端. 经常听到初学者争论,是学C/S结 ...
- vue中background-image图片路径问题
按照以往在css文件中写background:url('图片路径'),完成后加载竟然显示出错,起初以为路径不对,检查了几遍,仍然没有问题.最后百度找答案,发现不少同行都遇到过这种问题,遂记录下自己所采 ...
- html的语法 3
<html> <head> <title>这是第一节课网页标题</title> <!--meta charset="UTF-8" ...
- 零基础逆向工程26_C++_03_Vector
1 Vector 核心代码 #define SUCCESS 1 // 成功 #define ERROR -1 // 失败 #define MALLOC_ERROR -2 // 申请内存失败 #defi ...
- Unity中的输入
目录 移动平台的输入 触摸 触摸相关的函数 触摸的一个示例 重力加速器 在Unity中访问重力加速器的信息 重力加速器示例 虚拟键盘 其他输入 传统的输入 鼠标,键盘,控制杆,手柄 虚拟控制轴(Vir ...
- 大数四则运算java(转)
// 大数的四则运算 #include <iostream> #include <string> #include <algorithm> using namesp ...
- OutOfMemoryError异常 和 StackOverflowError异常
OutOfMemoryError异常 StackOverflowError异常 程序计数器 无 无 Java虚拟机栈 如果虚拟机栈可扩展,扩展时无法申请到足够内存 线程请求的栈深度大于虚拟机所 ...