HDU 1719 Friend 【规律题】
来源:互联网 发布:淘宝信用贷款入口 编辑:程序博客网 时间:2024/04/28 14:09
Friend
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2215 Accepted Submission(s): 1111
Problem Description
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.
(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.
Input
There are several lines in input, each line has a nunnegative integer a, 0<=a<=2^30.
Output
For the number a on each line of the input, if a is a friend number, output “YES!”, otherwise output “NO!”.
Sample Input
31312112131
Sample Output
YES!YES!NO!
Source
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;}
搜索
0 0
- HDU 1719 Friend 【规律题】
- HDU 1719 Friend 【规律】
- HDU 1719 Friend(规律)
- HDU 1719 Friend(规律)
- Friend HDU杭电1719 【规律题】
- HDU 1719 Friend 找规律
- hdoj 1719 Friend(找规律)
- hdu 1719 Friend
- hdu(1719)Friend
- friend(hdu 1719)
- hdu-1719 Friend
- hdu---1719Friend
- HDU 1719--Friend【数学】
- HDU 1719 Friend 【数学题】
- hdu 1719 Friend
- HDU 1719 Friend
- 【HDU】 1719 Friend
- HDU 1719 Friend
- 静默安装和静默卸载
- Linux异常关机后,Mysql启动出错ERROR 2002 (HY000)
- 百度地图商家标注,查询附近3000米内的商家并标到地图上
- 数据仓库项目应该如何选择ETL工具:ETL or E-LT
- Qt 设置窗体无边框
- HDU 1719 Friend 【规律题】
- 大数据学习之BigData常用算法和数据结构
- STM32定时器输入捕获
- Spring MVC的多视图解析器配置及与Freemarker的集成
- HTML学习02
- 关于UIScrollView的alwaysBounceVertical属性
- 使用Nexus创建Maven私有仓库
- 自学QT之报错: name lookup of 'i' changed for ISO 'for' scoping
- 2016阿里校招算法岗笔试题——魔方