Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1719
Main topic:
Let's do a recursive definition of a friendly number:
(1) integers 1, 2 are friendly numbers
(2) If A and B are friendly numbers, then a*b + A + B is also a friendly number
(3) Only the numbers defined in (1) and (2) are friendly numbers.
Now give you a number n (0 <= N <= 30) to determine whether N is a friendly number
Ideas:
Very interesting a simple math problem. Set N is a friendly number, then n can be decomposed into:
n = a*b + A + b = (a+1) * (b+1)-1, i.e. n+1 = (a+1) * (b+1) is defined by recursion, where A and B must also be friendly numbers.
A = a1*b1 + a1 + B1 = (a1+1) * (b1+1)-1, i.e. A + 1 = (a1+1) * (b1+1), same as B + 1 = (a2+1) * (b2+1).
Substituting n + 1 = (a+1) * (b+1) is obtained: n + 1 = (a1+1) * (b1+1) * (a2+1) * (b2+1). And if only AI and bi are not
is 1 or 2, it can be broken down all the time. Finally get n + 1 = (1 + 1) ^x * (2 + 1) ^y = 2^x * 3^y.
This is good to do, first n plus 1, and then all 2 of the factor and 3 of the vegetarian factor all eliminated, if the elimination factor after the results are
1, the n+1 contains only 2 and 3 factors, then n is the friendly number. If the result of elimination factor after 1, that is, n also contains other factors,
Then n is not a friendly number.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;int Main () { int n; Solve (); while (~SCANF ("%d", &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;}
HDU1719 Friend "Mathematical Law"