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大的数是多少

先算个x=phi(n),所以1到n内有x个数跟n互质

再讨论大于n的数:

对于y=tn+x,当x与n互质的时候,x也和n的所有因子互质。任取一个因子s,x%s != 0,那么(nt+x)%s != 0,所以y%s != 0,所以没有一个n的因子整除y,y和n互质

对于y=tn+x,当x与n不互质的时候,令s=gcd(x,n),s|x,则s|(tn+x),则s|y,所以y和n也不互质

所以y=tn+x跟n是否互质,可以转化为x跟n是否互质

所以在1~n有phi[n]个数跟n互质,n+1~2n有phi[n]个数跟n互质……

所以先给phi[n]取个模,因为m比较小点,所以剩下的直接暴力找到第k大的就行了

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
bool mk[];
int p[],len;
int phi[];
int s[],len2;
bool pp[];
inline void work()
{
LL cur1=m%phi[n],cur2=(m-)/phi[n];
if (n==){printf("%d\n",m);return;}
if (!cur1)cur1=phi[n];
if (n<=)for (int i=;i<=n;i++)pp[i]=;
else memset(pp,,sizeof(pp));
len2=;
int t=n;
for (int i=;i<=len;i++)
{
if (p[i]*p[i]>t)break;
if (t%p[i]==)
{
s[++len2]=p[i];
while (t%p[i]==)t/=p[i];
}
}
if (t!=)s[++len2]=t;
int now=;
for (int i=;i<=n;i++)
{
if (now<=len2&&s[now]==i)
{
for(int j=*i;j<=n;j+=i)pp[j]=;
now++;
}
else if (!pp[i])cur1--;
if (cur1==){printf("%lld\n",cur2*n+i);return;}
}
}
inline void getp()
{
for (int i=;i<=;i++)phi[i]=i;
for (int i=;i<=;i++)
if (!mk[i])
{
phi[i]=i-;
for (int j=*i;j<=;j+=i)mk[j]=,phi[j]=phi[j]/i*(i-);
p[++len]=i;
}
}
int main()
{
getp();
while (~scanf("%d%d",&n,&m))work();
}

poj2773

[暑假集训--数论]poj2773 Happy 2006的更多相关文章

  1. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  2. [暑假集训--数论]hdu1019 Least Common Multiple

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  3. [暑假集训--数论]poj2115 C Looooops

    A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...

  4. [暑假集训--数论]poj1365 Prime Land

    Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...

  5. [暑假集训--数论]poj2034 Anti-prime Sequences

    Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...

  6. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  7. [暑假集训--数论]poj2262 Goldbach's Conjecture

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...

  8. [暑假集训--数论]poj2909 Goldbach's Conjecture

    For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...

  9. [暑假集训--数论]poj3518 Prime Gap

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...

随机推荐

  1. vmware:使用.zip文件在vmware中安装操作系统

    问题描述: 之前在vmware中安装系统时,全部都是加载的.iso文件来实现.后面同事给了一个zip包,解压后是".vmdk"等一系列具体的文件.一时间不知道怎么安装系统了,搜网页 ...

  2. Binary Agents-freecodecamp算法题目

    Binary Agents 1.要求 传入二进制字符串,翻译成英语句子并返回. 二进制字符串是以空格分隔的. 2.思路 用.split(' ')将输入二进制字符串转化为各个二进制数字符串组成的数组 用 ...

  3. 4396: [Usaco2015 dec]High Card Wins

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 275  Solved: 175[Submit][Status][Discuss] Descriptio ...

  4. dom 添加删除节点

    //找到 div1 var div1 = document.getElementById("div1"); //创建 一个 p标签 var p = document.createE ...

  5. python 实现无序列表

    # -*- coding:utf-8 -*- class Node: def __init__(self, initdata): self.data = initdata self.next = No ...

  6. PHP namespace、abstract、interface、trait使用介绍

    小菜鸟一枚,一直搞不懂 namespace.abstract.interface.trait 这些关系,就抽出几天时间研究,做个总结,不足之处希望大家指正交流. namespace 命名空间 介绍:顾 ...

  7. Python 列表元素分组,比如 [1,2,3,...20]变成 [[1,2,3],[4,5,6]....](列表生成式解决)

    # 生成一个1到20的列表 a=[x for x in range(1,21)] # 把a列表切片,并赋值给b列表,x为0到20且步长为3的列表,在这里具体为[0,3,6,9,12,15,18] # ...

  8. Android通过AIDL和反射调用系统拨打电话和挂断电话

    首先在项目中添加ITelephony.aidl文件,我的如下: /* * Copyright (C) 2007 The Android Open Source Project * * Licensed ...

  9. 第三模块 面向对象& 网络编程基础 实战考核

    1.简述构造方法和析构方法. 构造方法(__init__):主要作用是实例化时给实例一些初始化参数,或执行一些其它的初始化工作,总之因为这个__init__只要一实例化, 就会自动执行,不管你在这个方 ...

  10. daily algorithm 判断链表是否有环

    public static boolean isLoopLink(ListNode head) { if (head == null) { return false; } ListNode fast ...