1349: Taking Pebbles (博弈 打表找规律)
1349: Taking Pebbles
Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 233 Solved: 141
Description
There is a pile of N pebbles initially. Alice and Bob are playing a taking pebbles game as follows:
Alice and Bob moves by turns, Alice moves first. For each move, the player can takes away at least one and at most half of the pebbles remained (“at most half” means you can take way at most k (k >= 1) pebbles if there are 2*k or 2*k+1 pebbles remained). If there is only one pebble remained, the player can also take away this pebble. The player who takes away the last pebble wins.
If Alice and Bob are both clever enough, and they both want to be the winner, who will win?
Input
The first line has one integer T (1 <= T <= 200), means there are T test cases.
For each test case, there is only one line with an integer N (2 <= N <= 109), means the number of pebbles initially.
Output
For each test case, print “Alice” (without quotation marks) in one line if Alice will win. Otherwise print “Bob” (without quotation marks) instead.
Sample Input
5 2 3 4 5 6
Sample Output
Bob Alice Alice Bob Alice
Hint
Source
中南大学第一届长沙地区程序设计邀请赛一般都是打个表,找找看有没有规律
因为答案就两种情况
肯定是跟n有关
那可能是有规律的......
试着打个表
果然!!!!!
n为以下数字的时候
2
5
11
23
47
95
191
383
767
Bob赢
找到这个规律之后
还pe,re了好几发
因为还想着把表存下来....
真是傻得一批
直接判断一下不是不是这些数字就可以了啊
.....
void dabiao() { f[1]=1; f[2]=0; for(int i=3; i<=1000; i++) { int flag=0; int x=i/2; if(i%2) x=i/2+1; for(int j=i-1; j>=x; j--) { if(f[j]==0) { flag=1; break; } } if(flag==1) { f[i]=1; } } for(int i=1; i<=1000; i++) { if(f[i]==0) { printf("%d\n",i); } } }
code:
#include<stdio.h> #include<iostream> #include<math.h> #include<algorithm> #include<memory.h> #include<memory> using namespace std; #define max_v 1005 #define max_n 1000 typedef long long LL; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); if(n%2==0&&n!=2) printf("Alice\n"); else if(n==2) printf("Bob\n"); else { int k=2; while(k<n) { k=k*2+1; } if(k==n) printf("Bob\n"); else printf("Alice\n"); } } return 0; } /* 碰到这种题就有点郁闷,因为写的很少 一般都是打个表,找找看有没有规律 因为答案就两种情况 肯定是跟n有关 那可能是有规律的...... 试着打个表 果然!!!!! n为以下数字的时候 2 5 11 23 47 95 191 383 767 Bob赢 找到这个规律之后 还pe,re了好几发 因为还想着把表存下来.... 真是傻得一批 直接判断一下不是不是这些数字就可以了啊 ..... */ /* 2 5 11 23 47 95 191 383 767 */