Hdoj 1207 Novo II "DP"

Source: Internet
Author: User

Hanoi IITime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 5501 Accepted Submission (s): 2668

Problem description The classic question of the Nottingham is often present as a recursive classic example. Some may not know the story of the Hanoi tower problem. Hanoi is a story from the Hindu legend, when God created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. God commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars. There is prophecy that the universe will be destroyed in a flash of time when this thing is done. Others believe that the Brahman is still moving the disc at a moment's stop. Well, of course the legend is not credible, and now Hanoi is more of a toy present. Gardon received a Hanoi toy as a birthday present.
Gardon is a person afraid of trouble (well, is lazy people), it is clear that the 64 discs one by one to move until all the plates to reach the third pillar is very difficult, so Gardon decided to do a little harm, he found an identical pillar, Pass this pillar to move all the plates to the third pillar more quickly. The following question is: How many times will he need to move to move them to the third pillar when Gardon uses n plates in a game? Obviously, when there is no fourth pillar, the solution to the problem is 2^n-1, but now with the help of this pillar, how much is it?

Input contains multiple sets of data, one row per data, and the number of plates N (1<=n<=64).

Output for each set of data, outputs a number to the minimum number of moves required to reach the target.

Sample Input
1312

Sample Output
1581

Test instructions: ...

Analysis: For n plates, we first put the X plate on a pillar and have at least f (x) steps, and then use the remaining 3 pillars to move the rest to the designated pillar Need 2^ (N-x)-1 steps, and finally move the original X plate to the designated pillar need f (x), so f (n) = 2*f (x) + 2^ (n-x)-1, and because X is not fixed (1~n), the enumeration needs to find the smallest.

Code:

#include <stdio.h> #include <string.h> #include <algorithm> #define M 70#define LL unsigned long Longusing namespace Std; LL S[m];bool Vis[m]; LL f (int k) {if (Vis[k]) return s[k];s[k] = ~0;for (int i = 1; i < K; + + i) {s[k] = min (S[k], F (i) *2+ ((LL) 1<< (k-i))-1 );} Vis[k] = 1;return s[k];} int main () {int n;s[1] = 1;vis[1] = 1;printf ("%i64u\n", ~0), while (scanf ("%d", &n) = = 1) {printf ("%i64u\n", f (n));} return 0;}


Hdoj 1207 Novo II "DP"

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.