P9736 [COCI2022-2023#2] Ekspert 题解

自己搬的题。

假设 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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值