CF 39E What Has Dirichlet Got to Do with That? (博弈)
轉載請註明出處,謝謝http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
題意:給出a ^ b,兩個人輪流操作,可以 a + 1 也可以 b + 1,誰先使得a ^ b >= n則輸。
由於題目給的n並不大,1e9的範圍,如果不考慮a == 1 or b == 1的情況下
a最大為sqrt (n) ,b最大為ln(n) / ln(2)。
所以我們可以處理出所有的a > 1 && b > 1的情況,dp[i][j]表示當前局面為 i ^ j下的輸贏情況,記憶化搜尋一下。
然後便是考慮特殊情況
如果 a == 1 && b > 1,這種情況可能導致平局,便是兩個輪流只在b上操作,導致結果一直為1。
那麼我們可以模擬當前是否考慮操作a,由於a > 1 && b > 1所有情況的輸贏已經處理過了。
所以只要當前a + 1 , b是個必敗態,則會考慮操作。否則可能是平局。當然了a也是有個上限的。直接列舉模擬就行。
如果b == 1 && a > 1,這種情況下同理。
列舉模擬當前是否考慮操作b,如果a , b + 1是個必敗態,那麼會考慮操作。
我們可以處理到上界sqrt (n),因為一旦 a > sqrt (n),只要操作b,便會失敗。後面的局面就已經確定了。
如果 a == 1 && b == 1,先手操作a的話,便成了a > 1 && b == 1的局面,操作b的話便 成了a == 1 && b > 1的局面。
兩種情況都處理一下,如果有必勝,肯定先手勝,否則如果有平局,則考慮平局,否則先手敗。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
#define Key_value ch[ch[root][1]][0]
using namespace std;
typedef long long LL;
const int N = 35000;
const int M = 31;
int a , b , n , dp[N][M];
// a ^ b >= n ? true : false
bool check (int a , int b , int n) {
LL ret = 1LL;
for (int i = 0 ; i < b ; i ++) {
ret = ret * a;
if (ret <= 0 || ret >= n)
return true;
}
return false;
}
int dfs (int a , int b) {
if (dp[a][b] != -1) return dp[a][b];
if (check (a , b , n)) {
return dp[a][b] = 1;
}
int ret = 0;
ret |= ! dfs (a + 1 , b);
ret |= ! dfs (a , b + 1);
return dp[a][b] = ret;
}
bool b_is_one (int a , int b = 1) {
int up = sqrt (n - 0.0000001);
int turn = 0;
while (a <= up) {
if (dp[a][b + 1] == 0) {
return turn == 0;
}
a ++;turn = 1 - turn;
}
int remain = n - 1 - a;
turn = (turn + remain) % 2;
return turn;
}
int a_is_one (int b , int a = 1) {
if (check (a , b , n)) return 0;
else {
int turn = 0;
for ( ; !check (2 , b , n) ; b ++) {
if (dp[a + 1][b] == 0) {
if (turn == 0) return 1;
else return -1;
}
turn = 1 - turn;
}
return 0;
}
}
int main () {
#ifndef ONLINE_JUDGE
freopen ("input.txt" , "r" , stdin);
// freopen ("ouput.txt" , "w" , stdout);
#endif
memset (dp , -1 , sizeof (dp));
cin >> a >> b >> n;
if (check (a , b , n)) {
puts ("Masha");
return 0;
}
for (int i = 2 ; i < N ; i ++) {
for (int j = 2 ; j < M ; j ++) {
dp[i][j] = dfs (i , j);
}
}
if (a !=1 && b != 1) {
puts (dp[a][b] ? "Masha" : "Stas");
return 0;
}
if (a == 1 && b == 1) {
int c = a_is_one (b + 1) , d = b_is_one (a + 1);
if (c < 0 || d == 0) puts ("Masha");
else if (c == 0) puts ("Missing");
else puts ("Stas");
}
else if (b == 1) {
puts (b_is_one (a) ? "Masha" : "Stas");
}
else if (a == 1) {
int c = a_is_one(b);
if (c == 0) puts ("Missing");
else if (c == 1) puts ("Masha");
else puts ("Stas");
}
return 0;
}
相關文章
- What does "xargs grep" do?
- You've got to find what you loveGo
- robbin的自白:You've got to find what you loveGo
- 【HPUX】What to do when mount does not work on HP-UX?UX
- What can men do against such reckless hate?AI
- CF1972B.Coin Games-博弈GAM
- QUESTION :What kind of shared storage do you use for Oracle RAC?Oracle
- What happens when exadata has lost two disks?(轉)APP
- When you issue "ALTER TABLESPACE xxx READ ONLY",what will oracle do?Oracle
- PostgreSQL 原始碼解讀(159)-PG Tools#6(What does pg_rewind do)SQL原始碼
- PostgreSQL 原始碼解讀(158)-PG Tools#5(pg_basebackup:what does db server do)SQL原始碼Server
- GoldenGate - What is supported and what is not ....Go
- What is it?
- She Got her ownGo
- 博弈論
- 博弈論入門之威佐夫博弈
- 博弈學習 (二) 斐波那契博弈
- Just Do IT
- What is WebpackWeb
- What is Babel?Babel
- What is MySQL?MySql
- What is Gravity
- what is life?
- What is wrong?
- mysqldump Got error: 1045MySqlGoError
- mysqld got signal 8MySqlGo
- 博弈論入門之斐波那契博弈
- 博弈總結
- 擁抱極限程式設計(Do do XP)程式設計
- jQuery has()jQuery
- jQuery :has()jQuery
- oracle hasOracle
- Do more, fasterAST
- mysqldump Got error 1290MySqlGoError
- What is an SQL relation?SQL
- What time is it on Mars?
- what the fuck java is?Java
- What is tradebit?