容斥原理公式:这里就需要用到容斥原理了,公式就是: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. Eclipse下git如何创建分支

    1.项目–Team–Switch To –New Branch 2.Branch name 填写自己的版本号,然后Finish即可 3.将分支内容Push到远程服务器上

  2. 在MFC对话框中快速集成三维控件

    在MFC的对话框中可以方便的集成AnyCAD三维控件(c++版本),遵循一下几步: 1.在对话框资源中增加一个Static控件,ID为IDC_STATIC_3D,并且把它的Notify属性设置为Tru ...

  3. Xcode8-beat升级需谨慎

    Xcode8-beat版本在打开xib文件的时候,出现了如下的弹窗 在这里要选择Cancel,选择Choose后xib文件的verson会改变,那么Xcode7就没法打开了(坑队友啦), 更没法运行 ...

  4. Servlet的生命周期以及线程安全问题

    一:Servlet生命周期图,以及注意事项 二:代码演示 LifeCycleServlet.java package cn.woo.servlet; import java.io.IOExceptio ...

  5. 弹框&可用于判断

    较常用的弹框:(3种) 1.prompt("显示用户的文本","输入域的默认值"): print();显示打印的对话框: find();显示查找的对话框: (用 ...

  6. webapp一些样式记录

    图片外面的div设置宽高自适应width: 100vw; max-width: 640px; display: block; height: 43.75vw; max-height: 280px; f ...

  7. ${fn:} 函数

    调用这样一个头文件<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions " ...

  8. uvm_hdl——DPI在UVM中的实现(四)

    我们可以在uvm中实现HDL的后门访问,具体包括的function有uvm_hdl_check_path,uvm_hdl_deposit, uvm_hdl_force,uvm_hdl_release, ...

  9. javascript字符串格式化string.format

    String.prototype.format = function () { var values = arguments; return this.replace(/\{(\d+)\}/g, fu ...

  10. Java JDBC链接Oracle数据库

    package com.test.test; import java.io.FileInputStream;import java.io.FileNotFoundException;import ja ...