CFR-838-Div-2解题报告
A. Divide and Conquer
题意:给你一个数组,每次操作可以将一个数变为它除以二下取整,求将数组的和变为偶数的最小次数。
显然如果数组本来就是偶数,则为
By jiangly
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n);
int par = 0;
int mn = 1E9;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
par ^= a[i] & 1;
int x = a[i], t = 0;
while (x % 2 == a[i] % 2) {
x /= 2;
t++;
}
mn = std::min(mn, t);
}
std::cout << (par ? mn : 0) << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
B. Make Array Good
题意:有一个数组,每次可以将一个数
改为 内的任意整数,求在 次以内将这个数组改为任意两个数均有倍数关系的方案。
由于在
By jiangly
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n);
std::cout << n << "\n";
for (int i = 0; i < n; i++) {
std::cin >> a[i];
int p = 1 << (std::__lg(a[i]) + 1);
std::cout << i + 1 << " " << p - a[i] << "\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
C. Binary Strings are Fun
给你一个
串,对于每个前缀,计算生成合法串的方案数,输出它们的和(对 取模)。生成一个合法串,需要在每相邻两位之间插入一个新位,使得任意一个奇数前缀的中位数为末尾的数字。
首先举例子找一找规律。假设待生成的串为 0_0_1_1_0_1_1_1
,下划线是需要填的。我们依此考虑:只考虑前三位,中位数为
依此类推,我们发现,如果某一个“限制位置”和下一个“限制位置”不同,那么中位数在这两位后发生改变,所以这两位之间的空位必须填后面的值,且前面这两位的数量差只能为
所以,只有一个位置后面没有不同位置时,才可以真正的任意填,否则只有一种方案。此时答案就很显然了,即为
By jiangly
void solve() {
int n;
std::cin >> n;
std::string s;
std::cin >> s;
int last = 0;
Z ans = 0, sum;
for (auto c : s) {
if (last != c) ans = 1;
else ans *= 2;
last = c;
sum += ans;
}
std::cout << sum << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
D. GCD Queries
题意:交互题。有一个
的排列,每次可以询问两位的 ,在 次操作内给出两个位置,使得它们中的一个位置为 。
首先有一个非常重要的性质:
初始时
By cxm1024
#include<bits/stdc++.h>
using namespace std;
signed main() {
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
int now=2,x=1,y=2,xx,yy;
while(now<n) {
now++;
cout<<"? "<<x<<" "<<now<<endl;
cin>>xx;
cout<<"? "<<y<<" "<<now<<endl;
cin>>yy;
if(xx>yy) y=now;
else if(yy>xx) x=now;
}
cout<<"! "<<x<<" "<<y<<endl;
cin>>n;
assert(n==1);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步