Happy 2006
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9049   Accepted: 3031

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

  1. 2006 1
  2. 2006 2
  3. 2006 3

Sample Output

  1. 1
  2. 3
  3. 5
  4.  
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5. #include<iostream>
  6. #include<algorithm>
  7. using namespace std;
  8. #define maxn 10000010
  9. bool a[maxn];
  10. int euler(int n)
  11. {
  12. int i,j,m=n,ann=n;
  13. memset(a,,sizeof(a));
  14. int size=sqrt(m+0.5);
  15. for(i=; i<=size; i++)
  16. {
  17. if(n%i==)
  18. {
  19. ann=ann/i*(i-);
  20. for(j=i; j<=m; j+=i)a[j]=;
  21. while(n%i==)n/=i;
  22. }
  23. }
  24. if(n>)
  25. {
  26. ann=ann/n*(n-);
  27. for(j=n; j<=m; j+=n)a[j]=;
  28. }
  29. return ann;
  30. }
  31. int main()
  32. {
  33. int n,k,i,m,t,q;
  34. while(~scanf("%d%d",&n,&k))
  35. {
  36. m=euler(n);
  37. if(k%m==)
  38. {
  39. t=k/m-;
  40. }else t=k/m;
  41. k=k-m*t;
  42. q=;
  43. for(i=;i<=n;i++)
  44. {
  45. if(!a[i])q++;
  46. if(q==k)break;
  47. }
  48. printf("%lld\n",(long long)i+n*t);
  49. }
  50. }

Happy 2006 poj2773的更多相关文章

  1. [暑假集训--数论]poj2773 Happy 2006

    Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD ...

  2. 【poj2773】 Happy 2006

    http://poj.org/problem?id=2773 (题目链接) 题意 给出两个数m,k,要求求出从1开始与m互质的第k个数. Solution 数据范围很大,直接模拟显然是不行的,我们需要 ...

  3. 【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 ...

  4. POJ-2773 Happy 2006,暴力2700ms+水过!

                                                         Happy 2006 这个题很可能会超时的,但我几乎暴力的方法2700ms+过了,可能是后台水 ...

  5. [POJ2773]:Happy 2006

    传送门 同样是欧拉函数的基本应用. $\phi (N)$表示$[1,N]$中,$gcd(i,N)==1$的数的个数,同理,其也能表示$[K \times N+1,(K+1) \times N]$中$g ...

  6. POJ2773 - Happy 2006(欧拉函数)

    题目大意 给定两个数m,k,要求你求出第k个和m互质的数 题解 我们需要知道一个等式,gcd(a,b)=gcd(a+t*b,b) 证明如下:gcd(a+t*b,b)=gcd(b,(a+t*b)%b)= ...

  7. POJ2773 Happy 2006【容斥原理】

    题目链接: http://poj.org/problem?id=2773 题目大意: 给你两个整数N和K.找到第k个与N互素的数(互素的数从小到大排列).当中 (1 <= m <= 100 ...

  8. BZOJ 2006: [NOI2010]超级钢琴

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2613  Solved: 1297[Submit][Statu ...

  9. #Deep Learning回顾#之2006年的Science Paper

    大家都清楚神经网络在上个世纪七八十年代是着实火过一回的,尤其是后向传播BP算法出来之后,但90年代后被SVM之类抢了风头,再后来大家更熟悉的是SVM.AdaBoost.随机森林.GBDT.LR.FTR ...

随机推荐

  1. switch处理多分支结构

    import java.util.Scanner; /** * Created by liwenj on 2017/7/17. */ public class test9 { public stati ...

  2. java HashSet改用

    写的一个Student类如下: 上面是直接使用的HashSet集合,系统会把new Student()  当做地址不用来出来,所以结果如下: 然后我在Student类中重写了hashCode()和eq ...

  3. 基于moco的mock server 简单应用 来玩玩吧

    提起mock大家应该就知道是干嘛用的了,再次再介绍一种简单的方式,基于moco的mock server.步骤很简单: 1. 首先,要下载个moco的jar0_1482402640757_moco-ru ...

  4. C语言运算符运算顺序判断实例2

    #include <stdio.h> int main(void) { , j = , k = ; printf("%d\n", ++i || ++j &&am ...

  5. Python有哪些高大上的项目?

    ​Python作为程序员的宠儿,得到了越来越多人的关注,使用Python进行应用程序开发的越来也多.那么,Python有哪些高大上的项目?这里有十个: 1.NuPIC 它是一个以HTM学习算法为工具的 ...

  6. zookeeper简单介绍

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt193 ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系 ...

  7. Docker - Failed to connect to localhost port 4000: Connection refused

    转载.翻译自 https://stackoverflow.com/questions/44014698/docker-failed-to-connect-to-localhost-port-4000- ...

  8. 转:深入Java集合学习系列:HashMap的实现原理

    1. HashMap概述: HashMap是基于哈希表的Map接口的非同步实现(Hashtable跟HashMap很像,唯一的区别是Hashtalbe中的方法是线程安全的,也就是同步的).此实现提供所 ...

  9. css预处理器less和scss之less介绍(一)

    第一次发的标题有误,重发一遍,抱歉了 一.less基础语法 1.声明变量:@变量名:变量值 使用变量:@变量名 例如 @color : #ff0000; @length : 100px; #div1{ ...

  10. 201521123105 第8周Java学习总结

    1.本周学习总结 1.1思维导图 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 1.删除元素的时候从最后一个元素开始,避免删除元素后位置发生变化而导致 ...