[暑假集训--数论]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 ...
随机推荐
- Bootstrap历练实例:警告框(Alert)插件的方法
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 自己写一个Promise
参考Promise 的 官方规范 https://promisesaplus.com/ Promise 其实就是一个状态机 它只有两种状态变化 pending => fulfill ...
- 通用后台管理系统源码,响应式布局,Java管理系统源码,零门槛安装部署
本项目是一个通用响应式管理后台,导入开发环境安装就能直接运行,界面也非诚漂亮,在PC端和移动端也是自适应的.非常适合企业或者个人搭建各种商城后台,博客后台,网站管理后台等. 源码启动后的截图 需要这套 ...
- 力扣题目汇总(丑数,重复N的元素,求众数)
丑数 1.题目描述 编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 ...
- Django权限管理系统设计分析
权限管理顾名思义,其实就是角色控制权限的系统,每个用户对应一个角色,每个角色有对应的权限,比如公司会有CEO,总监,销售经理,销售员,每个人的权限都不一样,那我们给他展示的url也都不同 一.首先创建 ...
- loj2049 「HNOI2016」网络
好像复杂度来说不是正解--不加谜之优化(下叙)能被loj上的加强数据卡 #include <algorithm> #include <iostream> #include &l ...
- 递归查询子类sql
--通过父节点查询子节点 WITH TREE AS( SELECT * FROM Role WHERE RoleID = 4 -- 要查询的父 id UNION ALL SELECT Role.* F ...
- Marketing learning-1
Today we start to learn something about marketing together.Sometimes i just propose a question,and i ...
- Robotium测试架构规划及测试用例组织
转自:http://blog.sina.com.cn/s/blog_68f262210102vrft.html 6.1 测试架构规划 由于测试用例执行的时候是在手机上执行的,所以类似于Web的把测试数 ...
- Python+Selenium练习篇之5-利用partial link text定位元素
本文介绍如何通过partial link text来定位页面元素.看到这个,有点和前一篇文字link text有点类似.字面意思,确实和link text相类似,partial link text就是 ...