2018 National multi-school algorithm winter training Camp practice Competition (first field) D N-order Hanoi deformation

Source: Internet
Author: User

Https://www.nowcoder.com/acm/contest/67/D

Ideas:

Simulate the process manually, the following is the simulation process, in order to indicate the number of steps required to move the disk label

1 1 2 1 1 2

1 1 3 1 1 2

1 1 2 1 1 3

1 1 2 1 1 2

1 1 4 1 1 2

。。。。。。

We found that each occurrence two times 1 will appear once 2, every two times 2 will appear once 3, every two times 3 will appear once 4, every two times 4 will appear once 5 ...

Then we find that if all labels greater than 1 are treated as 1, then the number of times that K step 1 appears is K/1

If all labels greater than 2 are considered 2, then the number of times that K step 2 appears is K/3

If all labels greater than 3 are considered 3, then the number of times that K step 3 appears is k/3^2

If all labels greater than 4 are considered 4, then the number of times that K step 4 appears is k/3^3

。。。。。。

Then why is this possible?

Because of this we can take the mobile cycle as a 6-step, just said each appeared two times I, appear a i+1, so each move two steps to stop (③ and ⑥ process), so k/3^ (i-1) can be expressed through this cycle of steps, take more than 6 I know where I go

Code:

#include <bits/stdc++.h>using namespacestd;#definell Long Long#definePB Push_backVector<int>a[3];intMain () {Ios::sync_with_stdio (false); Cin.tie (0);    ll N,k;  while(cin>>n>>k) {         for(intI=0;i<3; i++) a[i].clear (); llBase=1;  for(intI=1; i<=n;i++){            intT= (k/Base)%6; if(t>2) t=5-T;            A[T].PB (i); Base*=3; }         for(intI=0;i<3; i++){            if(A[i].size ()) for(intJ=a[i].size ()-1; j>=0; j--) cout<<a[i][j]<< (j==0?'\ n':' '); Elsecout<<0<<Endl; }    }    return 0;}

2018 National multi-school algorithm winter training Camp practice Competition (first field) D N-order Hanoi deformation

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.