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 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

题目大意就是给出n和k求出第k个与n互素的数

如果知道欧几里德算法的话就应该知道gcd(b×t+a,b)=gcd(a,b)  (t为任意整数)

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

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

假设小于m的数且与m互素的数有k个,其中第i个是ai,则第m×k+i与m互素的数是k×m+ai

 #include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int pri[];
int gcd ( int a , int b )
{
return b == ? a : gcd ( b , a % b ) ;
}
int main()
{
freopen("in.txt","r",stdin);
int m , k ;
while ( cin >> m >> k )
{
int i , j ;
for ( i = , j = ; i <= m ; i ++ )
if ( gcd ( m , i ) == )
pri [ j ++ ] = i ; if ( k%j != )
cout <<k/j * m +pri[k%j-] << endl;
else//要特别考虑k%j=0的情况,因为数组是从0开始的,第i个对应的是pri[i-1]
cout << (k/j-)*m+pri[j-] << endl ;
}
return ;
}

poj 2773欧几里德的更多相关文章

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

    题意:给出一个数m,让我们找到第k个与m互质的数. 方法:这题有两种方法,一种是欧拉函数+容斥原理,但代码量较大,另一种办法是欧几里德算法,比较容易理解,但是效率很低. 我这里使用欧几里德算法,欧几里 ...

  2. POJ 2773 Happy 2006 数学题

    题目地址:http://poj.org/problem?id=2773 因为k可能大于m,利用gcd(m+k,m)=gcd(k,m)=gcd(m,k)的性质,最后可以转化为计算在[1,m]范围内的个数 ...

  3. POJ 2773 Happy 2006#素数筛选+容斥原理+二分

    http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...

  4. poj 2773 Happy 2006 - 二分答案 - 容斥原理

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11161   Accepted: 3893 Description Two ...

  5. [poj 2773] Happy 2006 解题报告 (二分答案+容斥原理)

    题目链接:http://poj.org/problem?id=2773 题目大意: 给出两个数m,k,要求求出从1开始与m互质的第k个数 题解: #include<algorithm> # ...

  6. POJ 2773 Happy 2006【GCD/欧拉函数】

    根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...

  7. poj 2773(容斥原理)

    容斥原理入门题吧. Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9798   Accepted: 3 ...

  8. D - C Looooops POJ - 2115 欧几里德拓展

    题意:就是看看for(; ;)多久停止. 最让我蛋疼的是1L和1LL的区别!让我足足wa了12发! 1L 是long类型的, 1LL为long long类型的! 思路: 这就是欧几里德扩展的标准式子了 ...

  9. POJ 2773 Happy 2006------欧几里得 or 欧拉函数。

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8359   Accepted: 2737 Descri ...

随机推荐

  1. Java面试通关要点汇总集(山东数漫江湖)

    这里,笔者结合自己过往的面试经验,整理了一些核心的知识清单,帮助读者更好地回顾与复习 Java 服务端核心技术.本文会以引出问题为主,后面有时间的话,笔者陆续会抽些重要的知识点进行详细的剖析与解答.敬 ...

  2. 【洛谷 P4934】 礼物 (位运算+DP)

    题目链接 位运算+\(DP\)=状压\(DP\)?(雾 \(a\&b>=min(a,b)\)在集合的意义上就是\(a\subseteq b\) 所以对每个数的子集向子集连一条边,然后答案 ...

  3. FZUOJ 2205 据说题目很水 (无三元环图最大边数)

    Problem Description Sunday最近对图论特别感兴趣,什么欧拉回路什么哈密顿回路,又是环又是树.在看完一本书后,他对自己特别有信心,便找到大牛牛犇犇,希望他出一题来考考自己. 在遥 ...

  4. bzoj 2786 DP

    我们可以将=左右的两个数看成一个块,块内无顺序要求,把<分隔的看成两个块,那么我们设w[i][j]代表将i个元素分成j个块的方案数,那么显然w[i][j]=w[i-1][j]*j+w[i-1][ ...

  5. CursorFileManager对cursor文件的读写

    public class CursorFileManager implements CursorManager{public void write(String key, LongCursor cur ...

  6. ew做socks5代理

    这个工具和之前讲过的xxoo类似.链接:https://www.cnblogs.com/nul1/p/8883271.html https://zhuanlan.zhihu.com/p/3282215 ...

  7. Vue组件-使用插槽分发内容

    在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer></app-fo ...

  8. 64_c2

    coin-or-Bcp-1.4.3-3.fc26.i686.rpm 22-May-2017 21:07 250866 coin-or-Bcp-1.4.3-3.fc26.x86_64.rpm 22-Ma ...

  9. quazip 在windows msvc 2005 下的编译

    quazip 在windows  msvc 2005 下的编译 http://blog.csdn.net/v6543210/article/details/11661427

  10. make :err Makefile.ssl is older than Makefile.org. Reconfigure the source tree (via './config' or 'perl Configure'), please.

    内核编译时出现错误 Makefile.ssl is older than Makefile.org. Reconfigure the source tree (via './config' or 'p ...