HDU1850(Nim游戏)
生活随笔
收集整理的這篇文章主要介紹了
HDU1850(Nim游戏)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:http://acm.hdu.edu.cn/showproblem.php?pid=1850
?
題意:對于Nim游戲:有n堆石子,每堆有a[i]個,兩人輪流從任意堆中取任意多的石子(一次不能同時從多堆中拿),每次至少
取一個,多者不限,最后取光者勝。先手的人如果想贏,一共有幾種選擇?
用到了一個很明顯的結論:a = a ^ b ^ b;
?
#include <iostream> #include <string.h> #include <stdio.h>using namespace std; const int N = 155;int a[N];int main() {int n;while(cin>>n){if(n==0) break;int ans = 0;int sum = 0;for(int i=0;i<n;i++){cin>>a[i];sum ^= a[i];}for(int i=0;i<n;i++){if(a[i] > (sum ^ a[i]))ans++;}cout<<ans<<endl;}return 0; }
?
題目:http://acm.hdu.edu.cn/showproblem.php?pid=2176
?
總結
以上是生活随笔為你收集整理的HDU1850(Nim游戏)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ3197(连分数表示)
- 下一篇: 高度为k的二叉树个数(递推分析)