Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2307    Accepted Submission(s): 861

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
 

题意:求A到B之间的数有多少个与n互质。

首先转化为(1---B)与n互质的个数减去(1--- A-1)与n互质的个数

然后就是求一个区间与n互质的个数了,注意如果是求(1---n)与n互质的个数,可以用欧拉函数,但是这里不是到n,所以无法用欧拉函数。

这里用到容斥原理,即将求互质个数转化为求不互质的个数,然后减一下搞定。

求互质个数的步骤:

1、先将n质因数分解

2、容斥原理模板求出不互质个数ans

3、总的个数减掉不互质个数就得到答案

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
#define ll long long
#define N 1000000
ll A,B,n;
vector<ll> v;
ll solve(ll x,ll n)
{
v.clear();
for(ll i=;i*i<=n;i++) //对n进行素数分解
{
if(n%i==)
{
v.push_back(i);
while(n%i==)
n/=i;
}
}
if(n>) v.push_back(n); ll ans=;
for(ll i=;i<( <<v.size() );i++)//用二进制来1,0来表示第几个素因子是否被用到,如m=3,三个因子是2,3,5,则i=3时二进制是011,表示第2、3个因子被用到
{
ll sum=;
ll tmp=;
for(ll j=;j<v.size();j++)
{
if((<<j)&i) //判断第几个因子目前被用到
{
tmp=tmp*v[j];
sum++;
}
}
if(sum&) ans+=x/tmp;//容斥原理,奇加偶减
else ans-=x/tmp;
}
return x-ans;
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d",&A,&B,&n);
printf("Case #%d: ",++ac);
printf("%I64d\n",solve(B,n)-solve(A-,n));
}
return ;
}
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
#define N 1000000
ll A,B,n;
ll fac[N];
ll solve(ll x,ll n)
{
ll num=;
for(ll i=;i*i<=n;i++)
{
if(n%i==)
{
fac[num++]=i;
while(n%i==)
n/=i;
}
}
if(n>) fac[num++]=n; ll ans=;
for(ll i=;i<(<<num);i++)
{
ll sum=;
ll tmp=;
for(ll j=;j<num;j++)
{
if((<<j)&i)
{
tmp=tmp*fac[j];
sum++;
}
}
if(sum&) ans+=x/tmp;
else ans-=x/tmp;
}
return x-ans;
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d",&A,&B,&n);
printf("Case #%d: ",++ac);
printf("%I64d\n",solve(B,n)-solve(A-,n));
}
return ;
}

hdu 4135 Co-prime(容斥)的更多相关文章

  1. HDU 4135 Co-prime(容斥+数论)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

  3. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

  4. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

  5. HDU 5297 Y sequence 容斥 迭代

    Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...

  6. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  7. HDU 3970 Harmonious Set 容斥欧拉函数

    pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n  求连续整数[0,n), 中随意选一些数使得选出的 ...

  8. [HDU4135]CO Prime(容斥)

    也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...

  9. HDU 4609 3-idiots FFT+容斥

    一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完 ...

  10. HDU 4336 Card Collector(容斥)

    题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...

随机推荐

  1. lesson8:AtomicInteger源码解析及性能分析

    AtomicInteger等对象出现的目的主要是为了解决在多线程环境下变量计数的问题,例如常用的i++,i--操作,它们不是线程安全的,AtomicInteger引入后,就不必在进行i++和i--操作 ...

  2. 性能计数器自动收集-logman

    1.在桌面云测试中,往往需要模拟并发连接中服务器的性能数据,这里主要介绍如何自动收集性能数据 2.创建xxxx.bat文件,文件内容如下: logman create counter test -cf ...

  3. OC与JS互相调用

    近期项目中要用到html5来实现.涉及到OC调用JS,以及JS调用OC的方法.这里把遇到的问题以及实现方法介绍一下. // // ViewController.h // OC_And_JS // // ...

  4. VS2010调试小技巧

    在VS下做开发的时候我们进行调试的时候路径是这个样子的:http://localhost:端口号/项目名称/index.aspx 但是发布到服务器上面的时候却是这个样子的:http://www.xxx ...

  5. android高仿微信UI点击头像显示大图片效果

    用过微信的朋友朋友都见过微信中点击对方头像显示会加载大图,先贴两张图片说明下: 这种UI效果对用户的体验不错,今天突然有了灵感,试着去实现,结果就出来了.. 下面说说我的思路: 1.点击图片时跳转到另 ...

  6. Java基础知识强化67:基本类型包装类之Integer直接赋值的面试题

    1. 面试题: Integer  i = 1: i += 1: 做了哪些事情? (1)其中Integer i =1:做了自动装箱( 使用valueOf()方法,int ---> Integer ...

  7. 使用Teleport Pro离线下载网页所有内容

    在学习生活中,碰到网页中内容太多,如何讲其保存到本地,已方便随时查看呢? 使用Teleport Pro就可以解决问题:     首先下载Teleport Pro V1.54 汉化绿色版的,解压完之后 ...

  8. 打印对象和toString方法

    JAVA对象 java对象是都是Object类的实例,都可直接调用该类中定义的方法,这些方法提供了处理java对象的通用方法. > > 6.2.1打印对象和toString方法    先看 ...

  9. How to start the Virtualbox VMs under terminal

    Since we have servral machine on my testbed(fedora), and if I need start some VMs on that, I have to ...

  10. java下socket传文件

    package cn.stat.p4.ipdemo; import java.io.BufferedReader; import java.io.BufferedWriter; import java ...