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. Qt使用QCustomPlot开发

    一.入门 1.下载源文件http://www.qcustomplot.com/: 2.把.cpp和.h放在工程目录下,并将cpp和h加入工程: 3.在.pro中:QT += printsupport: ...

  2. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. SPOJ1825 FTOUR2 - Free tour II

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. eclipse下进行c开发,使用zeromq

    使用eclipse开发代码,一段zeromq的代码. #include <string.h> #include <stdio.h> #include <unistd.h& ...

  5. ROS配置C++14环境

    ROS配置C++11环境 打开Terminal, sudo vim /opt/ros/<yourversion>/share/catkin/cmake/toplevel.cmake 比如: ...

  6. Codeforces 895C Square Subsets:状压dp【组合数结论】

    题目链接:http://codeforces.com/problemset/problem/895/C 题意: 给你n个数a[i].(n <= 10^5, 1 <= a[i] <= ...

  7. html5学习笔记(audio)

    来源于<HTML5高级程序设计> audio api <audio controls> controls告诉浏览器显示播放控件 不指定 type 浏览器自解 oggMP3 ty ...

  8. 子矩阵(暴搜(全排列)+DP)

    子矩阵(暴搜(全排列)+DP) 一.题目 子矩阵 时间限制: 1 Sec  内存限制: 128 MB 提交: 1  解决: 1 [提交][状态][讨论版] 题目描述 给出如下定义: 1. 子矩阵:从一 ...

  9. MDX中Filter 与Exist的区别

        获得一个集合,这个一般用来筛选出一个自定义的set,比如在中国的餐厅 该set返回所有MSDNteam下并且在Fact Thread度量上有记录的products 用Exists实现 sele ...

  10. java:jsp: 一个简单的自定义标签 tld

    java:jsp: 一个简单的自定义标签 tld 请注意,uri都是:http://www.tag.com/mytag,保持统一,要不然报错,不能访问 tld文件 <?xml version=& ...