Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 935    Accepted Submission(s): 339

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
 /*
题意:求区间[A,B],与数字N互素的个数。 转化一下思路:1.求[A,B],转化为
[1,B]中与N互素的个数
减去
[1,A-1]中与N互素的个数。
这样问题就转化为[1,K]的问题了。
2.求[1,K]中与N互素的个数又转化为 K 减去 [1,K]中与N满足
gcd(N,k1)>1的个数。
接下来,就要求N的素因子了。为什么是素因子??
因为每一个数都能有素因子组成。
求素因子的方法和求欧拉的方法是一样的。
有两种方法,一种适合于单点求取,数字比较大的时候更好。
一种适合于打表求取,适用数字相对较的情况。
3.求解的过程中会遇到重复的问题。
举一个例子 m=12,n=30
第一步:求出n的质因子:2,3,5;
第二步:(1,m)中是n的因子的倍数当然就不互质了
(2,4,6,8,10)->n/2 6个,(3,6,9,12)->n/3 4个,(5,10)->n/5 2个。
如果是粗心的同学就把它们全部加起来就是:6+4+2=12个了,
那你就大错特错了,里面明显出现了重复的,
我们现在要处理的就是如何去掉那些重复的了!
第三步:这里就需要用到容斥原理了,公式就是:n/2+n/3+n/5-n/(2*3)-n/(2*5)-n/(3*5)+n/(2*3*5).
第四步:我们该如何实现呢?
我在网上看到有几种实现方法:dfs(深搜),队列数组,位运算三种方法都可以!
上述公式有一个特点:n除以奇数个数相乘的时候是加,n除以偶数个数相乘的时候是减。
我这里就写下用队列数组如何实现吧:我们可以把第一个元素设为-1然后具体看代码如何实现吧!
*/ #include<stdio.h>
#include<string.h>
#include<stdlib.h> __int64 Que[];
__int64 f[],len;
void Euler(__int64 n)//求取素因子,保存在f[]
{
__int64 i;
len=;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
while(n%i==)
n=n/i;
f[++len]=i;
}
}
if(n!=)
f[++len]=n;
} __int64 Capticy(__int64 num)容斥定理的运用
{
__int64 i,j,t=,k,sum=;
Que[t++]=-;
for(i=;i<=len;i++)
{
k=t;
for(j=;j<k;j++)
Que[t++]=-*Que[j]*f[i];
}
for(i=;i<t;i++)
sum=sum+num/Que[i];
return sum;
} int main()
{
__int64 T,A,B,N,i,k;
while(scanf("%I64d",&T)>)
{
for(i=;i<=T;i++)
{
scanf("%I64d%I64d%I64d",&A,&B,&N);
Euler(N);
k=B-Capticy(B)-(A--Capticy(A-));
printf("Case #%I64d: %I64d\n",i,k);
}
}
return ;
}

HDU 4135 Co-prime 欧拉+容斥定理的更多相关文章

  1. GCD HDU - 1695 (欧拉 + 容斥)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

  4. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  5. HDU 2841 Visible Trees(容斥定理)

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  6. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. 【hdu4135】【hdu2841】【hdu1695】一类通过容斥定理求区间互质的方法

    [HDU4135]Co-prime 题意 给出三个整数N,A,B.问在区间[A,B]内,与N互质的数的个数.其中N<=10^9,A,B<=10^15. 分析 容斥定理的模板题.可以通过容斥 ...

  9. HihoCoder1655 : 第K小最简真分数([Offer收割]编程练习赛39)(唯一分解+容斥定理+二分)(不错的数学题)

    描述 给一个整数N,请你求出以N为分母的最简(既约)真分数中第K小的是多少? 输入 两个整数N个K. 对于30%的数据,1 <= N <= 1000000 对于100%的数据,1 < ...

随机推荐

  1. POI2014 RAJ-Rally

    Description 给定一个\(N\)个点\(M\)条边的\(DAG(N,M\leq10^6)\),边权为\(1\).删去一个点,使剩余图中的最长路径最短,求删去的点和最长路径长度. Soluti ...

  2. 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  3. Two Pointers-349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  4. java验证身份证合理性

    package com.tiantian.util; import java.util.Calendar;import java.util.HashMap;import java.util.Map;i ...

  5. SELECT 三级联动 [转]

    <!DOCTYPE html> <html> <head> <meta charset=gbk /> <title>selectList&l ...

  6. iOS---UICollectlionView 的使用

    UICollectlionView继承自UIScrollerview,跟tableview的使用很相似. 下面是UIcollectionView的一些属性和代理方法. #import "Vi ...

  7. express+nodemon 修改后浏览器自动刷新

    添加nodemon模块 cnpm install --save nodemon 根目录添加文件 nodemon.json { "restartable": "rs&quo ...

  8. YYYY-mm-dd HH:MM:SS 时间格式

    YYYY-mm-dd HH:MM:SS部分解释 d               月中的某一天.一位数的日期没有前导零.    dd             月中的某一天.一位数的日期有一个前导零.   ...

  9. Linux CentOS7系统探索

    这两天,突发奇想,想着用着微软家的windows系统很多年了,也想尝试一下其他的操作系统.很快的就想到了Linux操作系统,它不是面向用户的,而是面向服务器的,在服务器端的市场中占了很大的市场份额,备 ...

  10. SQL中文转拼音

    使用下方的函数.. 忘了从哪抄的了..留存一份 如果只要首字母..建议将数据  Left(tableFiled,1) 后传入函数 如果字段是空或者null, 不会报错..返回空 方法体: SET AN ...