日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【ZOJ - 3591】Nim(博弈问题,思维,STLmap)

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ZOJ - 3591】Nim(博弈问题,思维,STLmap) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. The game ends when one of the players is unable to remove object in his/her turn. This player will then lose. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap. Here is another version of Nim game. There are?N?piles of stones on the table. Alice first chooses some CONSECUTIVE piles of stones to play the Nim game with Tom. Also, Alice will make the first move. Alice wants to know how many ways of choosing can make her win the game if both players play optimally.

You are given a sequence a[0],a[1], ... a[N-1] of positive integers to indicate the number of stones in each pile. The sequence a[0]...a[N-1] of length N is generated by the following code:

int g = S;?

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

? ? a[i] = g;

? ? if( a[i] == 0 ) { a[i] = g = W; }

? ? if( g%2 == 0 ) { g = (g/2); }

? ? else ? ? ? ? ? { g = (g/2) ^ W; }

}

Input

There are multiple test cases. The first line of input is an integer?T(T?≤ 100) indicates the number of test cases. Then?T?test cases follow. Each test case is represented by a line containing 3 integers?N,?S?and?W, separated by spaces. (0 <?N≤ 105, 0 <?S, W?≤ 109)

Output

For each test case, output the number of ways to win the game.

Sample Input

2 3 1 1 3 2 1

Sample Output

4 5

題目大意:

? ?因為我們知道Nim博弈,這x堆石子的異或和為0則必敗,不為零則必勝,所以就是問你在這些石子中選擇哪些連續的堆,可以必勝,問你選擇方案數。

? 也就是,給n個數,讓你求,有多少段連續區間,使得這段區間的異或和不為0。

解題報告:

正難則反,可以先求出有多少個區間,異或和為零,然后用總區間數去作差就行了。?

做法就是個常見的在線處理。。。這題也可以先求出這個和來,然后用C(n,2)去減這個和,得到的就是答案。

AC代碼:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<map> using namespace std; typedef long long ll; int n; ll a[100010]; ll sum[100010]; map<ll,int> mp; int main() {int t,q,cnt;ll i,j,k,g,S,W,res,tmp,ss;cin>>t;while(t--) {cin>>n>>S>>W;g = S;res=0;for (i=1; i<=n; i++) {a[i] = g;if( a[i] == 0)a[i] = g = W;if( g%2 == 0 )g = (g/2);else g = (g/2) ^ W;}mp.clear();mp[0]=1;for(i=1; i<=n; i++) {sum[i]=sum[i-1]^a[i];res+=i-mp[sum[i]];mp[sum[i]]++;}cout<<res<<endl;}return 0; }

?

總結

以上是生活随笔為你收集整理的【ZOJ - 3591】Nim(博弈问题,思维,STLmap)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。