Friend
题目链接
题目大意
定义一种数叫做Friend Number:首先1和2是Friend Number,然后如果a和b都是Firend Number的话,ab+a+b也是Friend Number.现在给你一个数,要你判断这个数是不是Friend Number.
题解
对于题目中的这个式子,我们可以稍做变形:
ab+a+b=Friend(1)
a(b+1)+b=Friend(2)
a(b+1)+b+1=Friend+1(3)
(a+1)(b+1)=Friend+1(*)
我们可以看到,在最后的式子中左右两边结构简单而且相似,所以我们定义一种数F为 Friend Number+1 这样我们可以发现我们新定义的数是这样的:
F(a)F(b)=F(c)
前两个F数为2和3,所以我们判断一个是不是Friend Number就可以先判断它是不是F数,而F数非常好判断:仅能被2和3整除。
至此问题就解决了。
代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n;
int main()
{
while(scanf("%d",&n)!=EOF)
{
if (n==0)
{
printf("NO!\n");
continue;
}
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;
}