Problem Statement

This is an interactive task (where your program interacts with the judge's program via Standard Input and Output).

You are given integers $N$, $L$, and $R$.

You play the following game against the judge:

There are $N$ cards numbered $1$ through $N$ on the table.

The players alternately perform the following operation:

  • choose an integer pair $(x, y)$ satisfying $1 \leq x \leq N$, $L \leq y \leq R$ such that all of the $y$ cards $x, x+1, \dots, x+y-1$ remain on the table, and remove cards $x, x+1, \dots, x+y-1$ from the table.

The first player to become unable to perform the operation loses, and the other player wins.

Choose whether to go first or second, and play the game against the judge to win.

Constraints

  • $1 \leq N \leq 2000$
  • $1 \leq L \leq R \leq N$
  • $N$, $L$, and $R$ are integers.

Input and Output

This is an interactive task (where your program interacts with the judge's program via Standard Input and Output).

Initially, receive $N$, $L$, and $R$, given from the input in the following format:

$N$ $L$ $R$

First, you choose whether to go first or second. Print First if you choose to go first, and Second if you choose to go second.

Then, the game immediately starts. If you choose to go first, the judge goes second, and vice versa. You are to interact with the judge via input and output until the game ends to win the game.

In your turn, print an integer pair $(x, y)$ that you choose in the operation in the following format. If there is no $(x, y)$ that you can choose, print $(x, y) = (0, 0)$ instead.

$x$ $y$

In the judge's turn, the judge print an integer pair $(a, b)$ in the following format:

$a$ $b$

Here, it is guaranteed that $(a, b)$ is of one of the following three kinds.

  • If $(a, b) = (0, 0)$: the judge is unable to perform the operation. In other words, you have won the game.
  • If $(a, b) = (-1, -1)$: you have chosen an illegal $(x, y)$ or printed $(0, 0)$. In other words, you have lost the game.
  • Otherwise: the judge has performed the operation with $(x,y) = (a,b)$. It is guaranteed that judge chooses valid $(x, y)$.

If the judge returns $(a,b)=(0,0)$ or $(a,b)=(-1,-1)$, the game has already ended. In that case, terminate the program immediately.

Notes

  • After each output, add a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
  • If an invalid output is printed during the interaction, or if the program terminates halfway, the verdict will be indeterminate. Especially, note that if a runtime error occurs during the execution of the program, you may get a WA or TLE verdict instead of a RE verdict.
  • Terminate the program immediately after the game ends. Otherwise, the verdict will be indeterminate.

Sample Interaction

The following is a sample interaction where $N = 6, L = 1$, and $R = 2$.

Input Output Description
6 1 2 Initially, you are given integers $N$, $L$, and $R$.
First You choose to go first and start the game.
2 1 $(x, y) = (2, 1)$ is chosen to remove card $2$.
3 2 $(x, y) = (3, 2)$ is chosen to remove cards $3, 4$.
6 1 $(x, y) = (6, 1)$ is chosen to remove card $6$.
5 1 $(x, y) = (5, 1)$ is chosen to remove card $5$.
1 1 $(x, y) = (1, 1)$ is chosen to remove card $1$.
0 0 The judge is unable to perform the operation, so you win.

首先先玩着试一下。假设 \(N=11,L=R=3\) 吧。\(0\) 表示未取,\(1\) 表示已取。

0 0 0 0 0 0 0 0 0 0 0

突然发现,如果我们第一步去了中间的部分,那么情况就会变成

0 0 0 0 1 1 1 0 0 0 0

然后此时左右情况对称,对手怎么取,我在另一个方向同样方式取。这样的话对手怎么做,我都有方法做出反映。此时先手必胜。

但是这种方法不能用在所有情况。比如 \(N=11,L=R=2\) 时就不能构造出两个对称的位置。但是这样可以解决所有 \(L\ne R\) 的情况。

我们现在只用研究 \(L=R\) 的情况了。这个时候可以用 SG 函数解决。

根据 SG 函数的定义,设 \(f_i\) 为有 \(i\) 个数时的 SG 函数,那么 \(f_i=\operatorname{mex}\limits_{j\le i-l}\{f_j\oplus f_{i-l-j}\}\)。如果 \(f_n\) 等于0,后手必胜。否则先手必胜。

若 \(f_n\ne 0\),可以先枚举每种方案,找到一种合法方案使得取完后 SG 函数为 0。而后面对手做出操作时,也一样找到一种方案使得 SG 函数为 \(0\)。这样一直维护,对手怎么操作我都有后续操作。所以肯定必胜。

SG 函数可以每次重算,不用想烦人的维护。反正瓶颈不在这。

#include<bits/stdc++.h>
const int N=2005;
int n,l,r,md,k,x,y,f[N],t[N],s[N],nxt[N],lst[N];
int main()
{
scanf("%d%d%d",&n,&l,&r);
if(l!=r)
{
if(n-l&1)
k=l+1;
else
k=l;
md=n+k>>1;
puts("First");
printf("%d %d\n",md-k+1,k);
fflush(stdout);
while(1)
{
scanf("%d%d",&x,&y);
if(!x)
break;
if(x<md)
printf("%d %d\n",x+md,y);
else
printf("%d %d\n",x-md,y);
fflush(stdout);
}
}
else
{
for(int i=l;i<=n;i++)
{
memset(s,0,sizeof(s));
for(int j=0;j<=i-l;j++)
s[f[j]^f[i-l-j]]=1;
for(int j=0;j>=0;j++)
if(!s[j])
f[i]=j,j=-5;
}
if(f[n])
{
puts("First");
for(int i=0;i<=n;i++)
{
if(!(f[i]^f[n-l-i]))
{
printf("%d %d\n",i+1,l);
for(int j=i+1;j<=i+l;j++)
t[j]=1;
i=n+1;
}
}
}
else
puts("Second");
t[n+1]=t[0]=1;
fflush(stdout);
while(1)
{
scanf("%d%d",&x,&y);
if(!x&&!y)
break;
k=0;
for(int i=x;i<=x+y-1;i++)
t[i]=1;
for(int i=n;i>=0;i--)
{
nxt[i]=nxt[i+1];
if(t[i+1])
nxt[i]=i+1;
if(t[i])
k^=f[nxt[i]-i-1];
}
for(int i=1;i<=n;i++)
{
s[i]=s[i-1]+t[i],lst[i]=lst[i-1];
if(t[i-1])
lst[i]=i-1;
}
for(int i=1;i+l-1<=n;i++)
{
if(s[i-1]==s[i+l-1])
{
if(!(k^f[nxt[i]-lst[i]-1]^f[nxt[i+l-1]-i-l]^f[i-lst[i]-1]))
{
// printf("%d %d\n",k^f[nxt[i]-lst[i]-1],f[nxt[i+l-1]-i-l]^f[i-lst[i]-1]);
printf("%d %d\n",i,l);
for(int j=i;j<i+l;j++)
t[j]=1;
break;
}
}
}
fflush(stdout);
}
}
}

[ABC278G] Generalized Subtraction Game的更多相关文章

  1. Analysis of Two-Channel Generalized Sidelobe Canceller (GSC) With Post-Filtering

    作者:凌逆战 地址:https://www.cnblogs.com/LXP-Never/p/12071748.html 题目:带后置滤波的双通道广义旁瓣相消器(GSC)的分析 作者:Israel Co ...

  2. [LeetCode] Generalized Abbreviation 通用简写

    Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...

  3. 广义线性模型(Generalized Linear Models)

    前面的文章已经介绍了一个回归和一个分类的例子.在逻辑回归模型中我们假设: 在分类问题中我们假设: 他们都是广义线性模型中的一个例子,在理解广义线性模型之前需要先理解指数分布族. 指数分布族(The E ...

  4. LeetCode Generalized Abbreviation

    原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...

  5. PAT 解题报告 1050. String Subtraction (20)

    1050. String Subtraction (20) Given two strings S1 and S2, S = S1 - S2 is defined to be the remainin ...

  6. [Locked] Generalized Abbreviation

    Write a function to generate the generalized abbreviations of a word. Example:Given word = "wor ...

  7. [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)

    Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will ...

  8. Regression:Generalized Linear Models

    作者:桂. 时间:2017-05-22  15:28:43 链接:http://www.cnblogs.com/xingshansi/p/6890048.html 前言 本文主要是线性回归模型,包括: ...

  9. [leetcode-592-Fraction Addition and Subtraction]

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  10. [LeetCode] Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

随机推荐

  1. CodeForces 1332E Height All the Same

    题意 对于一个\(n*m\)的矩阵,有两种操作 一个格子加二 一个格子和另一个相邻的格子同时加一 通过这两种操作最终使得所有矩阵元素相等 对于矩阵元素来说,有\(L\leq a_{i,j}\leq R ...

  2. 2D KD-Tree实现

    KD-tree 1.使用背景 在项目中遇到一个问题: 如何算一个点到一段折线的最近距离~折线的折点可能有上千个, 而需要检索的点可能出现上万的数据量, 的确是个值得思考的问题~ 2.暴力解法 有个比较 ...

  3. 升讯威在线客服系统的并发高性能数据处理技术:PLINQ并行查询技术

    我在业余时间开发维护了一款免费开源的升讯威在线客服系统,也收获了许多用户.对我来说,只要能获得用户的认可,就是我最大的动力. 最近客服系统成功经受住了客户现场组织的压力测试,获得了客户的认可. 客户组 ...

  4. java中有哪些并发的List?只知道一种的就太逊了

    java中有很多list,但是原生支持并发的并不多,我们在多线程的环境中如果想同时操作同一个list的时候,就涉及到了一个并发的过程,这时候我们就需要选择自带有并发属性的list,那么java中的并发 ...

  5. java获取服务器ip地址的工具类

    参考: https://www.cnblogs.com/raphael5200/p/5996464.html 代码实现 import lombok.extern.slf4j.Slf4j; import ...

  6. 【爬虫笔记】Python爬虫简单运用爬取代理IP

    一.前言 近些年来,网络上的爬虫越来越多,很多网站都针对爬虫进行了限制,封禁了一些不规则的请求.为了实现正常的网络爬虫任务,爬虫常用代理IP来隐藏自己的真实IP,避免被服务器封禁.本文将介绍如何使用P ...

  7. 两种方式,创建有返回值的DB2函数

    函数场景:路径信息由若干个机构编码组成,且一个机构编码是9位字符. 要求:获取路径信息,并且删除路径中包含'99'开头的机构编码. 从客户端及服务器端分别创建ignore99(pathinfo var ...

  8. PDFPlumber使用入门

    目录 背景 教程开始 应用场景 安装 命令行使用 可选参数 Python包 简单样例 读取PDF pdfplumber.PDF类 pdfplumber.Page类 对象(Object) chars / ...

  9. JDK21的虚拟线程是什么?和平台线程什么关系?

    虚拟线程(Virtual Thread)是 JDK 而不是 OS 实现的轻量级线程(Lightweight Process,LWP),由 JVM 调度.许多虚拟线程共享同一个操作系统线程,虚拟线程的数 ...

  10. 文心一言 VS 讯飞星火 VS chatgpt (118)-- 算法导论10.3 3题

    三.用go语言,在ALLOCATE-OBJECT和 FREE-OBJECT过程的实现中,为什么不需要设置或重置对象的 prev 属性呢? 文心一言: 在 Go 语言中,ALLOCATE-OBJECT ...