转载请注明出处,谢谢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;
}

CF 39E What Has Dirichlet Got to Do with That? (博弈)的更多相关文章

  1. CF 39E. What Has Dirichlet Got to Do with That?(记忆化搜索+博弈论)

    传送门 解题思路 首先很好写出一个\(O(ab)\)的记搜,但发现这样无法处理\(a=1\)和\(b=1\)的情况,这两种情况需要特判.首先\(a=1\)的情况,就是如果当前选手让\(a+1\)必胜, ...

  2. Codeforces 39E What Has Dirichlet Got to Do with That? 游戏+内存搜索

    主题链接:点击打开链接 意甲冠军: 特定 a一箱 b球 不变n (球和箱子都不尽相同,样的物品) 设 way = 把b个球放到a个箱子中的方法数, 若way >= n则游戏结束 有2个人玩游戏. ...

  3. codeforce -39E-What Has Dirichlet Got to Do with That?(博弈+dfs)

    You all know the Dirichlet principle, the point of which is that if n boxes have no less than n + 1  ...

  4. [UOJ Round#4 A] [#51] 元旦三侠的游戏 【容斥 + 递推】

    题目链接:UOJ - 51 据说这题与 CF 39E 类似. 题目分析 一看题目描述,啊,博弈论,不会!等待爆零吧... 这时,XCJ神犇拯救了我,他说,这题可以直接搜啊. 注意!是用记忆化搜索,状态 ...

  5. NOI2019网络同步赛游记

    我发的邮件**f没收到,后来去专门询问才整到一个名额(估计是嫌我太菜,参加了也是垫底) day -1 上午写了到类似随机游走的高斯消元期望dp,然后颓颓颓 下午打洛咕月赛.T1一直50pts,后来才知 ...

  6. 开源推荐系统Librec中recommender模块算法了解——cf模块

    1.      k近邻(k-NearestNeighbor)算法介绍及在推荐系统中的应用 https://zhuanlan.zhihu.com/p/25994179 k近邻(k-NearestNeig ...

  7. spark MLlib 概念 4: 协同过滤(CF)

    1. 定义 协同过滤(Collaborative Filtering)有狭义和广义两种意义: 广义协同过滤:对来源不同的数据,根据他们的共同点做过滤处理. Collaborative filterin ...

  8. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. JAVA大整数傻瓜入门

    http://blog.csdn.net/skiffloveblue/article/details/7032290..先记着

  2. WPF ICommand 用法

    基础类,继承与ICommand接口 using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  3. 在iOS上present一个半透明的viewController

    UIViewController *viewController = [[UIViewController alloc]init]; UIViewController* controller = se ...

  4. (转)Apache+Tomcat集群配置

    本文Apache+Tomcat集群配置 基于最新的Apache和Tomcat,具体是2011年4月20日最新的Tomcat和Apache集群和负载均衡配置. 准备环境 Apache Apache是ht ...

  5. 在多线程环境中使用Jedis

    Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口. Jedis不是线程安全的.故不应该在多线程环境中共用一个Jedis实例.可是.也应该避免直接 ...

  6. Android日志系统驱动程序Logger源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6595744 我们知道,在Android系统中, ...

  7. qt超强精美绘图控件 - QCustomPlot一览 及 安装使用教程

    1.概述 QCustomPlot 是一个超强超小巧的qt绘图类,非常漂亮,非常易用,只需要加入一个qcustomplot.h和qcustomplot.cpp文件即可使用,远比qwt方便和漂亮,可以自己 ...

  8. 一些基础的.net用法

    一.using 用法 using 别名设置 using 别名 = System.web 当两个不同的namespace里有同名的class时.可以用 using aclass = namespace1 ...

  9. Sql server Lock

    http://www.cnblogs.com/wuyifu/archive/2013/11/28/3447870.html

  10. tomcat6.0目录和server.xml详解

    Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,目前最新版本是6.x,相对5.x性能提升很多,主要优化了内存使用,增强IO能力,重新构造集群功能. 近期对Tomcat6.x作深入学习, ...