*[hackerrank]Chocolate in Box

https://www.hackerrank.com/contests/w7/challenges/chocolate-in-box

Nim Game:http://www.cdf.toronto.edu/~ajr/270/probsess/03/strategy.html

策略是将数组变成XOR为0的(这样就对称了)。之后对方每做一步,可以证明总能找到一个方法己方也做一步能使XOR为0。这样最后会为0,获胜。

计算赢的方案数只要看一下数组里多少个数能够经过改变使得XOR为0.

#include <vector>

#include <iostream>



using namespace std;



int main() {

	int N;

	cin >> N;

	vector<int> vec;

	int r = 0;

	for (int i = 0; i < N; i++) {

		int t;

		cin >> t;

		vec.push_back(t);

		r ^= t;

	}

	int count = 0;

	for (int i = 0; i < N; i++) {

		if (vec[i] > (vec[i]^r))

			count++;

	}

	cout << count << endl;

    return 0;

}

  

你可能感兴趣的:(rank)