POJ 2773 Happy 2006
来源:互联网 发布:武侠小说推荐 知乎 编辑:程序博客网 时间:2024/05/18 00:52
数论的一道题目
k很大,感觉暴搜会超时,但是最后还是没有想出来 看别人的解释后 发现好神啊
先来看看求两个数的最大公约数的求法 对于 x y (假设x>y)
若 x%y==0 则说明最大公约数为y
若!=0 则 要继续 递归求解 gcd(y,x%y)
从求最大公约数过程可以看出 gcd(x,y)=1,则 gcd(x*n+y,x)=1(第一步为 (x*n+y)%x=y 第二步 为 gcd(x,y),所以两个的最大公约数也应为1 )
故 只需求出 m内的最大公约数为1 的数 ,超过部分可通过这些数得出 。。。
#include<stdio.h> #define N 1000005int pto[N];int gcd(int x,int y){if(x%y==0)return y;return gcd(y,x%y);}int main(){int i,t,ans,m,k;while(scanf("%d%d",&m,&k)!=EOF){t=0;for(i=1;i<=m;i++) //注意i=m时要判断 因为m=1时 就要加入pto数组中if(gcd(m,i)==1)pto[t++]=i;ans=k/t;k%=t;if(k!=0)printf("%I64d\n",__int64(ans*m+pto[k-1]));elseprintf("%I64d\n",__int64((ans-1)*m+pto[t-1]));}return 0;}
- POJ 2773 Happy 2006
- poj 2773 Happy 2006
- poj 2773 Happy 2006
- poj 2773 Happy 2006
- poj 2773 Happy 2006
- POJ 2773 Happy 2006
- poj 2773 Happy 2006
- POJ 2773 Happy 2006
- poj 2773 Happy 2006
- poj 2773 Happy 2006
- POJ 2773 Happy 2006
- poj 2773 Happy 2006
- POJ 2773 Happy 2006
- POJ 2773 HAPPY 2006
- POJ 2773 Happy 2006
- POJ 2773:Happy 2006
- poj 2773Happy 2006
- POJ 2773 Happy 2006
- UIView控件 、 autolayout 、transform属性及如何修改控件的位置和尺寸
- ibatis Map作为resultClass時,必須指定具体的实现类
- grep 搜索目录中的文件
- hibernate的缓存机制
- tomcat设置内存参数<内存溢出时生成heapDump文件,生成GC日志>
- POJ 2773 Happy 2006
- DIV 浏览器水平垂直居中及文字垂直水平居中问题
- poj1185 炮兵阵地
- 11
- Java查看注册表错误:ERROR You have not installed the DLL named 'ICE_JNIRegistry.DLL'
- 计算几何基础题目
- [小说]魔王冢(41)准备出发
- Log4J日志管理类使用详解
- 完整的站内搜索Demo(Lucene.Net+盘古分词)