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. iOS 图像处理 - 图像拼接

    解决这个问题:将两个图像拼接在一起 前提:须要加入Framework:CoreGraphics.framework 源代码: - (UIImage *) combine:(UIImage*)leftI ...

  2. OTA制作及升级过程笔记【转】

    本文转载自:http://www.it610.com/article/5752570.htm 1.概述 1.1   文档概要 前段时间学习了AndroidRecovery模式及OTA升级过程,为加深理 ...

  3. 第14章 Wi-Fi系统应用 14.1 了解Wi-Fi系统的结构

    Android平台中Wi-Fi系统从上到下主要包括Java框架类.Android适配器库.wpa_supplicant守护进程.驱动程序和协议,这几部分的系统结构如图14-3所示. (1)Wi-Fi用 ...

  4. PCB MS SQL 排序应用---SQL相邻数据区间值求解

    其中一篇 博文中有写<PCB MS SQL 排序应用---相邻数据且相同合并处理>此篇有也应相用也同的技巧,实现相邻数据区间值求解 示例: 原数据:处理前 求出区间值:处理后 SQL 代码 ...

  5. DIV+CSS设计时浏览器兼容性

          近期用Div+css做了个企业网站,在浏览器中测试的时候确发现在IE7中显示正常的页面,在ie6中非常混乱,当时第一感觉就想到了兼容问题,可是百思不得其解应该从哪下手,经过一两天的查资料, ...

  6. HDU 1166 线段树模板&树状数组模板

    HDU1166 上好的线段树模板&&树状数组模板 自己写的第一棵线段树&第一棵树状数组 莫名的兴奋 线段树: #include <cstdio> using nam ...

  7. wap网测一道题目

    1. 给定一个字符串s, 1 <= len(s) <= 3000, 定义odd palindrome string为长度为奇数的回文串, 求s中该奇回文串的个数. 比如axbcba , 结 ...

  8. VS2015 右侧导航插件地址

    右侧导航插件: https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ProductivityPowe ...

  9. 重装系统后快速安装.NET 3.5

    每一次重装系统(Windows 8.1 和Windows 10)之后,最让我头疼的一件事就是配置把一大堆软件装上了.通常我会装好SQL Server之后,把电脑放在工作组安装Visual Studio ...

  10. .net core2.0 中使用DB First

    一.新建一个控制台测试项目 1.1.添加引用 1.2.修改项目文件 1.3.添加红框的内容 <ItemGroup> <DotNetCliToolReference Include=& ...