Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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}.

分析:求出n的素因子,然后容斥求解出不互质的个数,剩下的就是互质的个数;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,cnt,fac[maxn],cas;
ll x,y;
void init(int x)
{
cnt=;
if(x%==){
fac[++cnt]=;
while(x%==)x/=;
}
for(int i=;(ll)i*i<=x;i+=)
{
if(x%i==)
{
fac[++cnt]=i;
while(x%i==)x/=i;
}
}
if(x>)fac[++cnt]=x;
}
ll gao(ll x)
{
ll ret=;
for(int i=;i<(<<cnt);i++)
{
ll num=,now=;
for(int j=;j<cnt;j++)
{
if(i&(<<j))
{
++num;
now*=fac[j+];
}
}
if(num&)ret+=x/now;
else ret-=x/now;
}
return x-ret;
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%d",&x,&y,&n);
init(n);
printf("Case #%d: %lld\n",++cas,gao(y)-gao(x-));
}
return ;
}
 

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. oop_day02_类、重载_20150810

    oop_day02_类.重载_20150810 1.怎样创建类?怎样创建对象? 2.引用类型之间画等号: 家门钥匙 1)指向同一个对象(数据有一份) 2)对当中一个引用的改动.会影响另外一个引用 基本 ...

  2. 【BZOJ 2565】 最长双回文串

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2565 [算法] Manacher [代码] #include<bits/std ...

  3. 机器学习——Day 2 简单线性回归

    写在开头 由于某些原因开始了机器学习,为了更好的理解和深入的思考(记录)所以开始写博客. 学习教程来源于github的Avik-Jain的100-Days-Of-MLCode 英文版:https:// ...

  4. Electron桌面应用:环境搭建

    目录 一.简介 二.市场 三.安装 1.安装node版本 2.安装国内的npm包管理器 3.安装electron 4.验证electron否安装成功 5.使用git克隆一个electron简单的项目, ...

  5. HTML 打印 换页

    打印 HTML 无法强制换页其实是一件很令人困扰的事,要达到这个功能其实可以透过 CSS 的 Pagebreak 来处理. 强制分页有大概只有二种用的到: { page-break-after: al ...

  6. JDK介绍

    Java的版本最开始是1995年的JDK Alpha and Beta版本,第二年发布JDK1.0版本之后就是JDK1.1,JDK1.2.到1998年,不再叫JDK了,而是叫J2SE,但是版本号还是继 ...

  7. Asp.net MVC访问框架页中嵌套的iframe页面时,如果session或cookie过期,登录验证超时怎样自动跳转到登录页

    一般登录验证的过滤器中,使用验证过滤器的Redirect方法,将请求重定向到指定的URL.但是如果我们要访问的页面是一个嵌套在框架页中的iframe页面时,这种重定向只会对iframe页面凑效,也就是 ...

  8. Android截图截取弹框AlertDialog

    1:效果如图 2:权限 <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" ...

  9. Linux系统下通过命令行对mysql数据进行备份和还原

    一.备份 1.进入mysql目录 cd /var/lib/mysql (进入mysql目录,根据安装情况会有差别) 2.备份 mysqldump -u root -p密码 数据库名 数据表名 > ...

  10. 微信小程序播放背景音乐

    小程序实现和h5一样的音乐图标一直旋转. 一..js中封装旋转动画方法 添加animation属性 data:{ animation:''" } 改变animation的值(官网提供角度范围 ...