Codeforces 1404 D. Game of Pairs
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的更多相关文章
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
- Codeforces Round #562 (Div. 2) B. Pairs
链接:https://codeforces.com/contest/1169/problem/B 题意: Toad Ivan has mm pairs of integers, each intege ...
- Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- codeforces#572Div2 E---Count Pairs【数学】【同余】
题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...
- 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 ...
- Codeforces 1169B Pairs
题目链接:http://codeforces.com/contest/1169/problem/B 题意:给你 m 对数 ,问你能不能在 1 − n 之间找到俩个不相等的 x 和 y 使得 对于前面每 ...
- 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 ...
- Codeforces 1188B - Count Pairs(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
随机推荐
- Linux系统编程 —时序竞态
时序竞态 什么是时序竞态?将同一个程序执行两次,正常情况下,前后两次执行得到的结果应该是一样的.但由于系统资源竞争的原因,前后两次执行的结果有可能得到不一样的结果,这个现象就是时序竞态. pause函 ...
- 【小白学PyTorch】20 TF2的eager模式与求导
[新闻]:机器学习炼丹术的粉丝的人工智能交流群已经建立,目前有目标检测.医学图像.时间序列等多个目标为技术学习的分群和水群唠嗑的总群,欢迎大家加炼丹兄为好友,加入炼丹协会.微信:cyx64501661 ...
- WesternCTF2018_shrine
这个想了半天没啥思路,直接查别人的wp,贴地址:https://blog.csdn.net/qq_42812036/article/details/104324923 0x00 开始的页面猛一看乱七八 ...
- gitlab-centos的安装
一:gitlab-CentOS的安装 1. 环境准备 1 [root@1-231 ~]# cat /etc/redhat-release 2 CentOS Linux release 7.4.170 ...
- python之线程池和进程池
线程池和进程池 一.池的概念 池是用来保证计算机硬件安全的情况下最大限度的利用计算机 它降低了程序的运行效率但是保证了计算机硬件的安全从而让你写的程序能够正常运行 ''' 无论是开设进程也好还是开设线 ...
- rs232转以太网转换器
rs232转以太网转换器 rs232转网络ZLAN5103可以实现RS232/485/422和TCP/IP之间进行透明数据转发.方便地使得串口设备连接到以太网和Internet,实现串口设备的网络化升 ...
- java 图片相似度算法
利用直方图原理实现图像内容相似度比较 ,作为笔记记录在随笔中. public class PhotoDigest { public static void main(String[] ...
- Mysql架构与内部模块-第一章
Mysql作为大多数中小型企业的首选数据库,也可能是众多同僚接触的第一个数据库,其热门程度不言而喻,一些相对基础的知识本系列不做赘述,主要简述Mysql相关的进阶知识. 本章将由浅入深的讲解从连接My ...
- 扫描仪扫描文件处理-imagemagick常用参数
-resize 宽x高(缩放,不变形) -extent 宽x高(放大,不变形)之前设置:-gravity center(重心居中) -brightness-contrast 亮度x对比度(设置亮度对比 ...
- swoft根据表创建实体
php bin/swoft entity:gen table= table1,table2,table3,... [root@localhost swoft]# php bin/swoft entit ...