您的位置:首页 > 其它

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;
}


搜索
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: