1. 程式人生 > >HDU 2176 取(m堆)石子遊戲 博弈

HDU 2176 取(m堆)石子遊戲 博弈

ani for brush print 個數 puts tdi spa 所有

取(m堆)石子遊戲

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3598 Accepted Submission(s): 2151


Problem Description m堆石子,兩人輪流取.只能在1堆中取.取完者勝.先取者負輸出No.先取者勝輸出Yes,然後輸出怎樣取子.例如5堆 5,7,8,9,10先取者勝,先取者第1次取時可以從有8個的那一堆取走7個剩下1個,也可以從有9個的中那一堆取走9個剩下0個,也可以從有10個的中那一堆取走7個剩下3個.

Input 輸入有多組.每組第1行是m,m<=200000. 後面m個非零正整數.m=0退出.

Output 先取者負輸出No.先取者勝輸出Yes,然後輸出先取者第1次取子的所有方法.如果從有a個石子的堆中取若幹個後剩下b個後會勝就輸出a b.參看Sample Output.

Sample Input 2 45 45 3 3 6 9 5 5 7 8 9 10 0

Sample Output No Yes 9 5 Yes 8 1 9 0 10 3

Nim博弈並要求給出策略

新開兩個數組記錄完事

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define INFLL   0x3f3f3f3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;
typedef pair<int, int> PII;
using namespace std;

int a[200005];
int p1[200005];
int p2[200005];

int main() {
    //FIN
    int n;
    while(~scanf("%d", &n) && n) {
        int ans = 0;
        int cnt = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            ans ^= a[i];
        }
        for(int i = 1; i <= n; i++) {
            if((ans ^ a[i]) < a[i]) {
                p1[cnt] = i;
                p2[cnt] = a[i] - (ans ^ a[i]);
                cnt++;
            }
        }
        if(ans == 0) puts("No");
        else {
            puts("Yes");
            for(int i = 0; i < cnt; i++) {
                printf("%d %d\n", a[p1[i]], a[p1[i]] - p2[i]);
            }
        }

    }

    return 0;
}

  

HDU 2176 取(m堆)石子遊戲 博弈