题目链接: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. 【ZJOI2017】树状数组

    题目描述 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的 OI 比赛经历.那是一道基础的树状数组题. 给出一个长度为 $n$ 的数组 $A$,初始值都为 $0$,接下来 ...

  2. Android 打开其他程序

    Intent intent = new Intent(); intent.setComponent(new ComponentName("所要打开的程序包名", "所要打 ...

  3. vbox在共享文件夹设置链接报错Protocol error问题

    环境: 基于VBox 的 vagrant (centos版本)开发环境. 问题: Virtualbox 虚拟机(centOS)中,在进行go程序编译的时候,需要设置一个链接符,然后得到了如下的错误: ...

  4. 谈oracle数据比对(DBMS_COMPARISON)

    今天是2014-08-19,我今天收到csdn给我发的申请博客专家的邀请,自己感觉实在羞愧啊. 自从换了工作也一直没有精力在写点东西了.今天我一个同事,在群里贴出了一个数据比对的包(DBMS_COMP ...

  5. Effective JavaScript Item 54 将undefined视为&quot;没有值&quot;

    将undefined视为"没有值" JavaScript中的undefined是一个特殊的值:当JavaScript没有提供详细的值时.它就会产生undefined. 比方: 未被 ...

  6. PS 如何使用抽出滤镜抠人物的头发丝等细节

    1.打开图片,复制背景,关闭背景眼睛.单击 滤镜 -抽出, 我们要学会观察图片,先来看下面这张图: 这张图片色彩虽然不算丰富,但也不是纯色背景,甚至有些许的零乱,但人物的主题却被黑色长发包围, 我们只 ...

  7. JavaScript-4.7-friendly_table---ShinePans

    <html> <head> <meta http-equiv="content-type" content="text/html;chars ...

  8. 猫猫学iOS之小知识之_xcode插件的删除方法_自己主动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自己主动提示,

    猫猫分享,必须精品 原创文章.欢迎转载. 转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:解决解决自己主动提示图片插件KSImageNamed有时不 ...

  9. STM32串行通信USART解说笔记

    STM32串行通信USART程序例举链接:http://blog.csdn.net/dragon12345666/article/details/24883111 1.STM32串行通信USART的相 ...

  10. 完美删除vector的内容与释放内存

    问题:stl中的vector容器常常造成删除假象,这对于c++程序员来说是极其讨厌的,<effective stl>大师已经将之列为第17条,使用交换技巧来修整过剩容量.内存空洞这个名词是 ...