容斥原理公式:这里就需要用到容斥原理了,公式就是: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的更多相关文章

  1. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  2. [容斥原理] hdu 4135 Co-prime

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  5. HDU 1016 Prime Ring Problem

    在刚刚写完代码的时候才发现我以前交过这道题,可是没有过. 后来因为不理解代码,于是也就不了了之了. 可说呢,那时的我哪知道什么DFS深搜的东西啊,而且对递归的理解也很肤浅. 这道题应该算HDU 261 ...

  6. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  7. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  8. HDU 1016 Prime Ring Problem 题解

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  9. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  10. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. SharePoint 2010开发方面的课堂中整理有关问题

    SharePoint 2010开发方面的课堂中整理有关问题 这是我这几天在做一个SharePoint开发的课程的时候,大家提出的一些问题,及我的解答,分享给更多的朋友参考一下 这个文档,也可以在这里下 ...

  2. 使用Quartz任务调用的时候报错Based on configured schedule, the given trigger will never fire.

    org.quartz.SchedulerException: Based on configured schedule, the given trigger will never fire. 大概意思 ...

  3. spring ehcache 使用详解

    Spring 整合 Ehcache 管理缓存详解  yellowbutterfly 前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象: ...

  4. ECMAScript 原始值和引用值

    原始值和引用值 在ECMAScript中,变量可以存在两种类型的值,即原始值和引用值 原始值 存储

  5. PHP线程安全和非线程安全有什么区别

    我们先来看一段PHP官网的原话: Which version do I choose? IIS If you are using PHP as FastCGI with IIS you should ...

  6. Team Foundation 版本控制

    与 Visual Studio 的一流集成. 使用富文件和文件夹差异工具突出显示代码更改. 借助强大的可视化跨分支跟踪代码更改. 集成的代码评审工具有助于在签入代码之前获得反馈. 使用托管版本或本地版 ...

  7. python3基础05(有关日期的使用1)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import timefrom datetime import datetime,timedelta,timez ...

  8. centos6.5_64bit-nginx开机自启动

    Nginx 是一个很强大的高性能Web和反向代理服务器.下面介绍在linux下安装后,如何设置开机自启动. 首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:   ...

  9. 怎么旋转PDF文件的方向并保存成功

    http://jingyan.baidu.com/article/59a015e39d7802f79488651e.html PDF格式的文档是非常普遍的一种阅读电子书格式,基本上非常好用了,不过有时 ...

  10. graylog插件的安装

    什么是插件和插件的作用我就不说了,大家应该都知道了. 安装方法是打开下面选择项     进去后出现如下界面 选择事先下载好的插件后,点击上传.这里以nginx日志插件为例 之后查看效果,发现对应的过滤 ...