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,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
随机推荐
- C\C++中strcat()函数
转载:https://blog.csdn.net/smf0504/article/details/52055971 C\C++中strcat()函数 ...
- matlab进行FIR滤波器设计(一)
来源:https://blog.csdn.net/leokingszx/article/details/80041910 在实际的应用中,有时需要使用FIR根据完成一些特定功能,比如近似一阶RC低通电 ...
- lens distortion
来源:http://michel.thoby.free.fr/Fisheye_history_short/International_Standards_about_Distortion.html H ...
- 《C++ primer plus》第3章练习题
注:有的题设条件自己改动了一下,比如英寸英尺改成米和厘米,部分题目自己加了点额外要求. 1.要求用户输入身高(m),用下划线提示用户输入,将身高转化成"米"加"厘米&qu ...
- 对ACE和ATL积分
下载source code - 39.66 KB 介绍 这篇文章展示了一种结合ACE和ATL的方法.它不打算作为功能演示,而是作为一个小型的"入门"解决方案,展示实现此目标的可行方 ...
- 用vscode写python,from引用本地文件的时候老是有红色波浪线,很不爽
前言 出于一些原因,国际关系等等,最近想把开发工具切换到一些免费开源的工具上面,先尝试了在vscode上搭建python环境,总体还是很简单的, 网上教程很多,vscode本身的插件也很丰富,可惜了国 ...
- php正则偷电影
1.是将电影网站弄到自己的phpstudy下面,然后进行获取电影的一些数据,然后将其存到数据库,不要获取别人网站的数据,不然会导致网站的崩溃.
- linxu 命令
top | grep java 统计 java 进程使用的资源比率 nohub java -jar test.war & 后台运行 test.war 程序,标准输出到 test.war 程序目 ...
- h2database在springboot中的使用
h2为轻量级数据库,使用特别方便,它可以不使用数据库服务器,直接嵌入到java程序中.可以配置持久化,同样也可以不持久化(数据在内存中)进程结束后,数据就释放,用做测试和演示特别方便.自带后台管理,非 ...
- 多测师讲解自动化--rf关键字--断言(下)_高级讲师肖sir
断言: 1.1Page Should Contain Maximize Browser Window sleep 2 Comment Page Should Contain hao123 #断言当前页 ...