POJ 2773 Happy 2006【GCD/欧拉函数】
根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b)
如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性。
所以只要求出小于n且与n互质的元素即可。
#include<stdio.h>
#include<string.h>
const int N=;
int pr[N],cnt;
int gcd(int a,int b){
if(!b) return a;
return gcd(b,a%b);
}
int main(){
int n,k;
while(~scanf("%d%d",&n,&k)){
cnt=;
for(int i=;i<=n;i++){
if(gcd(n,i)==)
pr[++cnt]=i;
}
if(k%cnt)
printf("%d\n",k/cnt*n+pr[k%cnt]);
else
printf("%d\n",(k/cnt-)*n+pr[cnt]);
}
return ;
}
也可以用欧拉函数求小于n且与n互质的元素。就不用O(N)了。速度提高10倍左右。
POJ 2773 Happy 2006【GCD/欧拉函数】的更多相关文章
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- poj 2480 Longge's problem [ 欧拉函数 ]
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 2 ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- Bzoj-2818 Gcd 欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...
- BZOJ2818: Gcd 欧拉函数求前缀和
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...
- hdu2588 gcd 欧拉函数
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 欧拉函数+容斥定理
输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- [python学习笔记]Day3
函数 如: def is_leapyear(year): if (year%4 == 0 and year%100 != 0) or (year%400 == 0): return True else ...
- 【Asphyre引擎】今天终于把精灵demo基本改好了。
doudou源代码 包含Sprite代码(Sprite还没改完,粒子特效有些问题,但是基本上可以用了) Stage1-1.map 不好意思,漏了地图配置.
- The main concepts
The MVC application model A Play application follows the MVC architectural pattern applied to the we ...
- javascript函数中的三个技巧【三】
技巧三: [函数绑定] 在javascript与DOM交互中经常需要使用函数绑定,定义一个函数然后将其绑定到特定DOM元素或集合的某个事件触发程序上,绑定函数经常和回调函数及事件处理程序一起使用,以便 ...
- request.getRequestDispatcher()的两个方法forward()/include()!!!
RequestDispatcher rd = request.getRequestDispatcher("/MyServlet");请求转发: rd.forward( reques ...
- Ranges用法
RANGES语句:要用与选择表相同的结构创建内表,可使用RANGES语句,如下所示: 语法:RANGES <seltab> FOR <f>. 该语句创建选择表<selta ...
- 《慕客网:IOS-动画入门》学习笔记
新建Cocoa Touch Class,语言是swift 然后继续为界面添加一个普通的View Controller,并且添加前面视图的静态table的转向剪头指向这个View Controller, ...
- new与malloc的区别
看起来,它们的不同只是new比malloc用起来更方便而已.仅仅是这样吗?不是吗? 1 来源不同: 1)new/delete是C++中的操作符,而malloc/free是C中的标准库函数,需要库文件支 ...
- Effective Java 44 Write doc comments for all exposed API elements
Principle You must precede every exported class, interface, constructor, method, and field declarati ...
- JavaScript Patterns 4.1 Functions Background
Functions are first-class objects and they provide scope. • Can be created dynamically at runtime, d ...