HDU1719 Friend "Mathematical Law"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.