Happy 2006
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 11458   Accepted: 4001

Description

Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are all relatively prime to 2006.

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

The input contains multiple test cases. For each test case, it contains two integers m (1 <= m <= 1000000), K (1 <= K <= 100000000).

Output

Output the K-th element in a single line.

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 ;
}

容斥原理+二分.

容斥原理介绍:http://baike.baidu.com/link?url=H0UEe3zE2jUT7Ree_tycNyXcLYRWH4v25KpCZ3DOcx2HN0jaMYB3rJNF45SFs_EDxWo01C0LCz1rrh-_CG4On_

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(容斥原理)的更多相关文章

  1. poj2773 —— 二分 + 容斥原理 + 唯一分解定理

    题目链接:http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  2. POJ2773 Happy 2006【容斥原理】

    题目链接: http://poj.org/problem?id=2773 题目大意: 给你两个整数N和K.找到第k个与N互素的数(互素的数从小到大排列).当中 (1 <= m <= 100 ...

  3. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  4. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  5. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  6. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

  7. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  8. HDU5838 Mountain(状压DP + 容斥原理)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...

  9. 【BZOJ-2669】局部极小值 状压DP + 容斥原理

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 561  Solved: 293[Submit][Status ...

随机推荐

  1. 机器学习相关知识整理系列之二:Bagging及随机森林

    1. Bagging的策略 从样本集中重采样(有放回)选出\(n\)个样本,定义子样本集为\(D\): 基于子样本集\(D\),所有属性上建立分类器,(ID3,C4.5,CART,SVM等): 重复以 ...

  2. 2018.7.12训练赛 -G

    第二道水题 前边说了很多话,但就最后两段有用. 就是给你一个序列,然后你判断一下这个序列是不是递增的,是就输出yes,否则输出no. 所以以后不管题目看起来多长.多复杂,都要读一遍. 代码就不贴了.

  3. 使用Navicat连接oracle时出现unsupported server character set ZHS16GBK的解决之道

    原文网址http://blog.mn886.net/chenjianhua/show/ba1dc6f835be403ea159b0a5e2685ff2/index.html ORA-12737:Ins ...

  4. 创建表空间及用户的SQL

    --创建表SOFA空间: CREATE SMALLFILE TABLESPACE "SOFA" DATAFILE 'G:\oracle\product\10.2.0\ORADATA ...

  5. 总结django知识点

    一.视图函数:     请求对象-----------request:           1.HttpRequest.body:         请求原数据           2.HttpRequ ...

  6. 关于synchronized关键字

    1.synchronized关键字的作用域有二种: 1)是某个对象实例内,synchronized aMethod(){}可以防止多个线程同时访问这个对象的synchronized方法(如果一个对象有 ...

  7. 刻录DVD.Win7系统盘(U盘)

    ZC:Win7x86的U盘安装盘做好之后,U盘 里面会留有 引导信息,在以后不想要它(引导信息)的时候 该如果将它删掉?直接普通的格式化 能行吗? ZC:(20180423)发现,UltraISO制作 ...

  8. jquery——简单的下拉列表制作及bind()方法的示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 不一样的控制面板 GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}

    这是一个快速打开所有控制面板选项的方法.被称作Gode Mode或者Master Control Panel. 步骤很简单: 复制:超级控制面板.{ED7BA470-8E54-465E-825C-99 ...

  10. hdu 2509 Be the Winner(anti nim)

    Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...