Co-prime

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 49   Accepted Submission(s) : 21

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

The Third Lebanese Collegiate Programming Contest

——————————————————————————————————
题目的意思是给出一段区间求与给出的k互质的数有几个

思路:先把问题转化为前缀和问题,然后算出与k有公因数有多少个,可以先对k分解质

因数,这样的数的种类肯定不多

然后dfs求每种数的个数,用容斥原理计数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath> using namespace std; #define LL long long
const int inf=0x7fffffff; LL p[100005];
LL ans,l,r;
int tot; void dfs(int pos,LL x,int cnt)
{
if(pos>=tot)
{
if(cnt==0)
return;
if(cnt%2)
{
ans+=r/x;
ans-=(l-1)/x;
}
else
{
ans-=r/x;
ans+=(l-1)/x;
}
return;
}
dfs(pos+1,x*p[pos],cnt+1);
dfs(pos+1,x,cnt);
} int main()
{ int T;
LL k;
scanf("%d",&T);
int q=1;
while(T--)
{
tot=0;
scanf("%lld%lld%lld",&l,&r,&k);
for(LL i=2;i*i<=k;i++)
{
if(k%i==0)
p[tot++]=i;
while(k%i==0) k/=i;
}
if(k>1) p[tot++]=k;
ans=0;
dfs(0,1,0);
ans=r-l+1-ans;
printf("Case #%d: %lld\n",q++,ans);
}
return 0;
}

Hdu4135 Co-prime 2017-06-27 16:03 25人阅读 评论(0) 收藏的更多相关文章

  1. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

  2. 指向函数的指针 分类: C/C++ 2015-07-13 11:03 14人阅读 评论(0) 收藏

    原文网址:http://www.cnblogs.com/zxl2431/archive/2011/03/25/1995285.html 讲的很清楚,备份记录. (一) 用函数指针变量调用函数 可以用指 ...

  3. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  4. 2014/11/06 Oracle触发器初步 2014-11-06 09:03 49人阅读 评论(0) 收藏

    触发器我就不多解释了,保证数据的完整性的神器,嗯..也是减少程序员工作托管给数据库操作的好帮手.就不讲一些大道理了.通俗点,我们对数据库的操作,无非就是增 删 改 查. 触发器就是在删,改,增的时候( ...

  5. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

  6. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  7. ListView 分类: WinForm 2014-07-18 22:03 289人阅读 评论(0) 收藏

    一.ListView类(转载) 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLin ...

  8. RMAN之一:快速入门 分类: H2_ORACLE 2014-02-17 16:11 689人阅读 评论(0) 收藏

    1.数据导出基础 (1)创建datapump导出文件的目录对象并为相应用户授予权限. 出于安全考虑,不允许oracle用户直接在OS上进行文件的操作,而应通过directory对象指定. SQL> ...

  9. Oracle 字符集的查看和修改 分类: H2_ORACLE 2013-06-19 16:52 316人阅读 评论(0) 收藏

    一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库 ...

随机推荐

  1. 学习javascript怎么入门,初学者5条建议

    你是否已经初步掌握了html和css,但完全不知道从何入手Java?如果是,这里总结了5条建议,帮助JavaScript初学者总结学习方法,提高学习效率. 一.多看视频少看书 对初学者而言,看书的效率 ...

  2. Liunx cp

    功能: 复制文件或目录 使用权限:所有使用者说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同 ...

  3. spring+mybatis框架搭建时遇到Mapped Statements collection does not contain value for...的错误

    http://your233.iteye.com/blog/1563240 上面的博客总结了4个可能出现的问题,如下: 1.mapper.xml中没有加入namespace 2.mapper.xml中 ...

  4. swift 中使用weak self

    request.responseString(encoding: NSUTF8StringEncoding) {[weak self] (res) -> Void in if let stron ...

  5. idea maven 创建webapp项目没有src目录

    archetypeCatalog=internal

  6. 局外者看 -- 美团 vs 滴滴

    1. 美团 美团外面 美团打车 美团云服务 2. 滴滴 滴滴打车 滴滴外面 滴滴云服务 这是一场企业级别的战争,而且是本土战争,时间开始于何时,不知道,现在写这个博客的时间是2018年3月21日下午. ...

  7. 比较完整的HIS系统解释(转载记录)

    HIS系统即医院信息系统(全称为Hospital Information System).在国际学术界,它已被公认为是新兴的医学信息学的重要分支.HIS系统的有效运行,将提高医院各项工作的效率和质量, ...

  8. Fiddler建好代理后,能连到手机,但手机不能上网了是什么原因

    依次 tools(工具) >>   fiddler options(fiddler选项)   >>   connections( 连接) >>allow remot ...

  9. GDBT

    理论知识: 第四范式自主研发算法GBDT(Gradient Boosting Decision Tree) GBDT是一种基分类器为决策树的集成学习方法.决策树是一种常见的机器学习算法,GBDT中使用 ...

  10. 2018.11.01 NOIP训练 递增数列(迭代加深)

    传送门 直接迭代加深搜索. 发现每次最多增加一倍,最少增加一,于是果断上下界剪枝. 代码