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 ...
随机推荐
- c实现的trim函数
功能:去掉字符串首尾的空格,换行符等空白. 代码: #include <string.h> #include <stdio.h> #include <ctype.h> ...
- 利用例子来理解spring的面向切面编程
最近学习了spring的面向切面编程,在网上看到猴子偷桃的例子,觉得这种方式学习比书本上讲解有趣多了,也便于理解.现在就来基于猴子偷桃写个基本的例子. maven工程:
- HDU - 3664 Permutation Counting
Discription Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the amount of ...
- xcode5 asset catalogs 由于图标尺寸错误导致编译问题解决[原创]
如下图,即使图片尺寸不规范,xcode5也可以正常预览(这里我提供的尺寸是57*57, 而需要的是120*120) 但编译运行失败,报的错是: Images.xcassets: error: The ...
- 拦截器及 Spring MVC 整合
一.实验介绍 1.1 实验内容 本节课程主要利用 Spring MVC 框架实现拦截器以及 Spring MVC 框架的整合. 1.2 实验知识点 Spring MVC 框架 拦截器 1.3 实验环境 ...
- SocketIO总结
我在马克飞象上写的一样的内容,感觉那个样式好看的:WorkerMan的部分总结 workerman中部分函数总结 以下是把我搜集到的资料进行了一个整合.详细怎么使用.慢慢摸索吧. Worker类 中文 ...
- NYOJ 353 3D dungeon 【bfs】
题意:给你一个高L长R宽C的图形.每个坐标都能够视为一个方格.你一次能够向上.下.左,右,前,后任一方向移动一个方格, 可是不能向有#标记的方格移动. 问:从S出发能不能到达E,假设能请输出最少的移动 ...
- OpenGL之路(七)为立方体加入丰富色彩
在立方体的六个面贴上不同的颜色,假设想达到混合颜色的效果,能够參照立方体的前面代码在每一行前都加上颜色 代码例如以下 #include <gl/glut.h> #pragma commen ...
- Laravel 设置语言不生效的问题
使用了validate 验证,提示错误默认是 英文的.将en 改为zh-CN 后 运行 composer require "overtrue/laravel-lang:~3.0"时 ...
- 获取css信息
一般情况是用style直接获取css信息但是style只能获取到卸载行内的样式外链的和嵌入的样式会获取不到 2.5 用下面方法获取外链和嵌入的css样式 这种方法只能用于读取 window.getCo ...