Friend
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2005 Accepted Submission(s): 1009
(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.
第一次寫硬是沒有寫出來,一直以爲是打表寫的,發現也沒啥規律可循的,唯一找出來是一個遞推方程,可是感覺太麻煩,不想寫。後來看了別人寫的,感覺自己在數學方面還是有點欠缺的,所以有必要解釋解釋,給自己看。
首先,原式ab+a+b = ab+a+b+1-1 = (a+1)*(b+1)-1
令a = (a1+1)*(a2+1)-1;
b = (b1+1)*(b2+1)-1;
代入原式中可得:n = (a1+1)*(b1+1)*(a2+1)*(b2+1)-1;
因爲原式的朋友數都是由1,2推到出來的
所以遞推到最底層,那麼(a1+1)*(b1+1)*(a2+1)*(b2+1)肯定是2,3的倍數(即是1+1,2+1)
所以最後就是要解決n+1得到的數到底是不是隻有2,3這些因子
#include<stdio.h>
int main()
{
int x,y,a;
while(~scanf("%d",&a))
{
if(a==0)
{
printf("NO!\n");
continue;
}
else
{
a=a+1;
while(a%2==0||a%3==0)
{
if(a%2==0)
a/=2;
else if(a%3==0)
a/=3;
}
}
if(a==1)
printf("YES!\n");
else
printf("NO!\n");
}
return 0;
}