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. [AngularJS] Angular 1.5 $transclude with named slot

    In Angular 1.5, there is no link and compile. So use if you transclude, you cannot access the fifth ...

  2. 解决从github下载web的源代码部署到eclipse的问题

    2015年6月2日 天气晴 github官网:https://github.com/ 以下以pdf.js作为案例说明:https://github.com/mozilla/pdf.js 1).点击案例 ...

  3. MAVEN Scope使用

    在Maven的依赖管理中,经常会用到依赖的scope设置.这里整理下各种scope的使用场景和说明,以及在使用中的实践心得.Scope的使用场景和说明1.compile编译范围,默认scope,在工程 ...

  4. (第三章)Java内存模型(下)

    一.happens-before happens-before是JMM最核心的概念.对于Java程序员来说,理解happens-before是理解JMM的关键. 1.1 JMM的设计 从JMM设计者的 ...

  5. 用MVC4练习,后台用aspx,数据库DemoDb《MvcUserDemo》

    将ado.net的cs文件SqlHelper.cs放入解决方案 using System; using System.Collections.Generic; using System.Linq; u ...

  6. ASP.NET MVC3.0或4.0设置二级域名的方法

    之前我就想做二级域名指向同一个IP同一个程序无非是在路由匹配规则上做文章也就是对Url的重写的一种思路.我用了半天时间上网查阅了相关资料并做了Demo测试是完全 以的,在这分享给大家... 假如网站主 ...

  7. ResourceDictionary 和 XAML 资源引用

    XAML 定义应用的 UI,并且 XAML 也可以定义 XAML 中的资源.资源通常是对你希望多次使用的某些对象的定义.你要为 XAML 资源定义一个键,以供将来引用,该键的作用类似于资源的名称.你可 ...

  8. Android-应用的本地化及知识拓展之配置修饰符

    步骤很简单,只需要两步: 1.创建带有目标语言的配置修饰符的资源子目录 2.将可选资源放入该目录下,android系统会自动处理后续工作 在这里我们需要讲解一下配置修饰符. 中文的配置修饰符:-zh, ...

  9. sql 字段字符串内容替换

    SELECT * FROM dbo.Table WHERE Name LIKE '%NYCL23%'UPDATE Table SET Name=replace(Name,'NYCL23','WYCL1 ...

  10. IDEA 运行 控制台显示一堆乱码,一些问号

    如下: 问题: 配置程序运行参数的时候,配置到了"VM options",应该为配置到"Program arguments",如图: