H. Split Game

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

For a long time, rich clientele of Binary Casino has been requesting a new way to gamble their money. To fulfill their wishes, the director of Binary Casino decided to introduce a new game called Split Your Tokens.

This game is played only when a customer is about to exit the casino. Instead of exchanging tokens won during his visit, he may take up casino's challenge and bet all of his earned tokens on winning this game. Should the customer lose, all of his tokens are lost in favor of the casino.

When the game starts, the customer splits his tokens into NN piles with not necessarily same amount of tokens in each pile. The customer and the casino then exchange turns ­ in this game we denote the customer as the first player and the casino as the second player. Each player in his turn decides which pile he wants to split and chooses a positive integer KK which is smaller than the size of the selected pile. Then the player splits the selected pile into as many piles of size KK as possible. If any tokens remain, they form another pile on their own. A player loses the game when he can not do any more splitting. The customer (first player) always plays first.

The director of Binary Casino is however not sure, whether this game will be profitable for the casino in the long term. Your task is thus to determine, for a given configuration of piles, which player wins when both players play optimally.

Input

The first line contains one integer NN (1≤N≤20001≤N≤2000), the number of piles. The second line contains a sequence of NN integers PiPi (1≤Pi≤20001≤Pi≤2000), PiPi represents the number of tokens in the ii-th pile.

Output

Output a single line with either "First" or "Second", depending on which player wins the game if both play optimally.

Examples
input
3
1 2 3
output
First
input
3
1 2 2
output
Second

题意概括:

给出 N 堆物品, 两个玩家轮流选择将其中一个大小为 M 的堆 分成最多的 大小为 K (K < 当前选择的堆的大小)的堆,若 M%K 有剩余,则剩余的自成一堆。

最后所有堆大小都为 1 时不可再分,不能再分堆的玩家输。

解题思路:

因为SG函数的作用就是把博弈的状态当成一个点,然后形成一张 有向图,后继状态也就是后继结点,通过转移图上结点的状态最后求的起始点的结果。

以往 都是选择某一堆 取走若干 然后留下一堆,所以 结点的后继结点就对应剩下的那个状态。也就是说 把每一堆单独作为一个 NIM游戏,最后再考虑所有堆最后异或的结果。

不过 这里是选择某一堆 然后分成若干个小堆,就相当于又变成了一个 NIM游戏,不过考虑到分成的若干小堆 有相同的 和 不同的两部分,相同的直接判奇偶即可,如果有不同的(即分剩下的)再异或上这种状态即可。也就是先把每一堆作为一个 NIM 游戏,每分一次就又玩一次 NIM 游戏。

AC code:

 #include <bits/stdc++.h>
#define inc(i, j, k) for(int i = j; i <= k; i++)
#define rep(i, j, k) for(int i = j; i < k; i++)
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
#define INF 0x3f3f3f3f
#define LL long long
#define MEM(i, j) memset(i, j, sizeof(i));
#define gcd(i, j) __gcd(i, j)
using namespace std;
const int MAXN = 2e5+;
const int MM = 2e3+;
int sg[MAXN];
int vis[MAXN]; void getsg()
{
int cnt, res;
sg[] = ;
MEM(vis, -);
inc(i, , MM){
rep(j, , i){
cnt = i/j;
res = i%j;
if(cnt%) vis[sg[res]^sg[j]] = i;
else vis[sg[res]] = i;
}
int k = ;
while(vis[k] == i && k < MM) k++;
sg[i] = k;
}
} int main()
{
getsg();
int N;
scanf("%d", &N);
int ans = , tp;
while(N--){
scanf("%d", &tp);
ans^=sg[tp];
}
if(ans) puts("First");
else puts("Second");
return ;
}

