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. 解决织梦替换ueditor编辑器后栏目与单页无法保存内容

    织梦的默认编辑器是ckeditor,没有插入代码的功能,不够强大,就换成了百度的UEditor编辑器. 使用织梦DedeCMS百度整合UEditor编辑器时,碰到了栏目内容或者单页无法保存的问题,这个 ...

  2. 前端面试问题html汇总

    1.对WEB标准以及W3C的理解与认识 结构层 html :表示层  css:行为层:js; 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索机率.使用外链css和js脚本.结构行为表现的分离.文件下 ...

  3. Expressions入门示例

    学习表达式的入门例子,前提是要对委托有一定的了解,泛型明白一些.using System; using System.Linq; using System.Linq.Expressions; usin ...

  4. 室内设计类网站Web原型制作分享——Dinzd

    Dinzd是一家德国室内设计网站,网站内涵盖全球设计精品资讯以及优秀案列.网站布局简单直观,内容丰富. 此原型模板所用到的交互动作有结合弹出面板做下拉菜单效果,鼠标按下文字按钮跳转页面,按钮hover ...

  5. No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal

    No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in t ...

  6. contenteditable设置元素可编辑

    需求背景 实现一个输入框,高度可以随着输入文字的增加而自动增高 有placeholder,输入为空时,显示placeholder 我们知道可以将div的contenteditable设置伪true,将 ...

  7. android源码下载/查看地址

    源码下载: http://git.omapzoom.org/ 高通平台android源码下载地址: https://www.codeaurora.org/xwiki/bin/QAEP/WebHome ...

  8. 【搜索】C - Catch That Cow

    #include<stdio.h> #include<string.h> struct A{ int state; int step; }queue[]; // 结构体数组用来 ...

  9. 【Linux】DNS服务-BIND从服务器、缓存服务器及转发服务器配置(三)

    环境 操作系统:CentOS 6.5 DNS软件:bind(安装参照:[Linux]DNS服务-BIND基础配置(二)) BIND从服务器 从服务器就是在bind的主配置文件中添加从域example. ...

  10. SpringMVC 学习 九 SSM环境搭建 (二) Spring配置文件的编写

    spring配置文件中需要干的事情 (一)开启  Service与pojo包的注解扫描 注意:spring 扫描与表对应的实体类,以及service层的类,不能用来扫描Controller层的类,因为 ...