HDU 1329 Hanoi Tower Troubles Again!

Hanoi Tower Troubles Again!

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on  HDU. Original ID: 1329
64-bit integer IO format: %I64d      Java class name: Main
People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn't not stopped thinking about similar puzzles with the Hanoi Tower. Mr.S invented a little game on it. The game consists of N pegs and a LOT of balls. The balls are numbered 1,2,3... The balls look ordinary, but they are actually magic. If the sum of the numbers on two balls is NOT a square number, they will push each other with a great force when they're too closed, so they can NEVER be put together touching each other. 



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

The first line of the input contains a single integer T, indicating the number of test cases. (1<=T<=50) Each test case contains a single integer N(1<=N<=50), indicating the number of pegs available. 
 

Output

For each test case in the input print a line containing an integer indicating the maximal number of balls that can be placed. Print -1 if an infinite number of balls can be placed. 
 

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 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4411609.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值