[POJ2773]:Happy 2006
同样是欧拉函数的基本应用。
$\phi (N)$表示$[1,N]$中,$gcd(i,N)==1$的数的个数,同理,其也能表示$[K \times N+1,(K+1) \times N]$中$gcd(i,N)==1$的数的个数,所有这样就能把区间固定下来,然后对于固定的区间扫一遍就行了。
//POJ 2773
//by Cydiater
//2016.10.8
#include <iostream>
#include <iomanip>
#include <cstring>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define ll long long
#define up(i,j,n) for(ll i=j;i<=n;i++)
#define down(i,j,n) for(ll i=j;i>=n;i--)
const int MAXN=1e6+5;
const int LIM=1e6;
const int oo=0x3f3f3f3f;
inline ll read(){
char ch=getchar();ll x=0,f=1;
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll phi[MAXN],cnt=0,prime[MAXN],N,K,leftt,rightt;
bool vis[MAXN];
namespace solution{
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
void pret(){
phi[1]=1;
memset(vis,0,sizeof(vis));
up(i,2,LIM){
if(!vis[i]){prime[++cnt]=i;phi[i]=i-1;}
up(j,1,cnt){
if(prime[j]*i>LIM)break;
vis[prime[j]*i]=1;
if(i%prime[j]==0){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}else phi[i*prime[j]]=phi[i]*phi[prime[j]];
}
}
}
void slove(){
ll PHI=phi[N],tol=0;
leftt=(K-1)/PHI*N+1;rightt=leftt+N-1;
K-=(K-1)/PHI*PHI;
up(i,leftt,rightt){
if(gcd(i,N)==1)tol++;
if(tol==K){
printf("%lld\n",i);
return;
}
}
}
}
int main(){
//freopen("input.in","r",stdin);
using namespace solution;
pret();
while(scanf("%lld %lld",&N,&K)!=EOF)slove();
return 0;
}
[POJ2773]:Happy 2006的更多相关文章
- [暑假集训--数论]poj2773 Happy 2006
Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD ...
- POJ-2773 Happy 2006,暴力2700ms+水过!
Happy 2006 这个题很可能会超时的,但我几乎暴力的方法2700ms+过了,可能是后台水 ...
- POJ2773 - Happy 2006(欧拉函数)
题目大意 给定两个数m,k,要求你求出第k个和m互质的数 题解 我们需要知道一个等式,gcd(a,b)=gcd(a+t*b,b) 证明如下:gcd(a+t*b,b)=gcd(b,(a+t*b)%b)= ...
- POJ2773 Happy 2006【容斥原理】
题目链接: http://poj.org/problem?id=2773 题目大意: 给你两个整数N和K.找到第k个与N互素的数(互素的数从小到大排列).当中 (1 <= m <= 100 ...
- 【poj2773】 Happy 2006
http://poj.org/problem?id=2773 (题目链接) 题意 给出两个数m,k,要求求出从1开始与m互质的第k个数. Solution 数据范围很大,直接模拟显然是不行的,我们需要 ...
- 【POJ2773】Happy 2006 欧几里德
题目描述: 分析: 根据欧几里德,我们有gcd(b×t+a,b)=gcd(a,b) 则如果a与b互质,则b×t+a与b也一定互质,如果a与b不互质,则b×t+a与b也一定不互质. 所以与m互质的数对m ...
- Happy 2006 poj2773
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9049 Accepted: 3031 Descri ...
- BZOJ 2006: [NOI2010]超级钢琴
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2613 Solved: 1297[Submit][Statu ...
- #Deep Learning回顾#之2006年的Science Paper
大家都清楚神经网络在上个世纪七八十年代是着实火过一回的,尤其是后向传播BP算法出来之后,但90年代后被SVM之类抢了风头,再后来大家更熟悉的是SVM.AdaBoost.随机森林.GBDT.LR.FTR ...
随机推荐
- grootJsAPI文档
groot.view(name,factory) 用于创建一个modelView对象与指令gt-view对应 参数 用途 name 创建的modelView的名称,用groot.vms[name]可以 ...
- 安装win10
1.百度win10,看到的大都是雨林木风,ghost等江湖杂牌非原版系统.百度”msdn,我告诉你“进入微软MSDN下载中心(原来还有这么个好地方,以后就从这里下了),下载链接是ed2k格式的链接(e ...
- 关于cmd下使用taskkill无法终止进程名包含空格的进程的解决方案
在我们使用taskkill命令终止进程时,有时会遇到无法终止含空格的进程的问题.只要在进程名外部加上双引号即可解决此问题. 如图所示: 附上taskkill命令的语法及相关说明: TASKKILL [ ...
- URL(待整合到HTTP书中哦)
一:scheme://host.domain:port/path/filename scheme - 定义因特网服务的类型.最常见的类型是 http host - 定义域主机(http 的默认主机是 ...
- hibernate的Criteria条件查询
项目中用到了criteria的查询方式,觉得挺好用的,下班后找了一下资料,一边测试,一边在博客上面记录下来 1.初解 快速浏览了资料,大致了解了以下的内容: 1. Hibernate 定义了Crite ...
- 模块(modue)的概念:
在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很 ...
- nginx安装(1) – ttlsa教程系列之nginx
1.必要软件准备 安装pcre 为了支持rewrite功能,我们需要安装pcre 1 # yum install pcre* //如过你已经装了,请跳过这一步 安装openssl 需要ssl的支持 ...
- ubuntu完全卸载nginx
删除nginx连带配置文件 sudo apt-get purge nginx # Removes everything. 卸载不再需要的nginx依赖程序 sudo apt-get autoremov ...
- Elasticsearch: Indexing SQL databases. The easy way
Elasticsearchis a great search engine, flexible, fast and fun. So how can I get started with it? Thi ...
- Swift开发小技巧--识别选中照片中的二维码
识别选中照片中的二维码 点击相册按钮,打开用户相册 @IBAction func photoBtnClick(sender: AnyObject) { // 打开相册 // 1.判断是否能够打开相册 ...