【poj2773】Happy 2006 欧几里德

题目描述:

分析:

根据欧几里德,我们有gcd(b×t+a,b)=gcd(a,b)

则如果a与b互质,则b×t+a与b也一定互质,如果a与b不互质,则b×t+a与b也一定不互质。

所以与m互质的数对m取模具有周期性,则根据这个方法我们就可以很快的求出第k个与m互质的数。

假设小于m的数且与m互质的数有l个,其中第i个是ai,则第k*l+i个与m互质的数是k*m+ai。

  所以,我就for一遍求出所有m以内的与m互质的数,然后根据周期性求解。(感觉有点暴力对吧)

代码如下,很短的:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 #define Maxn 1000010
 9 #define LL long long
10
11 int p[Maxn],len;
12
13 int gcd(int a,int b)
14 {
15     if(b==0) return a;
16     return gcd(b,a%b);
17 }
18
19 int main()
20 {
21     int m,k;
22     while(scanf("%d%d",&m,&k)!=EOF)
23     {
24         if(m==1) {printf("%d\n",k);continue;}
25         len=0;
26         for(int i=1;i<=m;i++) if(gcd(i,m)==1) p[++len]=i;
27         int ans;
28         if(k%len==0) ans=(k/len-1)*m+p[len];
29         else ans=k/len*m+p[k%len];
30         printf("%d\n",ans);
31     }
32     return 0;
33 }

poj2773

2016-02-05 16:21:38

时间: 2024-08-13 22:01:31

【poj2773】Happy 2006 欧几里德的相关文章

poj2773 Happy 2006

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9987   Accepted: 3434 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 al

POJ2773 Happy 2006【容斥原理】

题目链接: http://poj.org/problem?id=2773 题目大意: 给你两个整数N和K.找到第k个与N互素的数(互素的数从小到大排列).当中 (1 <= m <= 1000000,1 <= K <= 100000000 ). 解题思路: K非常大,直接从小到大枚举找出不现实,仅仅能二分答案.二分枚举[1.INF]范围内全部的数x, 找到1~x范围内与N互素的数个数.假设等于K,则就是结果. 然后考虑1~x范围内与N互素的数个数 = x - 1~x范围内与N不互素的

poj2773 Happy 2006(二分+容斥)

题目链接:点这里!!!! 题意: 给你两个整数m(1<=m<=1e6),k(1<=k<=1e8).求第k个与m互质的数是多少. 题解: 直接二分+容斥. 代码: #include<cstdio> #include<cstring> #include<iostream> #include<sstream> #include<algorithm> #include<vector> #include<bitse

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 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 a

【poj2773】 Happy 2006

http://poj.org/problem?id=2773 (题目链接) 题意:给出两个数m,k,要求求出从1开始与m互质的第k个数. Solution 数据范围很大,直接模拟显然是不行的,我们需要用到一些奇奇怪怪的方法. 考虑是否可以通过某些途径快速得到解,然而并没有头绪.正难则反,能不能通过计算不与m互质的数的个数来得到互质的数的个数呢?答案是可行的,我们可以运用容斥. 二分一个答案mid,容斥统计出在区间[1,mid]中是m的质因子的倍数的数的个数ans,然后我们可以用mid-ans得到

POJ 2773 Happy 2006(欧几里德算法)

题意:给出一个数m,让我们找到第k个与m互质的数. 方法:这题有两种方法,一种是欧拉函数+容斥原理,但代码量较大,另一种办法是欧几里德算法,比较容易理解,但是效率很低. 我这里使用欧几里德算法,欧几里德算法又名辗转相除法,原先单纯的用于求最大公约数,这里也算是一个小小的拓展应用,这个题利用的欧几里德算法的重要性质,假如a与b互质,那么b*t+a与b也一定互质,那样我们可以枚举1-m之间所有符合条件的数,然后打一个表格,求出所有符合条件的数,正如下表中的(5,5)所示,这个表格是一个带有周期性的自

【欧几里德算法】POJ2773-HAPPY 2006

[题目大意] 求与n互质的第k个数. [思路] 先求出小于k且与n互质的数,再利用gcd(bt+a,b)=gcd(a,b)的性质求解,效率低.枚举与n互质的数的效率是O(nlogn),求解第k个数的效率是O(1). 据说0ms做法是容斥+二分? 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 c

POJ2773---Happy 2006(容斥+二分)

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

poj 2773欧几里德

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5957   Accepted: 1833 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 al