BZOJ1115: [POI2009]石子遊戲Kam

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

1115: [POI2009]石子遊戲KamTime Limit: 10 Sec  Memory Limit: 162 MB
Submit: 500  Solved: 308
[Submit][Status]Description

有N堆石子,除了第一堆外,每堆石子個數都不少於前一堆的石子個數。兩人輪流操作每次操作可以從一堆石子中移走任意多石子,但是要保證操作後仍然滿足初始時的條件誰沒有石子可移時輸掉遊戲。問先手是否必勝。

Input

第一行u表示資料群組數。對於每組資料,第一行N表示石子堆數,第二行N個數ai表示第i堆石子的個數(a1<=a2<=……<=an)。 1<=u<=10 1<=n<=1000 0<=ai<=10000

Output

u行,若先手必勝輸出TAK,否則輸出NIE。

Sample Input2
2
2 2
3
1 2 4Sample OutputNIE
TAKHINT

 

感謝MT大牛翻譯.

 

Source

題解:

考慮將問題轉化到階梯NIM上來。

首先初始 i 格可以移掉的石子數 為 a[i]-a[i-1]

事實上,若我們從 i 處取走了 x 個石子,那麼下一次及以後就可以在 i+1 處多取 x 個,相當於 把 i 處的 x 個石子加到了 i+1處。

問題就轉化成了階梯NIM。

orz

代碼:

 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 100514 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26     int x=0,f=1;char ch=getchar();27     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}28     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}29     return x*f;30 }31 int n,cs,ans,a[maxn];32 int main()33 {34     freopen("input.txt","r",stdin);35     freopen("output.txt","w",stdout);36     cs=read();37     while(cs--)38     {39         n=read();40         for1(i,n)a[n+1-i]=read();a[n+1]=0;41         ans=0;42         for1(i,n)if(i&1)ans^=a[i]-a[i+1];43         if(ans)printf("TAK\n");else printf("NIE\n");44     }45     return 0;46 }
View Code

 

BZOJ1115: [POI2009]石子遊戲Kam

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.