POJ2773(容斥原理)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 11458 | Accepted: 4001 |
Description
Now your job is easy: for the given integer m, find the K-th element which is relatively prime to m when these elements are sorted in ascending order.
Input
Output
Sample Input
2006 1
2006 2
2006 3
Sample Output
1
3
5
思路:若a与m互素,那么a+t*m(t>=1)与m 也互素,否则不互素.设小于m且与m互素的数有n个,分别为a(0),a(1),a(2),...,a(n-1).那么第n+1个为a0+m,第n+2个为a(1)+m...第k个为m*(k-1)+a((k-1)%n);
#include <cstdio>
using namespace std;
const int MAXN=;
int m,k;
int relative[MAXN],top;
int gcd(int a,int b)
{
if(b==) return a;
else return gcd(b,a%b);
}
void sieve()
{
for(int i=;i<=m;i++)
{
if(gcd(i,m)==)
{
relative[top++]=i;
}
}
}
int main()
{
while(scanf("%d%d",&m,&k)!=EOF)
{
top=;
sieve();
int n=(k-)/top;
int z=(k-)%top;
int res=n*m+relative[z];
printf("%d\n",res);
}
return ;
}
容斥原理+二分.
n/p表示1~n中是p倍数的数的个数。求1~m中与n互素的数的个数。先将n进行质因数分解,然后通过位运算枚举所有质因数的组合。若选了奇数个质因数ans+=m/质因数之积,否则ans-=m/质因数之积。然后二分枚举m的范围,确定k.
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
LL sieve(LL n,LL m)
{
vector<LL> divisor;
for(LL i=;i*i<=n;i++)
{
if(n%i==)
{
divisor.push_back(i);
while(n%i==) n/=i;
}
}
if(n>) divisor.push_back(n);
LL ans=;
for(LL mark=;mark<(<<divisor.size());mark++)
{
LL mul=;
LL odd=;
for(LL i=;i<divisor.size();i++)
{
if(mark&(<<i))
{
odd++;
mul*=divisor[i];
}
}
LL cnt=m/mul;
if(odd&) ans+=cnt;
else ans-=cnt;
}
return m-ans;
}
LL n,k;
int main()
{
while(scanf("%lld%lld",&n,&k)!=EOF)
{
LL left=;
LL right=1LL<<;
while(right-left>)
{
LL mid=(left+right)>>;
LL cnt=sieve(n,mid);
if(cnt>=k)
{
right=mid;
}
else
{
left=mid;
}
}
printf("%lld\n",right);
}
return ;
}
Java版:
import java.util.Scanner;
import java.util.ArrayList;
public class Main{
Scanner in = new Scanner(System.in);
long m, k;
long sieve(long n, long m)
{
ArrayList<Long> divisor = new ArrayList();
for(long i = ; i * i <= n; i++)
{
if(n % i == )
{
divisor.add(i);
while(n % i == ) n /= i;
}
}
if(n > ) divisor.add(n);
long ret = ;
for(long mark = , size = divisor.size(); mark < ( << size); mark++)
{
long odd = ;
long mul = ;
for(int i = ; i < size; i++)
{
if((mark & (1L << i)) != )
{
odd++;
mul *= divisor.get(i);
}
}
if(odd % == )
{
ret += m / mul;
}
else
{
ret -= m / mul;
}
}
return m - ret;
}
Main()
{
while(in.hasNext())
{
m = in.nextLong();
k = in.nextLong();
long left = , right = 1L << ;
while(right > left)
{
long mid = (right + left) >> ;
long s = sieve(m, mid);
if(s >= k)
{
right = mid;
}
else
{
left = mid + ;
}
}
System.out.println(right);
}
}
public static void main(String[] args){ new Main();
}
}
POJ2773(容斥原理)的更多相关文章
- poj2773 —— 二分 + 容斥原理 + 唯一分解定理
题目链接:http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- POJ2773 Happy 2006【容斥原理】
题目链接: http://poj.org/problem?id=2773 题目大意: 给你两个整数N和K.找到第k个与N互素的数(互素的数从小到大排列).当中 (1 <= m <= 100 ...
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3028 Solved: 1460[Submit][Sta ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- HDU5838 Mountain(状压DP + 容斥原理)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...
- 【BZOJ-2669】局部极小值 状压DP + 容斥原理
2669: [cqoi2012]局部极小值 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 561 Solved: 293[Submit][Status ...
随机推荐
- Go goroutine (协程)
在Go语言中goroutine是一个协程,但是跟Python里面的协程有很大的不同: 在任何函数前只需要加上go关键字就可以定义为协程; 不需要在定义时区分是否是异步函数 VS async def ...
- 吴恩达深度学习笔记(十二)—— Batch Normalization
主要内容: 一.Normalizing activations in a network 二.Fitting Batch Norm in a neural network 三.Why does ...
- Linux 配置 SSL 证书
完整的 SSL 证书分为四个部分: CA 根证书 (root CA) 中级证书 (Intermediate Certificate) 域名证书 证书密钥 (仅由您持有) 以 COMODO Positi ...
- Elasticsearch6.4.3安装
Linux内存一定要1g以上! 首先要有jdk环境 要求1.8版本以上 elasticsearch是Java写的 将上传的 elasticSearch安装包解压 cd /home/elastics ...
- PHP开发框架
利用PHP开发框架可以帮助你编写干净和可重用的代码.PHP开发框架遵循MVC设计模式,以确保能够明确区分逻辑和演示文稿.但是有关PHP框架的争论也不少,这是因为有的人喜欢性能,有的人喜欢文档,而有的人 ...
- 【P3572】little bird(单调队列+DP)
一眼看上去这个题就要DP,可是应该怎么DP呢,我们发现,数据范围最多支持O(NlogN),但是这种DP貌似不怎么有,所以应该是O(N)算法,自然想到单调队列优化DP. 然后我们先考虑如果不用单调队列应 ...
- springboot - web项目
一:使用Thymeleaf:参考http://blog.csdn.net/u012702547/article/details/53784992#t0 1.1 引入相应的包 1.2 thymelea ...
- 防域名DNS劫持 从保护帐号安全做起
什么攻击能造成区域性的网络瘫痪?没错,DNS劫持.这个堪称核武器的攻击方式,一旦爆炸,后果不堪设想.2014年1月21日,全国大范围出现DNS故障,下午,中国顶级域名根服务器出现故障,大部分网站受影响 ...
- JS实现选项卡切换效果
1.在网页制作过程中,我们经常会用到选项卡切换效果,它能够让我们的网页在交互和布局上都能得到提升 原理:在布局好选项卡的HTML结构后,我们可以看的出来,选项卡实际上是三个选项卡标头和三个对应的版块, ...
- 修改SpringBoot 默认的小叶子图标
Springboot 项目,在浏览器中访问时,浏览器上导航栏的图标是一片绿色的叶子,我们可以修改它. 将格式为.ico的图片放入以下任一项目文件夹即可.但,图片命名必须为favicon.ico 1.类 ...