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. JAVA 8.20 游戏:四子连(Java&C++)

    (游戏:四子连 )四子连是一个两个人玩的棋盘游戏,在游戏中,玩家轮流将有颜色的棋子放在一个六行七列的垂直悬挂的网格中:         这个游戏的目的是在对手实现一行.一列或者一条对角线上有四个相同颜 ...

  2. andorid 菜单 进度条

    activity_ui2.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  3. Thrift.1

    1. 依据thrift生成相对应语言的代码 [Ref]: http://wiki.apache.org/thrift/ThriftGeneration [Todo] 2. 如何使用生成的代码 [Ref ...

  4. XiaoKL学Python(E)Generator Expressions

    在 阅读 https://github.com/vitonzhang/objc_dep 中的 objc_dep.py 时遇到: objc_files = (f for f in files if f. ...

  5. 洛谷1993 小K的农场

    原题链接 裸的差分约束. \(X_a-X_b\geqslant C\) \(X_a-X_b\leqslant C\Rightarrow X_b-X_a\geqslant -C\) \(X_a-X_b\ ...

  6. Generate a Push Certificate

    To send Push notification to an application/device couple you need an unique device token (see the O ...

  7. AFNetworking HTTP响应头返回数据

    //发送验证码 NSLog(@"发送验证码"); AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationM ...

  8. JSP指令(page include taglib)

    JSP指令指示JSP转换器如何翻译JSP页面到Servlet:JSP指令用来设置整个JSP页面相关的属性,如网页编码方式.脚本语言等 JSP指令的格式: <%@ directive attrib ...

  9. IOS初级:导航控制器

    1.AppDelegate.m老生常谈了,创建window,创建根视图rootViewController - (BOOL)application:(UIApplication *)applicati ...

  10. 提升HTML5的性能体验系列之三 流畅下拉刷新和上拉

    下拉刷新 为实现下拉刷新功能,大多H5框架都是通过DIV模拟下拉回弹动画,在低端android手机(Android4.4以下)上,DIV动画经常出现卡顿现象(特别是图文列表的情况).解决方案还是web ...