2018 - 2019 CTU Open Contest H. Split Game 【SG函数】的更多相关文章

  1. 2018 - 2019 CTU Open Contest E. Locker Room 【后缀数组】

    任意门:http://codeforces.com/gym/101954/problem/E E. Locker Room time limit per test 2.0 s memory limit ...

  2. COCI 2018/2019 CONTEST #2 T4 Maja T5Sunčanje Solution

    COCI 2018/2019 CONTEST #2 T4 T5 Solution abstract 花式暴力 #2 T5 Sunčanje 题意 按顺序给你1e5个长方形(左下角坐标&& ...

  3. 20172328 2018—2019《Java软件结构与数据结构》第二周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第二周学习总结 概述 Generalization 本周学习了第三章集合概述--栈和第四章链式结构--栈.主要讨论了集合以 ...

  4. 2018 German Collegiate Programming Contest (GCPC 18)

    2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...

  5. (寒假GYM开黑)2018 German Collegiate Programming Contest (GCPC 18)

    layout: post title: 2018 German Collegiate Programming Contest (GCPC 18) author: "luowentaoaa&q ...

  6. 2019 Multi-University Training Contest 7

    2019 Multi-University Training Contest 7 A. A + B = C 题意 给出 \(a,b,c\) 解方程 \(a10^x+b10^y=c10^z\). tri ...

  7. 2019 Multi-University Training Contest 2

    2019 Multi-University Training Contest 2 A. Another Chess Problem B. Beauty Of Unimodal Sequence 题意 ...

  8. HDU校赛 | 2019 Multi-University Training Contest 6

    2019 Multi-University Training Contest 6 http://acm.hdu.edu.cn/contests/contest_show.php?cid=853 100 ...

  9. HDU校赛 | 2019 Multi-University Training Contest 5

    2019 Multi-University Training Contest 5 http://acm.hdu.edu.cn/contests/contest_show.php?cid=852 100 ...

随机推荐

  1. IDEA 导出maven项目所有的依赖包到指定的目录

    问题产生: 需要把一个maven工程调整结构,部署到一个新环境中,需要把依赖的jar包,单独打到一个目录中. 解决方案: 需要两步便可以copy到指定的目录中. 第一步: 找到maven projec ...

  2. 深入理解MyBatis的原理(三):配置文件用法(续)

    前言:前文讲解了 MyBatis 的配置文件一部分用法,本文将继续讲解 MyBatis 的配置文件的用法. 目录 1.typeHandler 类型处理器 2.ObjectFactory 3.插件 4. ...

  3. spring boot 入门一 构建spring boot 工程

    最近在学习Spring boot,所以想通过博客的形式和大家分享学习的过程,同时也为了更好的学习技术,下面直接进入Spring boot的世界. 简介 spring boot 它的设计目的就是为例简化 ...

  4. ctcms Nginx 伪静态

    location /whole { rewrite ^/whole/(.+).html$ /index.php?c=whole&key=$1; } location /show { rewri ...

  5. 用jquery实现带左右按键的轮播图

    成品如下: 简单来说就是点击“右”按钮时,转换到右边的下一幅图片,同时上面的小方块颜色也跟着改变,如果已经是最后一幅图片,再点击“右”,则转换到第一幅图片,是直接向左移找到第一幅图的,明天再做一下无缝 ...

  6. Java设计模式—建造者模式

    建造模式:        将一个复杂的对象的构建与它的表示分离,使得同样的构建 过程可以创建不同的. 建造模式表示是将复杂的内部创建封装在内部,对于外部调用的人来说,只需要传入建造者和建造工具,对于内 ...

  7. 报表和aspx结合后在aspx页面实现工具栏的方法

     现在有好多客户在.net中用到了快逸报表,主要方法是把报表部署在一个j2ee应用中,.net部署在IIS中,把报表嵌入到aspx页面中的iframe里面.如果原本aspx页面中一些按钮或者工具, ...

  8. mysql navicat 及命令行 创建、删除数据库

    1.命令行创建数据库 create database mybatis default character set utf8 collate utf8_general_ci; drop database ...

  9. 前端构建工具 Gulp.js 上手实例

    在软件开发中使用自动化构建工具的好处是显而易见的.通过工具自动化运行大量单调乏味.重复性的任务,比如图像压缩.文件合并.代码压缩.单元测试等等,可以为开发者节约大量的时间,使我们能够专注于真正重要的. ...

  10. Java 性能调优指南之 Java 集合概览

    [编者按]本文作者为拥有十年金融软件开发经验的 Mikhail Vorontsov,文章主要概览了所有标准 Java 集合类型.文章系国内 ITOM 管理平台 OneAPM 编译呈现,以下为正文: 本 ...