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.
 

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
原创粉丝点击