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.

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 Input
3
13121
12131
Sample Output
YES!
YES!
NO!


OJ-ID:
HDU-1719

author:
Caution_X

date of submission:
20190930

tags:
数学推导

description modelling:
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.

major steps to solve it:
1.设n是Friend数,则n=ab+a+b=(a+1)*(b+1)-1   =>   n+1=(a+1)*(b+1)
2.a,b都是Friend数,所以a+1=(a'+1)*(b'+1),b+1=(a''+1)*(b''+1)直到a,b=1,2
3.那么:n+1= 2^x * 3^y

warnings:
0需要特判

AC CODE:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n;
    while(~scanf("%lld",&n)){
        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;
}
View Code



01-26 17:01