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. android AutoCompleteTextView 实现手机号格式化,附带清空历史的操作

    有个小伙伴遇到了这样一个问题,就是AutoCompleteTextView实现自动填充的功能.同时要具备手机格式化的功能.下拉列表最后一行是有个清除历史的功能.可是点击“清除历史”却把文字要设置进去A ...

  2. MVVM没你想象的那么的好

    我写过很多有关于让View Controller 更易于理解的文章,其中一种比较常见的模式就是Model-View-ViewModel(MVVM). 我认为MVVM 是一种非常容易让人混淆的 anti ...

  3. k8s部署测试实例

    查看运行中pod,并运行一个容器 [root@mast-1 k8s]# kubectl get pods No resources found. [root@mast-1 k8s]# kubectl ...

  4. Java代理模式精讲之静态代理,动态代理,CGLib代理

    代理(Proxy)是一种设计模式,通俗的讲就是通过别人达到自己不可告人的目的(玩笑). 如图: 代理模式的关键点是:代理对象与目标对象.代理对象是对目标对象的扩展,并会调用目标对象 这三个代理模式,就 ...

  5. Python matlab octave 矩阵运算基础

    基础总结,分别在三种软件下,计算 求逆矩阵 矩阵转置 等运算,比较异同 例子:正规方程法求多元线性回归的最优解 θ=(XTX)-1XTY octave: pwd()当前目录 ones() zeros( ...

  6. 1.ssm web项目中的遇到的坑--自定义JQuery插件(slide menu)

    自定义的JQuery插件写的回调函数不执行: 写好了回调函数,将函数打印出来是原形,就是不执行 function () { console.log("---onClickItem---&qu ...

  7. python之操作excel:xlrd、xlwt、xlutiles、枚举函数enumerate()

    一.读excel: xlrd-----只能读.不能写 import xlrd book=xlrd.open_workbook(r'E:\BestTest\内容\名单.xlsx') #打开excel s ...

  8. No-7.系统信息相关命令

    系统信息相关命令 本节内容主要是为了方便通过远程终端维护服务器时,查看服务器上当前 系统日期和时间 / 磁盘空间占用情况 / 程序执行情况 本小结学习的终端命令基本都是查询命令,通过这些命令对系统资源 ...

  9. 纯html+css中实现静态选座位效果技巧(input+label使用小技巧)

    很多时候,我们想通过html+css的方式实现排列在后方的代码在选中状态下,能控制排列在前的代码的样式.那么要怎么实现呢?在这里我就要用1个小技巧来完成. 众所周知的,我们css中的选择器通常只能向下 ...

  10. MYSQL数据库攻防与加固

    这是“官方”原本的模样搬过来的..写的很粗略啊.还有篇详细的请查看:MySQL安全加固题目及答案参考解析 启动xserver-mysql,进入xserver-mysql,开始实验,实验步骤如下: 1. ...