Chain: http://acm.hdu.edu.cn/showproblem.php?pid=1719
FriendTime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2099 Accepted Submission (s): 1058
Problem descriptionfriend number is 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) is friend number.
Now your task was to judge whether an integer is a friend number.
Inputthere is several lines in input with each line have a nunnegative integer a, 0<=a<=2^30.
Outputfor the number A on all line of the input, if a is a friend number, output "yes!", otherwise output "no!".
Sample Input
31312112131
Sample Output
yes! Yes!no!
Test instructions: 1 and 2 are friendly numbers, and if a, B is a friendly number, then A*b+a+b is also a friendly number.
Practice:
friend=a*b+a+b=a*b+a+b+1-1= (a+1) * (b+1)-1
Suppose a= (c+1) * (d+1)-1 b= (e+1) * (f+1)-1
Then friend= (c+1) * (d+1) * (e+1) * (f+1)-1
Then you can put a friend number through this, constantly into the decomposition, and finally must be (c+1) * (d+1) * (e+1) * (f+1) * (g+1) * (h+1) * (i+1) *.....-1 This form, decomposition to the end, that is, the letters are all 1 or 2.
Then can get any one friend=2^n+3^m-1;
So after a number plus 1, if factorization is only 2 and 3, then this number is the friend number.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> #define INF 999999999#define EPS 0.00001#define LL __int64 #define PI ACOs ( -1.0) int main () {int n;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"); elseprintf ("no!\n");} return 0;} /*4 12 3 4 11 2 3 4*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1719 Friend Mathematical deduction