CSU C (1972): The Gift of the great Brahma-Hanoi deformation-double Hanoi

Source: Internet
Author: User
topic: Description

For a long time did not smoke the SSR son Hao June, pray God to give him a SSR, his integrity moved the big Brahma, so, the big Brahma and got a 2*n layer of Hanoi, there are n kinds of size, each size two, from the top down in order from small to large, if the son Hao June can be placed in accordance with the rules of Hanoi, Then give the son Hao June a SSR.
However, when the son Hao June, the big Brahma said: "No,no,no, this is not what I want, I need the initial situation is what kind of results, even if the same size of the order can not be replaced." And then applied the magic back to the initial state.
So ask poor son ho how many steps he has taken and how many steps he needs to carry.
Although the son Hao June is very not, but very clever, so will choose the least number of steps of the scheme ^_^ Input

Multiple sets of test data.
Each set of data begins with an integer, N (0<n<=1234567), representing how many sizes of Hanoi plates are Output

Output two integers, representing how many steps the Son-ho has moved and how many steps to carry.
As the result is very large, you need to model a large number of 233333333 (8 3) Sample Input

1
1234567
Sample Output
2 3
109259870 218519739

The main idea: the deformation of the Hanoi, each size of the Hanoi into two, ask the two results order does not matter and the order required unchanged the number of moving steps. Train of thought: Classic recursive set move n order doesn't matter cost F (n): First move the n-1 Hanoi to the middle, spend f (n-1), then move the largest two to the far right, take 2 steps, then move the middle n-1 species to the far right and spend F (n-1). The result is f (n) =f (n-1) *2+2, where f (1) = 2;
Set the order to be the same (even if the same order of two species cannot be changed) to G (n): Because each time we call the above f (n) This method, the maximum two order will be reversed, so call even number of times, the order will not change, to ensure that this is a few times, because the following recursive items are even several times, So it can be guaranteed in order, so we can have the following recursion: G (n) =f (n-1) +2+f (n-1) +2+g (n-1). The explanation is to move the n-1 to the far right first in a way that doesn't matter in order, and then move the top two to the middle, when they're in the opposite order. And then move the n-1 to the left to use the same method of not considering the order, at this point because the n-1 moved even several times so the n-1 order is unchanged, and then the middle of the two largest move to the rightmost, then they are in the order of no problem. The last n-1 on the Left is G (n-1).
The code I wrote is based on the existing conclusion that the standard Hanoi move number is (2^n)-1. Then there are two of each layer and the order does not matter the nature is 2* ((2^n)-1) times slightly.
The order of the requirements of the sample directly to see the conclusions, of course, these two formulas will be able to pass through my recursive formula to derive results, interested in the big guy can try.
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
Const double PI = ACOs ( -1.0);
Const double EPS = 1e-6;
const int INF = 1000000000;
const int mod = 233333333;
int t,n,m;

ll Q_pow (ll A,ll b)
{
    ll ans=1;
    while (b)
    {
        if (b&1)
        {
            ans= (ans*a)%mod;
        }
        A= (a*a)%mod;
        b>>=1;
    }
    return ans;
}

int main ()
{
    ll ans1,ans2;
	while (scanf ("%d", &n)!=eof)
    {
        ans1= (Q_pow (2,n) *2-2)%mod;
        Ans2= (ans1*2-1)%mod;
        printf ("%lld%lld\n", ans1,ans2);
    }
    return 0;
}





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.