AOJ 331.汉诺塔

汉诺塔
Time Limit: 1000 ms   Case Time Limit: 1000 ms   Memory Limit: 64 MB
Total Submission: 892   Submission Accepted: 173
 
Description
大家都听说过汉诺塔吧?有n个圆盘由小到大排列,套在a柱上,每次只能移动一个圆盘,而且只能大的在下,小的在上,让你把a柱上的圆盘移到b柱,给你一个多余的c柱,问你最少移动多少次才能完成任务。
Input
输入有多组数据,每组包括一个整数n(n<=10000000),表示初始状态下有n个圆盘,当输入的n为0时,程序结束,n为负的情况不作处理。
Output
对每个输入,对应一行输出,每行输出包括一个整数,即移动的最小次数,因为数目非常大,所以请对9973求余后再输出。
Sample Input
Original Transformed
1
2
3
4
0
Sample Output
Original Transformed
1
3
7
15
Hint
采用结构:
……
for(;;){
scanf("%d",&n);
if(n==0)
break;
……
printf(……);
……
}
……
Source
Ccyjava

求n个圆盘的汉诺塔的最少移动次数

找规律得 ans = 2n-1

由于最后要对9973取模,因此使用快速幂取模算法

另外要额外注意:如果n是负数,不输出 

AC代码:GitHub

 1 //====================================================================
 2 /*
 3 By:OhYee
 4 Github:OhYee
 5 HomePage:http://www.oyohyee.com
 6 Email:oyohyee@oyohyee.com
 7 Blog:http://www.cnblogs.com/ohyee/
 8 
 9 かしこいかわいい?
10 エリーチカ!
11 要写出来Хорошо的代码哦~
12 */
13 
14 #include <cstdio>
15 #include <algorithm>
16 #include <cstring>
17 #include <cmath>
18 #include <string>
19 #include <iostream>
20 #include <vector>
21 #include <list>
22 #include <queue>
23 #include <stack>
24 #include <map>
25 using namespace std;
26 
27 //DEBUG MODE
28 #define debug 0
29 
30 //循环
31 #define REP(n) for(int o=0;o<n;o++)
32 
33 typedef long long LL;
34 
35 typedef long long LL;
36 LL exp_mod(LL a,LL n,LL b) {
37     LL t;
38     if(n == 0) return 1 % b;
39     if(n == 1) return a % b;
40     t = exp_mod(a,n / 2,b);
41     t = t * t % b;
42     if((n & 1) == 1) t = t * a % b;
43     return t;
44 }
45 
46 bool Do() {
47     int n;
48     if(scanf("%d",&n),n == 0)
49         return false;
50     if(n < 0)
51         return true;
52     printf("%lld
",exp_mod(2,n,9973) - 1);
53     return true;
54 }
55 
56 int main() {
57     while(Do());
58     return 0;
59 }
原文地址:https://www.cnblogs.com/ohyee/p/5540500.html