XTU1168:Alice and Bob(二维DP)
2014-05-19 22:13
609 查看
Problem Description
Alice and Bob always love to play games, so does this time. It is their favorite stone-taken game. However, this time they does not compete but co-operate to finish this task. Suppose there is a stack of n stones. Each turn, Alice can only take away stones in number pow of 2, say 1, 2, 4, 8, 16, ... Bob can only take away stones in number pow of 3, say 1, 3, 9, 27, 81, ... They takes stones alternately, and lady first.Notice in each turn, Alice/Bob have to take away at least one stone, unless the stack is empty. Now, the question is, what is the least number of operation for taking away all the stones.Input
Multiple test cases. First line, there is an integer T ( 1 ≤ T ≤ 20 ), indicating the number of test cases. For each test case, there is a number n ( 1 ≤ n ≤ 10000 ), occupying a line, indicating the total number of the stones.Ouput
For each test case, output a line. It is an integer number k, indicating the least number of operation in need to finish the task.Sample Input
5 1 2 3 4 5
Sample Output
1 1 2 1 2
dp[i][0]表示取i颗石子到Alice的最小步数
dp[i][1]表示取i颗石子到Bob的最小步数
dp[i][0]=min(dp[i-2^k][1])+1) 代表 alice 取一次,后得到的 bob取得最小数量加上刚才aclice取得1次,等于alice取得最小次数[code]同理dp[i][1]=min(dp[i-3^k][0]+1)
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define L 10001#define inf 1<<30int dp[L][2];int main(){ int n,t,i,j; dp[0][0] = dp[0][1] = 0; dp[1][0] = dp[1][1] = dp[2][0] = 1; dp[2][1] = 2; for(i = 3; i<L; i++) { dp[i][0] = dp[i][1] = inf; for(j = 1; j<=i; j*=2) dp[i][0] = min(dp[i][0],dp[i-j][1]+1); for(j = 1; j<=i; j*=3) dp[i][1] = min(dp[i][1],dp[i-j][0]+1); } scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d\n",dp[0]); } return 0;}
相关文章推荐
- XTU OJ 1168 Alice and Bob (二维dp)
- 树形dp-hdu-3660-Alice and Bob's Trip
- hdu3660 Alice and Bob's Trip 树形dp
- HDU 4111 Alice and Bob 【DP解决博弈】
- [HDU] 3660 Alice and Bob's Trip -- 树形DP?
- 【XSY2190】Alice and Bob VI 树形DP 树剖
- UVA1484 Alice and Bob's Trip(树形dp)
- uvalive 5760 Alice and Bob (组合游戏,dp)
- uva1484 Alice and Bob's Trip (树形dp)
- Uva 1484 Alice and Bob's Trip(树形dp)
- uva 1484 - Alice and Bob's Trip(树形dp)
- XTOJ 1168 Alice and Bob (记忆化搜索)
- uva 1484 Alice and Bob's Trip (树形dp)
- UVALive - 5088 Alice and Bob's Trip 树形DP
- 玲珑学院OJ 1028 - Bob and Alice are playing numbers 字典树,dp
- UVALive 5088 Alice and Bob's Trip(树形DP)
- hdu4268 Alice and Bob(二维贪心的固定思想 STL的multiset的二分查找函数实现)
- hdu - 3660 - Alice and Bob's Trip(树形dp)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- UVA 1484 - Alice and Bob's Trip(树形DP)