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. 类成员的指针必须NULL化,否则是乱七八糟的东西

    class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...

  2. spring @value 为什么没有获取到值

    1.配置文件的路径没有扫描到 2.注解的bean 不是通过spring托管的.bean 要通过spring 注解,引用的时候要用@Autowired  自动注入的bean 不要用new 出来的bean ...

  3. android开发工具eclipse的安装与配置

    l开发主要应用Eclipse 3.7版本. l辅助工具为jdk.Androidsdk Android环境搭建   –1.1.JDK安装 –1.2.Eclipse安装 –1.3.Android SDK安 ...

  4. ssm框架搭建(下) 简单案例

    前言 这段时间没有更新博客,一直想做一个基于ssm的简单的项目.经过多次的尝试,终于实现了简单的增删查改功能了. 正文 由于前端的技术不是很熟悉,经过多方的查阅,使用了bootstrap的样式,来使界 ...

  5. leetcode_935. Knight Dialer_动态规划_矩阵快速幂

    https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...

  6. shell 数值比较和字符串比较

    1. 数值比较 -eq        是否相等(equal) -gt         是否大于(greater than) -ge       是否大于等于(greater and equal tha ...

  7. 关于MessageBox返回值

    风格设置MB_OK. 此时无论点击确定还是点击X,都返回IDOK.风格设置MB_OKCANCEL,点击确认返回IDOK,点击取消和X都返回IDCANCEL.风格设置MB_YESNO,点击是返回IDYE ...

  8. 批处理 更新 svn git hg

    @echo off Setlocal enabledelayedexpansion ::CODER BY Administrator POWERD BY iBAT 1.6 ::设置svn默认安装位置以 ...

  9. source 1.7 中不支持 lambda 表达式(请使用 -source 8 或更高版本以启用 lambda 表达式)

    from:http://blog.csdn.net/qwdafedv/article/details/54691740 https://www.youtube.com/watch?v=oXxijdf8 ...

  10. nodejs实现网站数据的爬取

    // 引入https模块,由于我们爬取的网站采用的是https协议 const https = require('https'); // 引入cheerio模块,使用这个模块可以将爬取的网页源代码进行 ...