Friend

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2131    Accepted Submission(s): 1074


Problem Description
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.
 

 

Input
There are several lines in input, each line has a nunnegative integer a, 0<=a<=2^30.
 

 

Output
For 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!
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1905 2082 1073 1717 1852 
 
friend数: n = (a + 1)(b + 1) - 1;
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 int main(){
 6     int n;
 7     while(~scanf("%d", &n)){
 8         if(!n){
 9             printf("NO!\n");
10             continue;
11         }
12         n += 1;
13         while(n % 2 == 0 || n % 3 == 0){
14             if(n % 2 == 0)
15                 n /= 2;
16             else
17                 n /= 3;
18         }
19         if(n == 1)
20             printf("YES!\n");
21         else
22             printf("NO!\n");
23     }
24     return 0;
25 }

 

 
 
posted on 2015-08-13 15:45  cleverbiger  阅读(249)  评论(0编辑  收藏  举报