Time Limit : 1000/1000ms (Java/Other)
Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 53
Accepted Submission(s) : 16
Problem DescriptionFriend 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.
InputThere are several lines in input, each line has a nunnegative integer a, 0<=a<=2^30.
OutputFor the number a on each line of the input, if a is a friend number, output “YES!”, otherwise output “NO!”.
Sample Input3
13121
12131
Sample OutputYES!
YES!
NO!
可观察其规律,c+1=(a+1)(b+1);即一定是由2,3变换过来的,不过不能直接计算它是否是2的倍数或者3的倍数,该过程与其无关
#include<cstdio>
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==0)
{
printf("NO!\n");
continue;
}
if((n+1)%2==0||(n+1)%3==0)//测试该数是否是由2,3变换过来的
{
n++;
while(n%2==0)
n/=2;
while(n%3==0)
n/=3;
}
if(n==1)
printf("YES!\n");
else
printf("NO!\n");
}
return 0;
}
最初写的内存不够。。。不过确实,十亿的内存开不起
#include<cstdio>
#include<cstring>
#include<math.h>
__int64 flag[100000000],a[100000000];
int main()
{
int m,n;
a[0]=1,a[1]=2;
memset(flag,0,sizeof(flag));
flag[a[0]]=1;
flag[a[1]]=1;
for(int i=2;i<10000000;)
{
a[i]=a[i-1]*a[i-1]+2*a[i-1];
flag[a[i]]=1;
i++;
a[i]=a[i-3]*a[i-3]+2*a[i-3];
flag[a[i]]=1;
i++;
a[i]=a[i-3]*a[i-4]+a[i-3]+a[i-4];
flag[a[i]]=1;
i++;
}
while(~scanf("%d",&n))
{
if(flag[n])
printf("YES!\n");
else
printf("NO!\n");
}
return 0;
}