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. DockPanelSuite中的DocumentStyle

    首先明确一个概念Mdi的完整词组:Multiple document interface namespace WeifenLuo.WinFormsUI.Docking { public enum Do ...

  2. zzulioj--1730--通信基站(全排列+dfs)(好题)

    1730: 通信基站 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 28  Solved: 11 SubmitStatusWeb Board Desc ...

  3. 浅谈自学Python之路(购物车程序练习)

    购物车程序练习 今天我们来做一个购物车的程序联系,首先要理清思路 购物车程序需要用到什么知识点 需要用到哪些循环 程序编写过程中考虑值的类型,是int型还是字符串 如果值为字符串该怎么转成int型 用 ...

  4. [NOI2015,LuoguP2146]软件包管理器------树剖

    ***题目链接戳我*** 又是在树上瞎搞滴题目.... 我们如果以安装的软件为1,未安装的软件为0,那么软件改变的数量即树上权值总和的数量,涉及到区间修改,区间查询,考虑树剖 分析完毕,似乎没啥好说的 ...

  5. C - Gravity Flip

    Problem description Little Chris is bored during his physics lessons (too easy), so he has built a t ...

  6. # Nginx常见问题

    try_files的使用 按顺序检查文件是否 存在 location /{ try_files $uri $uri/ /index.php } 解析:在/下寻找$uri,如果没有找到,则去找$uri/ ...

  7. sleep()和wait()的区别

    1 sleep()方法,我们首先要知道该方法是属于Thread类中的.而wait()方法,则是属于Object类中的. 2 Thread.sleep和Object.wait都会暂停当前的线程,对于CP ...

  8. 用Python+selenium打开IE浏览器和Chrome浏览器的问题

    这几天在学Python+selenium自动化,对三大浏览器Firefox,Chrome和IE都做了尝试,也都分别下载了对应的webdriver,如:geckodriver.chromedriver. ...

  9. ICCV2015上的GazeTracker论文总结

    SLAM问题先慢慢编译一段时间,赶紧拾起来GazeTrack的事情...... ICCV2015的大部分paper已经可以下载,文章列表在这个位置. http://www.cvpapers.com/i ...

  10. promise原理及使用方法

    Promise 的含义 所谓Promise ,简单说就是一个容器,里面保存着某个未来才回结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消息. re ...