[暑假集训--数论]poj2773 Happy 2006
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
Output
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的更多相关文章
- [暑假集训--数论]hdu2136 Largest prime factor
Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...
- [暑假集训--数论]hdu1019 Least Common Multiple
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...
- [暑假集训--数论]poj2115 C Looooops
A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...
- [暑假集训--数论]poj1365 Prime Land
Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...
- [暑假集训--数论]poj2034 Anti-prime Sequences
Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...
- [暑假集训--数论]poj1595 Prime Cuts
A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...
- [暑假集训--数论]poj2262 Goldbach's Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...
- [暑假集训--数论]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 ...
- [暑假集训--数论]poj3518 Prime Gap
The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...
随机推荐
- Solaris&&QNX® Neutrino®&&OpenVMS&&FreeBSD&&AIX
原文链接Solaris (读作 /se'laris:/ 或者 /so'le:ris/ 或者 '梭拉瑞斯' )是Sun Microsystems研发的计算机 操作系统.它被认为是UNIX操作系统的衍生版 ...
- GCH文件
GCH文件是将H文件当作CPP进行编译之后出现的结果, 在头文件进行编译后就会在文件夹中看到一个 “文件名.h.gch” 的文件. 那么在再次对gch文件进行编译的时候就会将gch当作cpp一样对待. ...
- 魅族MX3 Flyme3.0找回手机功能支持远程拍照密码错两次自动拍照
进入Flyme页面(http://app.meizu.com/),选择“查找手机”即可进行查找自己登记的魅族系列手机. 如果您在一个账号下登记过N多魅族系列手机,那么都是可以进行查找的,参见下图 魅族 ...
- G++ 编译多个源文件
g++ -c *.cpp g++ graph.o maxflow.o test.o -o test // 链接顺序必须写对
- JZOJ 5185. 【NOIP2017提高组模拟6.30】tty's sequence
5185. [NOIP2017提高组模拟6.30]tty's sequence (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB ...
- 20.VUE学习之-变异方法push的留言版实例讲解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python While循环、运算符以及一些基础运用
1.循环语句 循环打印"人生苦短,我用python" while True: print("人生苦短,我用python") 利用While循环,打印1~10 c ...
- 内容提供器(Content Provider)
一个跟数据库很相似的用于与其他程序传递信息的组件,用的也是数据库的CRUD操作 相关权限 注册内容提供者以及权限 <provider android:name=".ContentRes ...
- 4 Template层-验证码
1.验证码 在用户注册.登录页面,为了防止暴力请求,可以加入验证码功能,如果验证码错误,则不需要继续处理,可以减轻一些服务器的压力 使用验证码也是一种有效的防止crsf的方法 验证码效果如下图: 官网 ...
- cf976f Minimal k-covering
枚举 \(k\),对于每个点 \(i\) 我们最多删 \(deg_i-k\) 条边,就源点向第一部.第二部向汇点连边,容量是 \(deg_i-k\),原边连上,容量是 \(1\),这样每流过一条原边在 ...