poj2773 欧拉函数+整除性质
来源:互联网 发布:苹果mac系统安装教程 编辑:程序博客网 时间:2024/06/04 17:52
Happy 2006
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 10751 Accepted: 3744
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.
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 12006 22006 3
Sample Output
135
题意:求出与m互素的第k大的数
首先,介绍一个定理:若a与b不互素,则,tb+a与b也不互素,若a与b互素,则tb+a与b互素。所以,这个题目可以先求出m以内的与m互素的数,大于m的可以用l*m+a求出,其中a为小于m的与m互素的数,l为循环次数。
用欧拉函数求出循环长度,同时对m以内的素数(即大于m的素数%m的余数)进行标记
(吐槽一下,昨天用sublime打欧拉的模板打错了,在dev上改了过来,sublime上保存的依然是错的,今天拿着错误的模板wa了好多发……也是醉了)
#include <stdio.h>#include<string.h> long long m,k;int pan[1000008];long long phi(long long n){ long long m=n;long long i,cnt,j;cnt=n;for(i=2;i*i<=n;i++){ if(n%i==0) { for(j=i;j<=m;j=j+i)pan[j]=1; cnt=cnt-cnt/i; while(n%i==0)n=n/i; }}//printf("n:%lld\n",n);if(n>1){for(j=n;j<=m;j=j+n)pan[j]=1;cnt=cnt-cnt/n;} return cnt;}int main(int argc, char const *argv[]){ long long i,n;while(~scanf("%lld%lld",&m,&k)){ if(m==1){printf("%lld\n",k);continue;} memset(pan,0,sizeof(pan)); long long f=phi(m); // for(i=1;i<=m;i++)printf("%lld:%lld\n",i,pan[i]); long long t=(k-1)/f; long long p=(k-1)%f+1; long long cnt=0; for(i=1;i<=1000001;i++) { if(pan[i]==0)cnt++; if(cnt==p){cnt=i;break;} } long long sum=cnt+t*m; printf("%lld\n",sum); }return 0;}
0 0
- poj2773 欧拉函数+整除性质
- 欧拉函数性质
- 欧拉函数的性质
- 欧拉函数的性质
- 欧拉函数及其性质
- HDU2824【欧拉函数性质】
- 欧拉函数性质总结
- poj2773(欧拉函数的应用 或者 用容斥原理+dfs+二分)
- 欧拉函数及其部分性质
- 欧拉函数性质及介绍
- 欧拉函数以部分性质
- 关于欧拉函数的一个性质
- 欧拉函数及其部分性质
- 一个关于欧拉函数的性质
- 欧拉函数的性质及应用
- 欧拉函数及部分性质
- 欧拉函数一个神奇性质
- 关于欧拉函数的一个性质
- Android API Guides---Creating an Input Method
- html 框架frameset
- C++ 的内置函数(inline)和作用域运算符(::)
- SUSE Linux设置时区
- 五步学会XMLHttpRequest
- poj2773 欧拉函数+整除性质
- 关于AndroidMainfest中配置的知识
- Activity任务栈
- 保证分布式系统数据一致性的6种方案
- Redis入门
- java并发-Volatile关键字
- 1015 of search
- hadoop运行环境安装与配置+hadoop开发环境配置(一)
- 汇编-判断两个字符串是否相等