HDU 1719 Friend 【规律题】
2015-08-24 10:37
141 查看
Friend
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2215 Accepted Submission(s): 1111
[align=left]Problem Description[/align]
Friend number are defined recursively as follows.
(1) numbers 1 and 2 are friend number;
(2) if a and b are friend numbers, so is ab+a+b;
(3) only the numbers defined in (1) and (2) are friend number.
Now your task is to judge whether an integer is a friend number.
[align=left]Input[/align]
There are several lines in input, each line has a nunnegative integer a, 0<=a<=2^30.
[align=left]Output[/align]
For the number a on each line of the input, if a is a friend number, output “YES!”, otherwise output “NO!”.
[align=left]Sample Input[/align]
3 13121 12131
[align=left]Sample Output[/align]
YES! YES! NO!
[align=left]Source[/align]
2007省赛集训队练习赛(2)
思路:
刚开始就只想到把给出来的那个式子进行变形n=a*b+a+b=(a+1)(b+1)-1;然后(n+1)=(a+1)*(b+1),想了想也没有什么规律,然后在网上看到人家的推导过程,原来就差了一点!哎,无语了!
设a,b是朋友数,则x=(a+1)*(b+1)-1;
设c,d是朋友数,则y=(c+1)*(d+1)-1;
则n=(x+1)*(y+1)-1,n也是朋友数;
n=(x+1)*(y+1)-1=[(a+1)*(b+1)]*[(c+1)*(d+1)]-1;
...................
如果往后继续算,我们能够得到一个规律,就是任何一个朋友数都是由最基本的朋友数构成的,也就是任何一个朋友数n+1=(1+1)^p+(1+2)^q,(其中p,q>=0),这一点需要注意的是在判断的时候,并不是那个数加1是2或3的倍数就是朋友数,而是由多个2和多个3相乘得到的!具体看代码:
代码:
#include <stdio.h> #include <string.h> int main() { int n; while(scanf("%d",&n)!=EOF) { if(n==0) { printf("NO!\n"); continue; } n=n+1; while(n%2==0) { n=n/2; } while(n%3==0) { n=n/3; } if(n==1) printf("YES!\n"); else printf("NO!\n"); } return 0; }
搜索
相关文章推荐
- 查看GCC默认定义的预编译宏
- 表单域规范写法
- Android 标题栏、状态栏隐藏
- 静默安装和静默卸载
- HDU2203 亲和串(string函数应用)
- 四大线程池的简单使用
- 后台拼接input 后,动态获取input的值
- django+uwsgi+nginx部署
- 对寄存器ESP和EBP的一些理解
- MarkDown CSS样式
- 大数据存储之分布式文件系统(一)
- oracle 估算redo脚本
- TCP/IP——TCP网络传输数据包1460MSS和1448负载
- Java中HashMap和TreeMap的区别深入理解
- css 清楚浮动的8种方式
- UIImageJPEGRepresentation UIImagePNGRepresentation
- Weblogic加Apache的群集配置
- RESTful API 设计最佳实践
- 《从零开始学Swift》学习笔记(Day4)——用Playground工具编写Swift
- 设计模式培训之一:为什么要用单例模式?