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 ...
随机推荐
- codevs贪吃的九头龙
传说中的九头龙是一种特别贪吃的动物.虽然名字叫“九头龙”,但这只是说它出生的时候有九个头,而在成长的过程中,它有时会长出很多的新头,头的总数会远大于九,当然也会有旧头因衰老而自己脱落.有一天,有M 个 ...
- Java中泛型T和Class<T>以及Class<?>的理解(转)
注意:class是java的关键字, 在声明Java类时使用; Class类的实例表示Java应用运行时的类(class ans enum)或接口(interface and annotation)( ...
- 邁向IT專家成功之路的三十則鐵律 鐵律二:IT專家專業之道–專精
在IT技術的領域當中有許多的類別,若要細分那可真是難以一一列舉,但常見的大致有軟體研發工程師.韌體研發工程師.系統分析師.網路工程師.系統工程師.維護工程師.動畫設計師.製圖工程師.以及各類別的專業電 ...
- SilverLight:基础控件使用(3)-DataGrid控件
ylbtech-SilverLight-Basic-Control:基础控件使用(3)-DataGrid控件 DataGrid控件-后台绑定 自动生成表列 不自动生成表列 1.A,返回顶部Person ...
- String空格删除和java删除字符串最后一个字符的几种方法
1. String.trim()trim()是去掉首尾空格2.str.replace(" ", ""); 去掉所有空格,包括首尾.中间复制代码 代码如下:Str ...
- apollo 消息分发源代码分析
1.MessageDispatch消息分发信息 public static final byte DATA_STRUCTURE_TYPE = CommandTypes.MESSAGE_DISPATCH ...
- 完美删除vector的内容与释放内存
问题:stl中的vector容器常常造成删除假象,这对于c++程序员来说是极其讨厌的,<effective stl>大师已经将之列为第17条,使用交换技巧来修整过剩容量.内存空洞这个名词是 ...
- sum-root-to-leaf-numbers——dfs
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- Hdu-1565 方格取数(1) (状态压缩dp入门题
方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- pyqt5 学习总结
关于基类 一般的文件都会基于QWidget,QtWidgets.QMainWindow 或QDialog,like this class Example(QWidget): QWidget类是所有用户 ...