Hanoi Tower Troubles Again!
64-bit integer IO format: %I64d Java class name: Main
The player should place one ball on the top of a peg at a time. He should first try ball 1, then ball 2, then ball 3... If he fails to do so, the game ends. Help the player to place as many balls as possible. You may take a look at the picture above, since it shows us a best result for 4 pegs.
Input
Output
Sample Input
2 4 25
Sample Output
11 337
Source
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int maxn = 2000; 6 bool e[maxn][maxn],ok[maxn<<1],used[maxn]; 7 int ans[55],link[maxn],n; 8 int dfs(int u) { 9 for(int i = 1; i <= n; ++i) { 10 if(!used[i] && e[u][i]) { 11 used[i] = true; 12 if(link[i] == -1||dfs(link[i])) { 13 link[i] = u; 14 return true; 15 } 16 } 17 } 18 return false; 19 } 20 int solve() { 21 int tmp = 0; 22 memset(link,-1,sizeof(link)); 23 for(int i = 1; i <= n; ++i) { 24 memset(used,false,sizeof(used)); 25 tmp += dfs(i); 26 } 27 return tmp; 28 } 29 int main() { 30 int i,j,t,o; 31 for(i = 1; i*i < (maxn<<1); ++i) ok[i*i] = 1; 32 for(i = 1; i < maxn; ++i) 33 for(j = i + 1; j < maxn; ++j) 34 e[i][j] = ok[i + j]; 35 for(n = 1; n < maxn; ++n) { 36 int tmp = solve(); 37 if(n - tmp > 50) break; 38 ans[n - tmp] = n; 39 } 40 scanf("%d",&t); 41 while(t--) { 42 scanf("%d",&o); 43 printf("%d\n",ans[o]); 44 } 45 return 0; 46 }