自己搬的题。
假设 y < x y < x y<x,那么 x × y x \times y x×y 可以用如下方法表示成不超过 log 2 y \log_2{y} log2y 个数:
x × y = ∑ 2 i × x x \times y=\sum 2^i \times x x×y=∑2i×x,其中 i i i 为 y y y 的二进制表示下第 i i i 位为 1 1 1 的那些数。
所以我们就可以用 2 log 2 y 2\log_2{y} 2log2y 次操作解决这个问题啦。
#include <bits/stdc++.h>
using namespace std;
char s[] = {'A', 'B'};
long long r[4];
vector<vector<char>> output;
int main() {
cin >> r[0] >> r[1];
int x = (r[0] < r[1] ? 0 : 1);
while (r[x]) {
if (r[x] % 2) {
output.push_back({s[x ^ 1], 'C', 'C'});
r[2] += r[x ^ 1];
}
output.push_back({s[x ^ 1], s[x ^ 1], s[x ^ 1]});
r[x ^ 1] *= 2;
r[x] /= 2;
}
cout << output.size() << endl;
for (auto t : output) {
cout << t[0] << " " << t[1] << " " << t[2] << endl;
}
cout << "C" << endl;
return 0;
}