题解:P15328 [GCPC 2025] Around the Table
题目大意
左右分别有 \(l,r\) 个孩子排队,左右队首配对后,分别跑到对面的队尾,求一共有多少种配对。
思路
答案可以分情况讨论。
正常情况下,重复 \(2 \times (l+r)\) 后会出现循环,但是有时候会出现重复:
- \(l=r\),其中有 \(\frac{l+r}{2}\) 次是重复出现的,所以此时输出 \(3 \times l\)。
- \(l=r+1\),此时每种配对出现且仅出现一次,输出 \(l+r\)。
- \(l=r+2\),也是有 \(\frac{l+r}{2}\) 次是重复出现的,输出 \(3 \times (r+1)\)。
- 其他情况,不会出现重复,输出 \(2 \times (l+r)\)。
#include<bits/stdc++.h>
using namespace std;
#define int long long
int l,r;
signed main(){
cin >> l >> r;
if(l == r){
cout << 3 * l << endl;
}else if(l == r + 1){
cout << l + r << endl;
}else if(l == r + 2){
cout << 3 * (r + 1) << endl;
}else cout << 2 * (l + r) << endl;
return 0;
}
如果这篇题解对您有帮助,可以点个赞吗?
对题目还有疑问的话,欢迎指出。

浙公网安备 33010602011771号