[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 ...
随机推荐
- 转载:SQL 递归树 子父节点相互查询
if object_id('[tb]') is not null drop table [tb] go create table [tb]([modeid] int,modename varchar( ...
- C# 退出应用程序办法
Application.Exit();//好像只在主线程可以起作用,而且当有线程,或是阻塞方法的情况下,很容易失灵 this.Close();//只是关闭当前窗体. Application.E ...
- Repeater——数据库控件学习
1.Repeater控件的使用:(用于重复加载一些内容,把要重复加载的代码写在ItemTemplate里面) 脱完控件后,在设计视图中配置数据源即可~ 2.ListView(数据显示,分页此控件一拖千 ...
- iOS -- 神战
http://github.ibireme.com/github/list/ios/# https://github.com/Tim9Liu9/TimLiu-iOS http://www.ios122 ...
- python环境搭建-pycharm2016软件注册码
pycharm软件下载地址 https://www.jetbrains.com/pycharm/ 方法一: pycharm 2016 注册码 43B4A73YYJ-eyJsaWNlbnNlSWQiOi ...
- Android中的各种单位
px(像素):屏幕上的点.in(英寸):长度单位.mm(毫米):长度单位.pt(磅):1/72英寸.dp(与密度无关的像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dp = 1px ...
- 【日常笔记】datatables表格数据渲染
现在有很多表格渲染方式 这里只是记录怎么使用datatables渲染数据 使用datatables可以更方便的来渲染数据 [中文api]http://datatables.club/index.htm ...
- [转]Hibernate update和saveOrUpdate详解
原文地址:http://www.iteye.com/topic/2712 先来点概念: 在Hibernate中,最核心的概念就是对PO的状态管理.一个PO有三种状态: 1.未被持久化的VO 此时就是一 ...
- 线段树 poj3225
U:把区间[l,r]覆盖成1I:把[-∞,l)(r,∞]覆盖成0 D:把区间[l,r]覆盖成0C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换S:[l,r]区间0/1互换 因 ...
- Doccms 中新闻列表排序无效bug的修复
手动修改 content/index/list.php 37 为 $sql="Select * FROM ".TB_PREFIX."list Where channelI ...