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.

InputThe 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 <= 10
15) and (1 <=N <= 10
9).OutputFor 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}. 

首先分解质因子,然后利用容斥原理分别求出0—(A-1 )的不互质个数和0—(B)的不互质个数,答案可求。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
#define maxn 70 LL p[maxn];
LL make_ans(LL num,int m)//1到num中的所有数与m个质因子不互质的个数 注意是不互质哦
{
LL ans=0,tmp,i,j,flag;
for(i=1;i<(LL)(1<<m);i++)
{ //用二进制来1,0来表示第几个素因子是否被用到,如m=3,三个因子是2,3,5,则i=3时二进制是011,表示第2、3个因子被用到
tmp=1,flag=0;
for(j=0;j<m;j++)
if(i&((LL)(1<<j)))//判断第几个因子目前被用到
flag++,tmp*=p[j];
if(flag&1)//容斥原理,奇加偶减
ans+=num/tmp;
else
ans-=num/tmp;
}
return ans;
} int main()
{
int T,t=0,m;
LL n,a,b,i;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld",&a,&b,&n);
m=0;
for(i=2;i*i<=n;i++) //对n进行素因子分解
if(n&&n%i==0)
{
p[m++]=i;
while(n%i==0)
n/=i;
}
if(n!=1)
p[m++]=n;
printf("Case #%d: %I64d\n",++t,(b-make_ans(b,m))-(a-1-make_ans(a-1,m)));
}
return 0;
}

  

hdu_4135_Co-prime的更多相关文章

  1. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  2. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  3. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  4. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  5. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  6. hdu 5901 count prime & code vs 3223 素数密度

    hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...

  7. 最小生成树 prime zoj1586

    题意:在n个星球,每2个星球之间的联通需要依靠一个网络适配器,每个星球喜欢的网络适配器的价钱不同,先给你一个n,然后n个数,代表第i个星球喜爱的网络适配器的价钱,然后给出一个矩阵M[i][j]代表第i ...

  8. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  9. 最小生成树 prime + 队列优化

    存图方式 最小生成树prime+队列优化 优化后时间复杂度是O(m*lgm) m为边数 优化后简直神速,应该说对于绝大多数的题目来说都够用了 具体有多快呢 请参照这篇博客:堆排序 Heapsort / ...

  10. 最小生成树 prime poj1287

    poj1287 裸最小生成树 代码 #include "map" #include "queue" #include "math.h" #i ...

随机推荐

  1. jQuery阻止向上冒泡事件

    //阻止起泡取消下面的注释 e.stopPropagation(); //或者使用这种方式 //return false; }); $('.three').click(function(e){ ale ...

  2. Bootstrap + AngularJS+ Ashx + SQL Server/MySQL

    去年年底12月,为适应移动端浏览需求,花了1个月时间学习Bootstrap,并将公司ASP网站重构成ASP.NET. 当时采取的网站架构: Bootstrap + jQuery + Ashx + SQ ...

  3. ubuntu 18 设置语言环境

    1. 查看语言环境 ubuntu系统中,存在两个系统变量:$LANG和$LANGUAGE 分别控制语言环境和地区,这两个变量是从/etc/default/locale中读取的: 方法一: echo $ ...

  4. 【Markdown】Latex基本语法

    Latex基本语法 注意点:Markdown 斜杠/ 转义字符! LaTeX 是大神Leslie Lamport 的杰作,该神是2013年图灵奖的获得者,感兴趣可以去瞻仰一下神人的相关著述: http ...

  5. 获取当前时间CTime

    std::string getcurtime(){ USES_CONVERSION; CTime z_CurTime; CString z_TimeStr; z_CurTime = CTime::Ge ...

  6. 动态注册broadcast的安全考虑

    一.android service通知activity更新方式有1. service 通过广播的形式发送broadcast,向这个activity的内部类发广播的消息来更新界面2. service直接 ...

  7. 从尾到头打印链表(C++和Python 实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 输入一个链表的头结点, 从尾到头反过来打印出每个结点的值. ...

  8. 并发包Semaphore实现信号灯

    /** * * @描述: Semaphore实现信号灯 . * Semaphore可以维护当前访问自身的线程个数,并提供了同步机制,使用Semaphore可以控制同时访问资源的线程个数,例如实现一个文 ...

  9. MySQL数据操作(借鉴)

    /* 启动MySQL */net start mysql /* 连接与断开服务器 */mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */mysqld ...

  10. VSTO 开发中 应用ActionPane、CustomTaskPane

    以Excel插件为例: 1. ActionPane 创建 ThisWorkbook 项目 private void ThisWorkbook_Startup(object sender, System ...