题目链接:http://poj.org/problem?id=2773

Happy 2006
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12942   Accepted: 4557

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

Source

题意:

求第k个与m互质的数。

题解:

方法一:

如果 gcd(a,b) = 1, 那么成a与b互质。 注:1和任何数都互质

根据唯一分解定理: 对一个数进行因式分解, 最终必定能分解为多个素数相乘的式子, 且这个式子是唯一的(1除外)。

1.那么我们可以m进行素数分解, 并记录它由哪些素数构成。
如果一个数的分解式中不含有构成m的素数, 那么这个数就与m互素。

2.然后二分答案ans, 如果在ans范围之内, 有>=k个与m互素的数, 那么就缩小范围, 否则扩大范围。

3.那么怎么知道在ans范围内, 有多少个数有m互素呢? 用容斥原理。

方法二:

可知 gcd(a+b*k, b) = gcd(b, a%b), gcd(a, b) = gcd(b, a%b), 所以 gcd(a+b*k, b) = gcd(a, b), k为常数。

这表明了:对于与b互素的数,他们对b取模的余数会周期性出现。 那么我们就只需要计算出在b的范围内, 与b互素的数有哪些就可以了。

然后看第k个与b互素的数是在第几个周期的第几个就可以了。(注意:刚好在周期末时, 需要特判)。

方法一:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e5+; LL m, k;
LL fac[maxn], sum; void getFactor()
{
sum = ;
LL tmp = m;
for(LL i = ; i*i<=tmp; i++)
if(tmp%i==)
{
fac[sum++] = i;
while(tmp%i==) tmp /= i;
}
if(tmp>) fac[sum++] = tmp;
} LL test(LL tmp)
{
if(m==) return tmp;
if(tmp==) return ; LL ret = ;
for(LL s = ; s < (<<sum); s++)
{
LL p = , cnt = ;
for(LL j = ; j<sum; j++)
if(s&(<<j))
{
p *= fac[j];
cnt++;
}
ret += (cnt&)?(tmp/p):(-tmp/p);
}
return tmp - ret;
} int main()
{
while(scanf("%lld%lld",&m,&k)!=EOF)
{
getFactor();
LL l = k, r = LNF;
while(l<=r)
{
LL mid = (l+r)>>;
if(test(mid)>=k)
r = mid - ;
else
l = mid + ;
}
printf("%lld\n", l);
}
return ;
}

方法二:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e6+; int pri[maxn]; int gcd(int a, int b)
{
return b==?a:gcd(b, a%b);
} int main()
{
int m, k, sum;
while(cin>>m>>k)
{
sum = ;
for(int i = ; i<=m; i++)
if(gcd(i,m)==)
pri[sum++] = i; if(k%sum)
cout<< (k/sum)*m+pri[(k%sum-)] <<endl;
else
cout<< (k/sum-)*m+pri[sum-] <<endl;
}
return ;
}

poj2773 —— 二分 + 容斥原理 + 唯一分解定理的更多相关文章

  1. POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

    Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  2. NOIP2009Hankson 的趣味题[唯一分解定理|暴力]

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...

  3. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  4. uva10375 Choose and Divide(唯一分解定理)

    uva10375 Choose and Divide(唯一分解定理) 题意: 已知C(m,n)=m! / (n!*(m-n!)),输入整数p,q,r,s(p>=q,r>=s,p,q,r,s ...

  5. 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...

  6. UVA 10375 Choose and divide【唯一分解定理】

    题意:求C(p,q)/C(r,s),4个数均小于10000,答案不大于10^8 思路:根据答案的范围猜测,不需要使用高精度.根据唯一分解定理,每一个数都可以分解成若干素数相乘.先求出10000以内的所 ...

  7. 唯一分解定理 poj 1365

    一行代表一个数 x 给你底数和指数 求x-1的唯一分解定理的底数和指数 从大到小输出 #include<stdio.h> #include<string.h> #include ...

  8. UVA294DIvisors(唯一分解定理+约数个数)

    题目链接 题意:输入两个整数L,U(L <= U <= 1000000000, u - l <= 10000),统计区间[L,U]的整数中哪一个的正约数最多,多个输出最小的那个 本来 ...

  9. POJ1845Sumdiv(求所有因子和 + 唯一分解定理)

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17387   Accepted: 4374 Descripti ...

随机推荐

  1. Noip2017赛前的一些记录

    前言 已经退役整整五个月了....选考以后终于又摸上了键盘.... 但是码力已经大不如前了........ 距离比赛也就只有一星期了....那就胡乱的做一些题目吧QAQ 这里是一些根据算法分类的咋杂题 ...

  2. 深入理解Thread构造函数

    上一篇快速认识线程 本文参考汪文君著:Java高并发编程详解. 1.线程的命名 在构造现成的时候可以为线程起一个名字.但是我们如果不给线程起名字,那线程会有一个怎样的命名呢? 这里我们看一下Threa ...

  3. 1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  4. redis-bitmap 命令使用的一些帖子

    https://segmentfault.com/a/1190000009841792?utm_source=tag-newest http://blog.csdn.net/lglgsy456/art ...

  5. DozerBeanMapper + 对象转Map方法

    1.简介     dozer是一种JavaBean的映射工具,类似于apache的BeanUtils.但是dozer更强大,它可以灵活的处理复杂类型之间的映射.不但可以进行简单的属性映射.复杂的类型映 ...

  6. 2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

    2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh ...

  7. 利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model

    利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model   使用场景:网站配置项目,为了便于管理,网站有几个Model类来管理配置文件, 比如ConfigWebsiteMo ...

  8. Controller//控制器

    #include<opencv2\core\core.hpp> #include<opencv2\imgproc\imgproc.hpp> #include<opencv ...

  9. 运维基础-Linux发展史、安装、基本操作

    Linux是目前互联网运维.大数据.云计算方向首选操作系统平台,能够在物理服务器Dell.hp.等server,以及当前主流的云平台,阿里云,腾讯云上面部署 发展史 . . .略过..... 物理服务 ...

  10. 宜人贷蜂巢API网关技术解密之Netty使用实践

    一.背景 宜人贷蜂巢团队,由Michael创立于2013年,通过使用互联网科技手段助力金融生态和谐健康发展.自成立起一直致力于多维度数据闭环平台建设.目前团队规模超过百人,涵盖征信.电商.金融.社交. ...