Nyoj 88 Hanoi (a) "Fast power"

Source: Internet
Author: User

Hanoi (a) time limit:MS | Memory limit:65535 KB Difficulty:3
Describe

In India, there is an old legend: in the world center of Sheng Miao (in northern India), a yellow copper plate is inserted with three stone needles. In the creation of the world, The Hindu god Brahma, on one of the needles, dressed from the bottom to the top 64 pieces of gold, the so-called Hanoi. No matter the day or night, there is always a monk in accordance with the following rules to move these gold pieces: one at a time, no matter where the needle, the small pieces must be on top of the large. The monks predicted that the world would be wiped out in a thunderbolt when all the pieces of gold were moved from the needle on which Brahma was dressed, and that the Vatican, the temples and all sentient beings would perish.

Now, can you calculate the minimum number of steps you need to move when all the Nottingham gold slices that start with M gold are moved to the other needle? (Because the result is too big, now only ask you to calculate the result of the decimal bit last six bits)

Input
The first line is an integer N representing the number of groups of test data (0<N<20)
The first line of each set of test data is an integer m, which represents the number of gold slices at the beginning. (0<m<1000000000)
Output
The output is the last six bits of the decimal representation of the minimum number of steps that need to be moved to move all the gold sheet from the starting pin to the other.
Sample input
211000
Sample output
169375

Solving: moving steps equals 2^n-1
#include <stdio.h> #include <string.h>long long f (Long long a,int b,int c) {    long long Ans=1;    a=a%c;    while (b)    {    if (b&1)        ans= (ans*a)%c;    b/=2;       A= (a*a)%c;    }    return ans;} int main () {int n,m,j,i,t;scanf ("%d", &t), while (t--) {scanf ("%d", &n);p rintf ("%lld\n", f (2,n,1000000)-1);} return 0;}

  

Nyoj 88 Hanoi (a) "Fast power"

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.