ãããé¢ç½ãã£ãï¼
åé¡æ¦è¦
åã®è¢ããã£ã¦ãããããã«ã¯
- æ¿è¾ãããã æ¬
- ç¾å³ãããããã æ¬
ããããå æã¨å¾æã以ä¸ã®ãã¨ã交äºã«è¡ãã
- æ¿è¾ãããã 0 æ¬ã¾ã㯠1 æ¬åãåºã
- ç¾å³ãããããã好ããªæ¬æ°ã ãåãåºã
- ãã ããæ¿è¾ãããã¨ç¾å³ãããããã¨ãåè¨ã㦠1 æ¬ä»¥ä¸åãåºããªããã°ãªããªã
å ã«æä½ãè¡ããªããªã£ãæ¹ãè² ãã§ãããåæ¹æåãå°½ãããã¨ããã©ã¡ããåã¤ãï¼
å¶ç´
èãããã¨
ããã«ã Grundy (Nim)ï¼ï¼ï¼ï¼ï¼
ããã¹ããã¨ã¯æããã§ãããããã®è¢ã® Grundy æ°ãæ±ããã°ãããGrundy æ°ã¯ DP ã§æ±ãããããã ãã®ç¶æ
空éã¯æ±ããªãããã£ã¦ããã£ã¨ä½ãããè¦åãããã¯ãããããè¦åºãããã«ãæãã³ã³ãã¥ã¼ã¿ã§å®é¨ããããã
ããããã¨ããæã§å®é¨ããããã³ã³ãã¥ã¼ã¿ã§å®é¨ãããã¯ãã¤ãè¿·ããã¾ãã¯æã§ç°¡åã«å®é¨ãã¦ã¿ã¦ããã®ç¯å²ã§æ±ããããã°ããã§ããã¦ã糸å£ãã¤ãã¿ãããªãã£ããã³ã³ãã¥ã¼ã¿ã§ã®å®é¨ã«è¸ã¿åãã®ãã¡ããã©ããããã
Grundy æ°ãæã§æ±ããã¨ãããªæãããã¨ãã°èµ¤ãã¹ã®å¤ãæ±ããããã«ã¯ã赤ãã¹ãã 1 æã§è¡ããå ´æã§ããé»è²ãã¹ã«ã¤ãã¦ã® Grundy æ°ã並ã¹ã¦ããã¦
0, 1, 2, 3, 4, 5, 7
ããã 0 ããé ã«è¦ã¦ãã£ã¦ãæåã«éåããæ°å¤ãçãã¨ãªããã¤ã¾ã赤ãã¹ã®å¤ã¯ 6 ã«ãªãã
ãã®è¡¨ãåããã¨ãå³ä¸ã®æ¹ã¯ ã«ãªã£ã¦ãããã¨ãããããããã¦å·¦ä¸ã®æ¹ã®å¤ã¯äº¤äºã«å ¥ãæ¿ãã£ã¦ããæ§åãã¿ã¦ã¨ãããå ·ä½çã«ã¯ãä¸å³ã®èµ¤ã©ã¤ã³ããä¸ (赤ã©ã¤ã³å«ã) ã«ããã¦ã¯ ã¨ãªã£ã¦ãã¦ãããããä¸ã«ã¤ãã¦ã¯ã2 ã¤ä¸ã®å¤ã¨çãããã¨ããæãã«ãªã£ã¦ããã
å ·ä½çã«ã¯ã以ä¸ã®ããã«ãªã
long long Grundy(long long H, long long B) { if (H <= B + 1) return H + B; if ((H - B) % 2 == 0) return B * 2; else return B * 2 + 1; }
以ä¸ãã¾ã¨ãã¦ãã³ã¼ãã¯ãããªæãã§éã£ããè¨ç®é㯠ã
#include <bits/stdc++.h> using namespace std; long long Grundy(long long H, long long B) { if (H <= B + 1) return H + B; if ((H - B) % 2 == 0) return B * 2; else return B * 2 + 1; } int main() { int N; cin >> N; long long res = 0; for (int i = 0; i < N; ++i) { long long H, B; cin >> H >> B; res ^= Grundy(H, B); } if (res) cout << "matsu" << endl; else cout << "prd" << endl; }