题目链接:

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

POJ2773 Happy 2006【容斥原理】的更多相关文章

  1. [暑假集训--数论]poj2773 Happy 2006

    Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD ...

  2. poj 2773 Happy 2006 容斥原理+二分

    题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #incl ...

  3. POJ-2773 Happy 2006,暴力2700ms+水过!

                                                         Happy 2006 这个题很可能会超时的,但我几乎暴力的方法2700ms+过了,可能是后台水 ...

  4. [POJ2773]:Happy 2006

    传送门 同样是欧拉函数的基本应用. $\phi (N)$表示$[1,N]$中,$gcd(i,N)==1$的数的个数,同理,其也能表示$[K \times N+1,(K+1) \times N]$中$g ...

  5. POJ2773 - Happy 2006(欧拉函数)

    题目大意 给定两个数m,k,要求你求出第k个和m互质的数 题解 我们需要知道一个等式,gcd(a,b)=gcd(a+t*b,b) 证明如下:gcd(a+t*b,b)=gcd(b,(a+t*b)%b)= ...

  6. poj 2773 Happy 2006 - 二分答案 - 容斥原理

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11161   Accepted: 3893 Description Two ...

  7. POJ2773(容斥原理)

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

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

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

  9. 【poj2773】 Happy 2006

    http://poj.org/problem?id=2773 (题目链接) 题意 给出两个数m,k,要求求出从1开始与m互质的第k个数. Solution 数据范围很大,直接模拟显然是不行的,我们需要 ...

随机推荐

  1. C#在Excel的簡單操作--適用:與DB數據的簡單交互

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  2. 用cesium本身添加水纹效果

    参考网站:https://blog.csdn.net/XLSMN/article/details/78752669 1.首先来看一下整体效果 2.具体方法如下: 首先,你必须有两张很重要的图片,你可以 ...

  3. thisInObject

    var k = { name:"kName" ,getName:function(){ console.info(this.name) } ,getParentName:funct ...

  4. C#override与new修饰隐藏的区别(转载)

    C#比java多一个new隐藏的功能.C# override重写相当于java中没有关键字的方法重写.所以java中方法是没有隐藏功能的. C# override重写,是指对父类中的虚方法(标记vir ...

  5. anchor_target_layer中的bounding regression

    在anchor_target层,这两行是计算bounding regression代码: bbox_targets = np.zeros((len(inds_inside), 4), dtype=np ...

  6. python msg_box

    转自:http://www.cnblogs.com/otfsenter/ # _*_ coding: utf-8 _*_ # @Time : 2017/3/27 17:39 # @Author : o ...

  7. 2018湖南省第14届大学生计算机程序设计竞赛 D: 卖萌表情

    Description 已知以下 4 种都是卖萌表情(空白的部分可以是任意字符.竖线是便于展示的分隔符,没有实际意义): ^ ^ | ^ | < | > v | v v | > | ...

  8. 【design pattern】工厂方法模式和抽象工厂模式

    前言 设计模式分为三大类: 创建型模式:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式: 结构型模式:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式: 行为型模式 ...

  9. mysql出现问题汇总(持续更新)

    1.mysql -uqwe -p普通用户登陆时提示: ERROR 1045 (28000): Access denied for user 'baijie'@'%' (using password: ...

  10. heroku安装(win7x64)

    Jdk安装:官网地址 http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html下载要安装的jdk版本. ...