hdu 1098Ignatius's puzzle(math)
2015-10-02 09:33
267 查看
Ignatius's puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7652 Accepted Submission(s): 5326
Problem Description
Ignatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal nonegative integer a,make the arbitrary integer
x ,65|f(x)if
no exists that a,then print "no".
Input
The input contains several test cases. Each test case consists of a nonegative integer k, More details in the Sample Input.
Output
The output contains a string "no",if you can't find a,or you should output a line contains the a.More details in the Sample Output.
Sample Input
11 100 9999
Sample Output
22 no 43
Author
eddy
思路:通过递推公式推导。
f ( x + 1) - f ( x ) = ...(省略明显被65整除的部分) + 18 + k*a;
则只需求出最小的a,使18 + k*a 能被65整除即可。
方法一: 利用欧几里德扩展公式求解。
代码如下:
#include <cstdio> void gcd_ex(int a, int b, int& d, int& x, int& y){ if(!b){d = a; x = 1; y = 0;} else {gcd_ex(b, a%b, d, y, x); y -= x*(a/b);} } int main(){ int k; //freopen("in.txt", "r", stdin); while(~scanf("%d", &k)){ int d, x, y; gcd_ex(-65, k, d, x, y); int mod = -65/d < 0 ? 65/d: -65/d; if(-18%d != 0){printf("no\n");} else {printf("%d\n", ((-18/d*y)%mod+mod)%mod);} } return 0; }
方法二: 分析:若18 + k*a 能被65整除,则必存在a = a + k*65,(k = 0, 1, 2....)使原式依然成立,则a的最小值必在[0, 65) 区间内。枚举即可。
代码:
#include <cstdio> int main(){ int k, i; // freopen("in.txt", "r", stdin); while(~scanf("%d", &k)){ for(i = 0; i < 65; i++){ if((18 + k*i)%65 == 0) {printf("%d\n", i);break;} } if(i == 65) printf("no\n"); } return 0; }
Author
eddy
相关文章推荐
- Android目录(更新中)
- SCI&EI 英文PAPER投稿经验【转】
- JavaScript权威指南学习之第2章 词法结构
- windows对象总结
- less语法基础
- Spring MVC一些配置信息解析
- java垃圾回收总结(2)
- 利用imagettftext实现动态的促销文字的变换
- Java基础知识强化之集合框架笔记14:List集合存储字符串并遍历
- Linux下静态库和动态库的创建和使用
- java 垃圾回收总结(1)
- 程序设计基石与实践之C语言内存地址基础
- WM_COMMAND 和 WM_NOTIFY 的区别
- 弹性盒子(flexible box)
- 字符串是否包含问题及扩展
- MarkDown 认识与入门
- 可移植性
- PHP中heredoc的用法(注意事项!)
- Linux的system()和popen()差异 http://blog.csdn.net/liuxingen/article/details/47057539
- POJ1637 Sightseeing tour(判定混合图欧拉回路)