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. 关于 Llama 2 的一切资源,我们都帮你整理好了

    Llama 2 是一个由 Meta 开发的大型语言模型,是 LLaMA 1 的继任者.Llama 2 可通过 AWS.Hugging Face 获取,并可以自由用于研究和商业用途.Llama 2 预训 ...

  2. 《SQL与数据库基础》23. 读写分离

    目录 读写分离 一主一从 准备 配置 双主双从 准备 配置 主库配置 从库配置 从库关联主库 主库相互复制 双主双从读写分离 本文以 MySQL 为例.以 MyCat 数据库中间件为例,通过 MyCa ...

  3. 利用BGP Anycast 实现DNS 服务的高可用测试

    一.背景     根据当前某公司内部生产系统容器平台架构设计,在各生产线边缘机房部署容器平台,与数据中心容器平台形成纵向冗余,在此情况下,传统部署在数据中心机房的DNS系统成为容器平台业务服务的短板, ...

  4. Go语句与表达式深度解析:全案例手册

    关注公众号[TechLeadCloud],分享互联网架构.云服务技术的全维度知识.作者拥有10+年互联网服务架构.AI产品研发经验.团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云认证的资 ...

  5. 【ChatGPT-应用篇】基于chatGPT覆盖测试过程的初步探索

    1.前言 22年底ChatGPT就已风靡行业内外,简单来说,它是基于自然语言生成式 AI 模型,打造的一款聊天机器人.是 OpenAI 于 11 月 30 日推出的最新作品,供公众免费测试.他可以根据 ...

  6. 【效率提升】maven 转 gradle 实战

    一.灵魂三问 1.gradle 是什么? 一个打包工具, 是一个开源构建自动化工具,足够灵活,可以构建几乎任何类型的软件,高性能.可扩展.能洞察等.其中洞察,可以用于分析构建过程中数据,提供分析参考, ...

  7. 给定3个整数a、b、c,计算表达式(a+b)/c的值,/是整除运算。[无解]

    题目4-2:给定3个整数a.b.c,计算表达式(a+b)/c的值,/是整除运算. 给定3个整数a.b.c,计算表达式(a+b)/c的值,/是整除运算. 输入格式:输入仅一行,包括三个 整数a.b.c, ...

  8. hash code

    值相同却可能有不同的hashcode //对象值到底指什么?(x.equals(y) == true)应该并不代表对象值相同 class A { A(){} public boolean equals ...

  9. jmeter生成HTML性能测试报告(非GUI的命令)

    非GUI的命令(在cmd执行即可 不需要打开jmeter) 使用命令:jmeter  -n  -t  [jmx file]  -l  [jtl file]  -e  -o   [report path ...

  10. Swagger系列:SpringBoot3.x中使用Knife4j

    目录 一.简介 二.版本说明 三.使用 四.效果图 一.简介 官网:https://doc.xiaominfo.com/ Knife4j是一个集Swagger2 和 OpenAPI3 为一体的增强解决 ...