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. 限定checkbox最多选中数量

    一.概述: checkbox是我们在编写网页的时候经常使用的多选框,但是有些时候我们会限定最多选中的数量,如何限定呢? 下面这例子限定了最多选中两个元素,并且将这两个选中的源依次显示在一个文本框里: ...

  2. java 存储oracle的clob字段

    项目中有很长的字符创需要存储,用到了oracle的clob字段,直接很长的字符串插入到clob字段中会报字符过长的异常,于是便寻求解决方案.看到这个博客写的还不错 首先,创建一个含CLOB字段的表: ...

  3. FusionChart实现金字塔分布图

    1.XML提供数据源 Pyramid.xml: <?xml version="1.0" encoding="UTF-8"?> <chart m ...

  4. linux命令帮助

    Linux命令格式:command [options] [arguments]command:命令options: 参数 [] 表示是可选的;<> 表示是可变化的; x|y|z 表示只能选 ...

  5. c语言数组小练习

    //查找数组中最大的值: #include<stdio.h> int main01() { , , , , , , , , , ,,}; ]; int i; ;i < ]);i++) ...

  6. Mac下如何不借助第三方工具实现NTFS分区的可写挂载

    问题背景 我想很多使用Mac的同学都会遇到读写NTFS磁盘的问题,因为默认情况下Mac OSX对NTFS磁盘的挂载方式是只读(read-only)的,因此把一个NTFS格式的磁盘插入到Mac上,是只能 ...

  7. (转)Phonegap VS AppCan

    简介 Phonegap PhoneGap是一个用基于HTML,CSS和JavaScript的,创建移动跨平台移动应用程序的快速开发平台.它使开发者能够利用iPhone,Android,Palm,Sym ...

  8. Ganglia 监控Hadoop

    Ganglia监控Hadoop集群的安装部署 一. 安装环境 Ubuntu server 12.04 安装gmetad的机器:192.168.52.105 安装gmond的机 器:192.168.52 ...

  9. Swift中的便利构造器和构造器链

    import UIKit // 1.一个类中至少有一个指定构造器, 其必须负责初始化类中所有的实例存储属性 // 2.便利构造器属于次要的, 辅助性的构造器 // 3.类中可以不定义便利构造器, 便利 ...

  10. Assimp场景模型输出Collada,STL,3DPDF

    本文介绍开源库模型的几种输出格式:DAE,STL,3DPDF. Assimp是C++写的,AssimpNet是C#重构其中主要数据结构,并开通Assimp中重要方法的调用接口,为不熟悉C++的码农带来 ...