Codeforces 812E Sagheer and Apple Tree

本文探讨了一个基于树型结构的游戏策略问题,利用Nim游戏的原理,分析了甲乙两玩家如何通过移动节点上的苹果数来决定胜负。文章详细解释了Nim游戏的规则与胜利策略,并将其应用到特定的树型结构游戏中,通过计算节点到叶子节点的路径奇偶性和苹果数的异或值,推导出后手玩家的必胜策略。

大致题意:

给你一颗树,这个树有下列特征:每个节点上有若干个苹果,且从根节点到任意叶子节点的路径长度奇偶性相同。

甲和乙玩(闲)游(得)戏(慌)。

游戏过程中,甲乙轮流将任意一个节点的若干个苹果移向它的一个叶子节点,若没有叶子节点,那么这些苹果就消失了(被吃掉了)。

若一个玩家没法操作,那么算他输。

游戏由甲开始,而乙可以先选择将两个节点上的苹果数交换一下。问乙有多少种交换方式,使得最后乙获胜。

Nim游戏:

如果懂Nim游戏的话,就不要看了\(^o^)/~你肯定会这道题了。

懂Nim游戏的定义,不懂证明的孩子们,跳过6行往下看。

这6行是写给萌新的

让懒惰的我摘录一段百度百科的话

让我们来看一下美妙绝伦的结论:

简单来说,只要把每一堆的石子数全部xor起来,最后得到0,则后手有必胜策略,否则先手有必胜策略。

Nim游戏结论证明:

不想看证明,想看本题做法的,跳过13行往下看。

让我来描述一下,假如xor起来是0的话,后手的必胜策略。

我们先设每一堆石头数全部xor起来,得到的结果是p。

一开始p=0。

假如先手选中一堆石头,这一堆石头数为x,他拿走了一些石头,剩余石头数为y。

那么我们记 t=x^y。

容易知道,t≠0,先手操作之后,p=t≠0。

于是,容易知道,后手一定能找到一堆石头(石头数为a),满足a^t<a,

因为一定能找到一个a,它与t的二进制最高位都为1。(这点都理解不了就放弃OI吧……好吧开个玩笑)

那么,后手将a变为a^t之后,p重新变为0。

于是,先手操作之后,p永远不为0,后手操作之后,p永远都是0。而游戏并不是永无止境的,所以一定是后手赢。

那么,假设一开始p≠0,先手策略是什么呢?

显而易见,就是找一堆石头(石头数为a),满足a^p<a,

先手将a变成a^p,使得p变为0。接着跟后手策略一样做即可。

本题做法:

也许当该游戏的玩家Sagheer and Soliman知道互相都使用必胜策略的话,他们也就不会喜欢玩这个游戏了。

因为,一开始游戏的局面决定了,谁能赢

let me see——

题目中有一个非常好的条件

于是本题就从非常难变成了非常简单。

设,d[x]表示x到该子树中某一个叶子节点的路径长度的奇偶性。(0表示偶数,1表示奇数)

假如后手一开始不能交换节点,那么后手策略可以是这样:

假设d值为1的点属于集合1,d值为0的点属于集合0。

①:先手将集合1中的点x的y个苹果往下移到了z,

那么我们知道d[z]=0,则后手可以将z的y个苹果往下移

②假如把集合0中的点列出来,那么这就是一个典型的Nim游戏。

只要这些点上的苹数xor起来的值p=0,后手就能赢。

回到原题:后手一开始有机会交换两个节点。

假设p=0,后手可以交换集合0中的任意两个点,或集合1中的任意两个点。

否则,肯定要将集合0中的一个点与集合1中的一个点交换了。

弄一个桶,枚举ok。

代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 #define ref(i,x,y)for(int i=x;i<=y;++i)
 5 int read(){
 6     char c=getchar();int d=0,f=1;
 7     for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
 8     for(;c>='0'&&c<='9';d=d*10+c-48,c=getchar());
 9     return d*f;
10 }
11 const int N=100001,M=16777216;
12 int n,cnt,t[M],d[N],a[N],head[N],to[N],nxt[N];
13 void addedge(int x,int y){
14     ++cnt;to[cnt]=y;nxt[cnt]=head[x];head[x]=cnt;
15 }
16 void dfs(int x){
17     for(int i=head[x];i;i=nxt[i]){
18         dfs(to[i]);
19         d[x]=d[to[i]]^1;
20     }
21 }
22 int main()
23 {
24     n=read();
25     ref(i,1,n)a[i]=read();
26     ref(i,2,n)addedge(read(),i);
27     dfs(1);
28     int tmp=0,tot=0;
29     long long ans=0;
30     ref(i,1,n)if(!d[i])tmp^=a[i];
31     ref(i,1,n)if(d[i])t[a[i]]++,tot++;
32     if(tmp==0)ans=1LL*tot*(tot-1)/2+1LL*(n-tot)*(n-tot-1)/2;
33     ref(i,1,n)if(!d[i])ans+=t[tmp^a[i]];
34     printf("%lld\n",ans);
35 }

 

转载于:https://www.cnblogs.com/Blog-of-Eden/p/6934949.html

代码下载地址: https://pan.quark.cn/s/bc087ffa872a "测控电路课后习题详解"文件.pdf是一份极具价值的学术资料,其中系统地阐述了测控电路的基础理论、系统构造、核心特性及其实际应用领域。 以下是对该文献的深入解读和系统梳理:1.1测控电路在测控系统中的核心功能测控电路在测控系统的整体架构中扮演着不可或缺的角色。 它承担着对传感器输出信号进行放大、滤除杂音、提取有效信息等关键任务,并且依据测量与控制的需求,执行必要的计算、处理与变换操作,最终输出能够驱动执行机构运作的指令信号。 测控电路作为测控系统中最具可塑性的部分,具备易于放大信号、转换模式、传输数据以及适应多样化应用场景的优势。 1.2决定测控电路精确度的关键要素影响测控电路精确度的核心要素包括:(1)噪声与干扰的存在;(2)失调现象与漂移效应,尤其是温度引起的漂移;(3)线性表现与保真度水平;(4)输入输出阻抗的特性影响。 在这些要素中,噪声干扰与失调漂移(含温度效应)是最为关键的因素,需要给予高度关注。 1.3测控电路的适应性表现测控电路在测控系统中展现出高度的适应性,具体表现在:* 具备选择特定信号、灵活实施各类转换以及进行信号处理与运算的能力* 实现模数转换与数模转换功能* 在直流与交流、电压与电流信号之间进行灵活转换* 在幅值、相位、频率与脉宽信号等不同参数间进行转换* 实现量程调整功能* 对信号实施多样化的处理与运算,如计算平均值、差值、峰值、绝对值,进行求导数、积分运算等,以及实现非线性环节的线性化处理、逻辑判断等操作1.4测量电路输入信号类型对电路结构设计的影响测量电路的输入信号类型对其电路结构设计产生显著影响。 依据传感器的类型差异,输入信号的形态也呈现多样性。 主要可分为...
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值