Codeforces 1404 D.Game of Pairs

给定\(2n\)个数\(1,2,...,2n\),A 和 B 将进行交互,规则如下:

  • A 需要将元素分成 n 组 \(\mathbf{pair}\)(二元组)
  • B 从每组 \(\mathbf{pair}\)中选择一个元素,如果权值和是 \(2n\) 的倍数,那么 B 胜,否则 A 胜。

你需要选择 A/B 中的一者扮演角色,并取得胜利。

\(n\le 5\times 10^5\).

老子懒得讲了,你们TMD对着代码自己发愣去吧。

由于可以自选角色,所以我们分别考虑两个角色的必胜情况。

考虑A,我们首先发现如下性质:

  • 由于\(\sum\limits_{i=1}^n=\frac{n\times(n-1)}{2}\),所以当\(n\)是偶数时,\(\sum_{i=1}^n\)一定不是\(n\)的倍数。

于是针对n为偶数的情况我们可以很容易地构造出无解的方案:将\(i\)和\(i+n(i\in[1,n])\)放进一组,那么无论B怎么选,最后的总和一定是形如\(\frac{n\times(n-1)}{2}+kn\)的某个数,这个式子的后一项一定是n的倍数,而前一项一定不是n的倍数,所以A必胜。

那么我们继续考虑n为奇数的情况。

然而遗憾的是,这种情况下,B是存在必胜策略的...

考虑B,我们重新审视A中发现的性质:

  • 由于\(\sum\limits_{i=1}^{n}=\frac{n\times(n-1)}{2}\),所以当n是奇数是,\(\sum_{i=1}^{n}\)一定是n的倍数。

然后如何取这个方法实在是抽象,难以描述,所以

非常抱歉,这篇文章从这里开始又咕了。

code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=500005;
int n,fa[maxn<<1],siz[maxn<<1];
int tp[maxn<<1],xorv[maxn<<1],used[maxn<<1];
int Q[maxn],topc;
bool vis[maxn<<1];
inline int read(){
int res=0,f_f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f_f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=(res<<3)+(res<<1)+(ch-'0'),ch=getchar();
return res*f_f;
}
inline int get_fa(int x){
return x==fa[x]?x:fa[x]=get_fa(fa[x]);
}
inline void merge_fa(int x,int y){
siz[get_fa(x)]+=siz[get_fa(y)];
fa[get_fa(y)]=get_fa(x);
}
inline void dfs(int u){
vis[u]=true;
int v=xorv[tp[u]]^u;
v=(v>n)?v-n:v+n;
if(vis[v]) return;
merge_fa(u,v),dfs(v);
}
int main(){
n=read();
if(n%2==0){
printf("First\n");
cout.flush();
for (int i=1;i<=(n<<1);i++){
if(i^(n<<1)) printf("%d ",(i-1)%n+1);
else printf("%d\n",(i-1)%n+1);
cout.flush();
}
}
else{
printf("Second\n");
cout.flush();
for (int i=1;i<=(n<<1);i++) tp[i]=read(),xorv[tp[i]]^=i;
for (int i=1;i<=(n<<1);i++) fa[i]=i;
for (int i=n+1;i<=(n<<1);i++) siz[i]=1;
for (int i=1;i<=(n<<1);i++){
if(vis[i]) continue;
dfs(i);
}
int ans=(n+1)/2&1;
for (int i=1;i<=(n<<1);i++){
if(i^get_fa(i)) continue;
int v=get_fa(xorv[tp[i]]^i);
if(v>i) continue;
used[i]=1,ans^=(siz[i]&1);
}
if(ans){
for (int i=1;i<=(n<<1);i++){
if(!used[i]) continue;
int v=get_fa(xorv[tp[i]]^i);
if((siz[v]^siz[i])&1){
used[i]=0,used[v]=1;
break;
}
}
}
for (int i=1;i<=(n<<1);i++){
if(used[get_fa(i)]) Q[++topc]=i;
}
for (int i=1;i<=topc;i++){
if(i^topc) printf("%d ",Q[i]);
else printf("%d\n",Q[i]);
}
}
return 0;
}

Codeforces 1404 D. Game of Pairs的更多相关文章

  1. Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...

  2. Codeforces Round #562 (Div. 2) B. Pairs

    链接:https://codeforces.com/contest/1169/problem/B 题意: Toad Ivan has mm pairs of integers, each intege ...

  3. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  4. codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. codeforces#572Div2 E---Count Pairs【数学】【同余】

    题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...

  6. CodeForces - 1189E Count Pairs(平方差)

    Count Pairs You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Fi ...

  7. Codeforces 1169B Pairs

    题目链接:http://codeforces.com/contest/1169/problem/B 题意:给你 m 对数 ,问你能不能在 1 − n 之间找到俩个不相等的 x 和 y 使得 对于前面每 ...

  8. CodeForces - 1189 E.Count Pairs (数学)

    You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Find the numbe ...

  9. Codeforces 1188B - Count Pairs(思维题)

    Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...

随机推荐

  1. P4715 【深基16.例1】淘汰赛

    P4715 [深基16.例1]淘汰赛 题目描述 有 2^n(n≤7) 个国家参加世界杯决赛圈且进入淘汰赛环节.我经知道各个国家的能力值,且都不相等.能力值高的国家和能力值低的国家踢比赛时高者获胜.1 ...

  2. python 给IDLE添加行号

    [LineNumbers] enable=1 enable_editor=1 enable_shell=1 visible=1 [LineNumbers_cfgBindings] linenumber ...

  3. CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 树上启发式合并(DSU ON TREE)

    题目描述 一棵根为\(1\) 的树,每条边上有一个字符(\(a-v\)共\(22\)种). 一条简单路径被称为\(Dokhtar-kosh\)当且仅当路径上的字符经过重新排序后可以变成一个回文串. 求 ...

  4. WSL2 bug

    错误现象 Stdout: Stderr: 2020/05/27 20:01:37 resolving /mnt/host/c/Program Files/Docker/Docker/resources ...

  5. ansible-命令使用说明

    1. ansible命令的使用说明 ansible 主机或组-m 模块名-a '模块参数' ansible参数 表示调用什么模块,使用模块的那些参数 • 主机和组,是在/etc/ansible/hos ...

  6. js 基础概念

    一 执行上下文 和 执行上下文栈 执行上下文:一段javascript代码执行前的准备工作 问题一:js引擎遇到怎样一段代码才会做"准备工作呢"? 可执行代码类型:全局代码.函数代 ...

  7. mysql CHAR and VARCHAR 比较

    写在前面 面试的时候突然有一位面试官问,说说CHAR和VARCHAR的区别,懵逼了,想自己平常使用的时候直接把VARCHAR拿来就用,真没注意到其中的不同. 反思,为什么没有注意到他们的不同 对于my ...

  8. npoi 设置单元格格式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. spring boot:使用mybatis访问多个mysql数据源/查看Hikari连接池的统计信息(spring boot 2.3.1)

    一,为什么要访问多个mysql数据源? 实际的生产环境中,我们的数据并不会总放在一个数据库, 例如:业务数据库:存放了用户/商品/订单 统计数据库:按年.月.日的针对用户.商品.订单的统计表 因为统计 ...

  10. centos8平台使用rz/sz上传下载文件

    一,rz/sz的用途 1,rz,sz是Linux/Unix服务器同客户端通过ZModem进行文件传输的命令行工具 说明:客户端需要使用支持ZModem的telnet/ssh客户端(比如windows平 ...