poj2773 —— 二分 + 容斥原理 + 唯一分解定理
题目链接:http://poj.org/problem?id=2773
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 12942 | Accepted: 4557 |
Description
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
Output
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 —— 二分 + 容斥原理 + 唯一分解定理的更多相关文章
- POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)
Sumdiv Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- NOIP2009Hankson 的趣味题[唯一分解定理|暴力]
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- 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 ...
- 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...
- UVA 10375 Choose and divide【唯一分解定理】
题意:求C(p,q)/C(r,s),4个数均小于10000,答案不大于10^8 思路:根据答案的范围猜测,不需要使用高精度.根据唯一分解定理,每一个数都可以分解成若干素数相乘.先求出10000以内的所 ...
- 唯一分解定理 poj 1365
一行代表一个数 x 给你底数和指数 求x-1的唯一分解定理的底数和指数 从大到小输出 #include<stdio.h> #include<string.h> #include ...
- UVA294DIvisors(唯一分解定理+约数个数)
题目链接 题意:输入两个整数L,U(L <= U <= 1000000000, u - l <= 10000),统计区间[L,U]的整数中哪一个的正约数最多,多个输出最小的那个 本来 ...
- POJ1845Sumdiv(求所有因子和 + 唯一分解定理)
Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17387 Accepted: 4374 Descripti ...
随机推荐
- Jenkins自动化部署入门(一)
开始使用 Jenkins 这一段时间,技术总监为了减少测试环境每次提交新增接口都要部署项目的时间,搞了一个jenkins持续集成github.docker,这样只要每次push代码都会自动部署,确实节 ...
- System.Length 函数
function _PCharLen(P: _PAnsiChr): Longint;{$IFNDEF LEGACY_PCHARLEN}begin Result := 0; if P <> ...
- XCode 4.3 Unable to load persistent store UserDictionary.sqlite 以及 ios simulator failed to install the application
I have been working on an iOS app for some time, all of a sudden I am getting the following crash ev ...
- Android 源码编译记录
问题1:Can't locate Switch.pm in @INC (you may need to install the Switch module) (@INC contains: /etc/ ...
- socket阻塞与非阻塞,同步与异步I/O模型
作者:huangguisu 原文出处:http://blog.csdn.NET/hguisu/article/details/7453390 socket阻塞与非阻塞,同步与异步 1. 概念理解 在进 ...
- C#开发ActiveX控件,.NET开发OCX控件案例 【转】
http://xiaochen.2003.4.blog.163.com/blog/static/480409672012530227678/ 讲下什么是ActiveX控件,到底有什么作用?在网页中又如 ...
- C#复习总结6 (需要进一步复习)
第十七章 泛型 什么是泛型 泛型是为了适应多种不同种类的数据类型而存在的.有了它之后,我们可以不用为不同的数据类型而单独写一个适配.这样很麻烦. 类型不是对象,而是对象的模板.泛型类型也不是类型,而是 ...
- Google架构学习
http://hideto.iteye.com/blog/130815 原文:Google Architecture Google是伸缩性的王者.Google一直的目标就是构建高性能高伸缩性的基础组织 ...
- 内核顶层Makefile相关3
http://www.groad.net/bbs/simple/?f104.html 伪目标 .PHONY是一个特殊工作目标(special target),它用来指定一个假想的工作目标,也就是说它后 ...
- mysql (8.0 或以下)数据 卸载, 安装, 创建用户, 赋权
卸载 安装 创建用户wmxl create user 'wmxl'@'202.115.253.71' identified by '你的密码' 如果是mysql8.0,再输入以下 ALTER USER ...