Co-prime

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

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
 
Recommend
lcy
 
就是求区间与2互质的数的个数,就是简单容斥吧,用dfs做会比较好。
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#define ll long long
using namespace std; ll ans,l,r,k;
int num,cnt;
ll a[],prime[];
bool boo[];
int Case; void init_prime()
{
for (int i=;i<=;i++)
{
if (!boo[i]) prime[++cnt]=i;
for (int j=;j<=cnt&&prime[j]*i<=;j++)
{
boo[prime[j]*i]=;
if (i%prime[j]==) break;
}
}
}
void devide_prime()
{
num=;
for (int i=;i<=cnt;i++)
{
if (k%prime[i]==)
{
a[++num]=prime[i];
while(k%prime[i]==) k/=prime[i];
}
}
if (k>) a[++num]=k;
}
void query(int deep,ll shu,ll qz,ll zhi)
{
if (deep>num)
{
ans+=qz*zhi/shu;
return;
}
query(deep+,shu*a[deep],-qz,zhi);
query(deep+,shu,qz,zhi);
}
int main()
{
init_prime();
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%lld%lld%lld",&l,&r,&k);
devide_prime();ans=;
query(,,,r);
query(,,-,l-);
printf("Case #%d: %lld\n",++Case,ans);
}
}

hdu4135 Co-prime【容斥原理】的更多相关文章

  1. HDU4135 Co-prime(容斥原理)

    题目求[A,B]区间内与N互质数的个数. 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]. 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1, ...

  2. [HDU4135]CO Prime(容斥)

    也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...

  3. hdu4135 Co-prime 容斥原理

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

  4. hdu4135容斥原理 组合遍历

    容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...

  5. HDU4135(容斥原理)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. 容斥原理的(二进制思想和质因子分解+模板)hdu4135+ecf81.D

    题:http://acm.hdu.edu.cn/showproblem.php?pid=4135 题意:求[A,B]与N互质的数的个数 #include<iostream> #includ ...

  7. Codeforces1036F Relatively Prime Powers 【容斥原理】

    题目分析: 这种题目标题写莫比乌斯反演会不会显得太恐怖了,那就容斥算了. gcd不为1的肯定可以开根.所以把根式结果算出来就行了. 辣鸡题目卡我精度. 代码: #include<bits/std ...

  8. HDU4135容斥原理

    #include <cstdio> #include <string.h> #include <cmath> using namespace std; #defin ...

  9. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

随机推荐

  1. Unity项目学习笔记

    1.TCP和IP IP:主要作用是在复杂的网络环境中将数据包发送给的最终的目标地址. 端口号:系统会分给系统端口号  一般知名的端口号在0-1023之间,而我们经常使用的自定义/动态分配的端口号则一般 ...

  2. .net core区域设置方式

    在Startup中配置 默认路由是这个,并不包含区域路由 routes.MapRoute( name: "default", template: "{controller ...

  3. JVM线程与Linux内核线程的映射[转]

    Linux从内核2.6开始使用NPTL (Native POSIX Thread Library)支持,但这时线程本质上还轻量级进程. Java里的线程是由JVM来管理的,它如何对应到操作系统的线程是 ...

  4. CCF|学生排队|Java

    import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Sc ...

  5. 洛谷 U10206 Cx的治疗

    题目背景 「Cx的故事」众所周知,Cx是一个宇宙大犇.由于Cx在空中花园失足摔下,导致他那蕴含着无穷智慧的大脑受到了严重的损伤,许多的脑神经断裂.于是,Cx的wife(有么?)决定请巴比伦最好的医师治 ...

  6. sccm系统更新补丁后服务无法正常启动

    更新完补丁后这几个应用无法启动,最后发现计算机丢失msvcp120.dll 文件,查询相关资料发现安装vcredist 2013 从官网下载Visual C++ Redistributable Pac ...

  7. python实现: protobuf解释器

    之前项目为了自动化,所以写一个protobuf的解释器,用来生成项目所需的格式. 当然现在通过以下链接的指导,跳过手工分析,直接生成代码了. https://developers.google.com ...

  8. systemtap执行过程中报probe timer.profile registration error

    probe timer.profile registration error 今天在执行火焰图的过程中,代码报错,probe timer.profile registration error 经过查询 ...

  9. CocosCreator工程内的命名

    命名结构总体的命名结构遵循以下格式 前缀-内容-尾缀 - 前缀:用来定义node的属性- 内容:node的名字- 尾缀:序列或状态1231. 前缀说明:在开始的时候定义/声明这个节点的属性前缀可以是一 ...

  10. Linux之用户权限管理

    chmod(更改目录或文件权限) 在linux中,文件的权限分为3中,拥有者,群组,其他人.而chmod则是对权限更改的命令. u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个组,o 表示其 ...