题目链接:

http://poj.org/problem?id=2773

题目大意:

给你两个整数N和K。找到第k个与N互素的数(互素的数从小到大排列)。当中

(1 <= m <= 1000000,1 <= K <= 100000000 )。

解题思路:

K非常大,直接从小到大枚举找出不现实,仅仅能二分答案。二分枚举[1。INF]范围内全部的数x,

找到1~x范围内与N互素的数个数。假设等于K,则就是结果。

然后考虑1~x范围内与N互素的数个数 = x - 1~x范围内与N不互素的数个数

1~x范围内与N不互素的数个数用简单的容斥定理来求就可以。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL __int64
using namespace std;
const LL INF = 0xfffffff0; int Prime[1000010],ct,N; void Divide()
{
ct = 0;
int n = N;
for(int i = 2; i <= sqrt(n*1.0); ++i)
{
if(n % i == 0)
{
Prime[ct++] = i;
while(n % i == 0)
n /= i;
}
}
if(n != 1)
Prime[ct++] = n;
} LL Solve(int n)
{
LL ans = 0;
for(int i = 1; i < (1 << ct); ++i)
{
LL odd = 0;
LL tmp = 1;
for(int j = 0; j < ct; ++j)
{
if((1 << j) & i)
{
odd++;
tmp *= Prime[j];
}
}
if(odd & 1)
ans += n/tmp;
else
ans -= n/tmp;
}
return n - ans;
} int main()
{
int K;
while(~scanf("%d%d",&N,&K))
{
Divide();
LL Left = 1, Right = INF, Mid, tmp;
while(Left < Right) //二分答案
{
Mid = (Left + Right) >> 1;
tmp = Solve(Mid);
if(tmp >= K)
Right = Mid;
else
Left = Mid + 1;
}
printf("%I64d\n",Left); } return 0;
}

最新文章

  1. Oracle环境变量NLS_LANG
  2. http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html
  3. 使用phpize安装php模块
  4. C++开源小贱鸡(simsimi api)
  5. javascript----bug
  6. nginx编译配置
  7. 邮件发送 emailsend .net开发
  8. Angularjs 滚动条控制
  9. webstorm11.0.3连接ftp
  10. Elasticsearch创建索引和映射结构详解
  11. Android-AndroidManifest.xml默认启动的Activity(探索篇01)
  12. Lucene.net 全文检索数据库
  13. 让 IE6, 7和 8支持CSS3的HTC文件补丁
  14. I/O Completion Ports
  15. LNMP-day2-进阶
  16. Codeforces 806 D.Prishable Roads
  17. Macro definition of snprintf conflicts with Standard Library function declaration
  18. fatal error C1083: 无法打开包括文件:“stdint.h”: No such file or directory
  19. 「新手向」koa2从起步到填坑
  20. kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU4300 Clairewd’s message

热门文章

  1. jquery 移动端 六位密码输入
  2. ALTER GROUP - 修改一个用户组
  3. 08C#事件
  4. shell 循环 read line
  5. CSU——2161: 漫漫上学路 最短路
  6. &lt;Linux&gt; 下安装和卸载JDK
  7. IO之转换流举例
  8. db2,差集
  9. UVA 213 信息解码(二进制&amp;位运算)
  10. Leetcode 220.存在重复元素III