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

最新文章

  1. Atitti 载入类的几种方法 &#160;&#160;&#160;Class.forName&#160;ClassLoader.loadClass&#160;&#160;直接new
  2. OpenCV阶段总结扩充。
  3. jqGrid实现当前页列合计与总计
  4. Wordpress模板制作、改造、设计
  5. python 输出乱码
  6. ASP.NET MVC Model验证总结【转】
  7. BZOJ1996 合唱队 区间DP
  8. Common Converters in WPF/Silverlight
  9. #include&lt; &gt; 和 #include” ” 的区别
  10. CLI-error
  11. Scala的Option类型
  12. VNC 黑屏
  13. 一个大学生屌丝心中的seo梦
  14. Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(三)
  15. Python下划线的使用
  16. Mybatis第五篇【Mybatis与Spring整合】
  17. 在ubuntu16.04中一键创建LAMP环境
  18. BZOJ 1069: [SCOI2007]最大土地面积 [旋转卡壳]
  19. fastclick原理剖析及其用法
  20. CodeForces - 1051D Bicolorings(DP)

热门文章

  1. 一个gpio 不受控制的bug
  2. Eclipse Task的使用
  3. C#中利用WebBrowser控件,获得HTML源码
  4. Spring中操作Hibernate的几种方式
  5. java基础(2)-面向对象(2)
  6. R语言笔记004——R批量读取txt文件
  7. codeforces766E Mahmoud and a xor trip(按位统计+树形DP)
  8. 不理解use explanatory variables
  9. 【转载】ORA-12519: TNS:no appropriate service handler found 解决
  10. SSIS包的组建之连接管理器