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. poj3204Ikki's Story I - Road Reconstruction(最大流求割边)

    链接 最大流=最小割  这题是求割边集 dinic求出残余网络 两边dfs分别以源点d找到可达点 再以汇点进行d找到可达汇点的点 如果u,v为割边 那么s->u可达 v->t可达 并且为饱 ...

  2. 搭建一个高可用的redis环境

    一.环境准备 我的环境: Fedora 25 server  64位版 6台: 192.168.10.204 192.168.10.205 192.168.10.206 192.168.10.203 ...

  3. sass 常用用法笔记

    最近公司开发的h5项目,需要用到sass,所以领导推荐让我去阮一峰大神的SASS用法指南博客学习,为方便以后自己使用,所以在此记录. 一.代码的重用 1.继承:SASS允许一个选择器,继承另一个选择器 ...

  4. asp.net mvc 5 微信接入VB版 - 接入认证

    微信接入官方文档是php的,网上被抄好几遍的代码是c#的,就是没vb的.今天我把这个坑填了,做vb版的接入认证. 首先是照着开发文档把微信接入的模型写好.在Models文件夹新建一个Model Pub ...

  5. SEO 第四章

    SEO第四章 课程目标: 掌握网站TDK的优化方法 1.  页面TKD介绍 Title  keywords description 标题 关键字 描述 网站的每一个页面都有三大标签(主页.栏目页.内容 ...

  6. ubuntu 安装 pcap

    最近在做负载均衡配置,希望将多个dhcp服务配置成一个虚拟dhcp地址,实现dhcp服务高可用.然而配置完成后却发现一个问题,该如何测试呢. 因此就要用上python了,然后ubuntu下面用pip ...

  7. (译文)IOS block编程指南 3 概念总览

    Conceptual Overview(概览) Block objects provide a way for you to create an ad hoc function body as an ...

  8. varchar2(100 char)是什么意思

    最佳答案   varchar2(100 char)最长可以插入100个任意字符而varchar2(100)最长可以插入100个英文字符

  9. axios 里面 then 默认写的function里面没有this,改成箭头函数后就可以用this了

    ,methods:{ loadJson:function(){ //this.jsonTest = "jjj" this.$http.get('http://localhost:3 ...

  10. GCC、g++编译器和gcc编译器的区别

    GCC:(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分. gcc:GNU的C语 ...