2021牛客暑期多校训练营1 A. Alice and Bob

本文介绍了一种两人博弈游戏的算法实现,通过预处理和动态规划的方法确定初始状态下哪方玩家将赢得游戏。代码详细展示了如何构建解决方案,并利用查表的方式快速判断结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:Alice and Bob like playing games. There are two piles of stones with numbers {n}n and {m}m. Alice and Bob take turns to operate, each operation can take away {k}(k>0)k(k>0) stones from one pile and take away s \times k(s \geq 0)s×k(s≥0) stones from another pile. Alice plays first. The person who cannot perform the operation loses the game.

Please determine who will win the game if both Alice and Bob play the game optimally.

大意:两人博弈,每次一个人从一堆中拿 k 个,同时从另一堆拿 k * s(s >= 0) 个,问谁先不能拿。

代码:

#include<bits/stdc++.h>
using namespace std;
bool dp[5005][5005];
int t,n,m;
void pre()//预处理打表由最初必败状态出发并标记上一步必胜的状态。
{
	for(int i=0;i<=5000;i++){
		for(int j=0;j<=5000;j++){
			if(!dp[i][j]){
				for(int k=1;i+k<=5000;k++){
					for(int l=0;j+l*k<=5000;l++){
						dp[i+k][j+l*k]=1;
					} 
				}
				for(int k=1;j+k<=5000;k++){
					for(int l=0;i+l*k<=5000;l++){
						dp[i+l*k][j+k]=1;
					} 
				}
			}
		}
	}
}
int main()
{
	pre();
	cin>>t;
	while(t--){
		cin>>n>>m;
		if(dp[n][m]){//查表
			cout<<"Alice"<<endl;
		}
		else{
			cout<<"Bob"<<endl;
		}
	}
	return 0; 
} 
### 2024 暑期训练营 Sort4 题目解析 #### 题目背景与描述 在2024年的暑期训练营中,Sort4是一道涉及字符串排序和字典序比较的题目。该题目的核心在于通过特定的操作改变给定字符串数组的顺序,并最终使得整个序列达到某种最优状态。 #### 解决方案概述 为了有效解决这个问题,需要理解并应用分治算法以及RMQ(Range Minimum Query)技术来优化查询效率[^3]。具体来说: - **初始化阶段**:读入输入数据并将所有字符串存储在一个列表中。 - **预处理部分**:构建笛卡尔树用于快速查找最小值及其位置;同时记录每个节点的信息以便后续更新操作时能够高效定位目标元素的位置。 - 对于每一次交换请求\( (d_i, p_i) \),判断当前待调整项是否满足条件 \( d_i > p_i \)。如果是,则执行相应的移动动作使新的排列更接近理想解; - 使用自定义比较器对修改后的集合重新排序,确保整体结构仍然保持有序特性不变; - 继续上述过程直至完成全部指令集中的每一条命令为止。 ```cpp #include <bits/stdc++.h> using namespace std; struct Node { int val; int idx; }; bool cmp(Node a, Node b){ return a.val < b.val || (a.val == b.val && a.idx < b.idx); } vector<Node> nodes; int n, q; void buildCartesianTree() {/* ... */ } // 执行单步转换操作 void performOperation(int di, int pi) { if(di > pi){ // 当di大于pi时才会引起字典序变化 swap(nodes[di],nodes[pi]); sort(nodes.begin(), nodes.end(),cmp); } } ``` 此段代码展示了如何基于给定条件实施一次有效的变换,并维持全局秩序不受影响。需要注意的是,在实际竞赛环境中可能还需要额外考虑边界情况以及其他潜在陷阱。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值