[暑假集训--数论]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 ...
随机推荐
- Ubuntu解决winscp连接不上虚拟机问题
前几天在配置虚拟机的时候,尝试用winscp连接Ubuntu,结果连接被拒绝.原因:Ubuntu的ssh服务需要自己安装和启动,在没启动之前,是无法连接上去的 解决方案: 我们可以输入:ssh loc ...
- Oracle 函数 之 Coalesce()、greatest()、least()
Coalesce().greatest().least() oracle比较一列的数据大小时,我们一般使用max()/min()函数,比较一行的最大值或者最小值时,使用函数Coalesce()/gre ...
- java基础必备单词讲解 day one
computer 电脑 computer path 路径 配置jdk环境 class 类 classpath 类路径 编译好的文件执行路径 public 公共的 private 私有的 static ...
- 牛客小白月赛1 G あなたの蛙は旅⽴っています【图存储】【DP】
题目链接:https://www.nowcoder.com/acm/contest/85/G 思路: DP 空间可以优化成一维的, 用一维数组的 0 号单元保存左斜对角的值即可. 存图这里真不好理解 ...
- C/C++程序基础 (五)位运算
C++中四种转换运算符的区分 const_cast 修改const和volatile属性 reinterpret_cast 指针间类型转换或者指针和整形的转换.二进制重新翻译. static_cast ...
- C/C++ 程序基础 (一)基本语法
域操作符: C++ 支持通过域操作符访问全局变量,C不支持(识别为重定义) ++i和i++的效率分析: 内置类型,无区别 自定义数据类型,++i可以返回引用,i++只能返回对象值(拷贝开销) 浮点数与 ...
- 更改 Linux 语言为中文
查看当前系统语言环境: echo $LANG 查看安了哪些中文语言包 locale -a |grep "zh_CN" 没有输出,说明没有安装,输入下面的命令安装 ...
- linux系统监控工具glances
glances linux系统自带了很多系统性能监控工具,如top,vmstat,iftop等等,还有一款监视工具glances,它能把其他几个监控的指标都集于一身.Glances是一个相对比较新的系 ...
- vue.js 四(指令和自定义指令)
官方的指令说明已经很简单了,这里再写一遍,也是自己加深一下印象 v-text 就是写入单纯的文本,可以忽略这个指令直接双花括号代替 <span v-text="msg"> ...
- Java总结 - List实现类ArrayList&LinkedList
本文是根据源码进行学习的,如果我有什么理解不对的地方请多指正,谢谢您 上面基本就是List集合类的类图关系了,图中省略掉了比如Cloneable等标记接口,那么List分别具体的主要实现类有:Arra ...