正文
增加 51 行删除 4 行
15 行未改动161718192021222324252615 行未改动2 9 4B1A3 1 2A……```令 $solve(o,a)$ 为原有 $o$ 个橘子, $a$ 个苹果。则观察可得 $solve(1,n)=(n-1)B$$solve(n,1)=(n-1)A$$当a>o时,solve(o,a)=(a/o)Bsolve(o,a%o)$15 行未改动1617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727315 行未改动2 9 4B1A3 1 2A……```令 $\mathrm{solve}(o,a)$ 为原有 $o$ 个橘子, $a$ 个苹果的方案。则观察可得 $\mathrm{solve}(1,n)=(n-1)B$。$\mathrm{solve}(n,1)=(n-1)A$。当 $a>o$ 时,$\mathrm{solve}(o,a)= ( \lfloor \frac b a \rfloor B)(\mathrm{solve}(o,a \bmod o)A)$。否则答案为 $\mathrm{inv}(\mathrm{solve}(a, o))$, $\mathrm{inv}(s)$ 定义为将 $s$ 串中的 $A,B$ 互换。观察可知其过程类似欧几里得算法,故时间复杂度为 $O(\log \max(o,a))$。代码如下:```cpp#include <bits/stdc++.h>using namespace std;using ll = long long;ll o, a;inline ll gcd(ll a, ll b) {if (!b)return a;return gcd(b, a % b);}inline string ct(string x) {int sz = x.size();for (int i = 0; i < sz; i++) if (!isdigit(x[i]))x[i] = x[i] == 'A' ? 'B' : 'A';return x;}inline string solve(ll x, ll y) {if (x == 1)return y > 1 ? to_string(y - 1) + "B" : "";if (y == 1)return x > 1 ? to_string(x - 1) + "A" : "";int op = 1;if (x > y)swap(x, y), op = -1;string t = to_string(y / x) + "B" + solve(x, y % x);return op == -1 ? ct(t) : t;}int main() {scanf("%lld%lld", &o, &a);if (gcd(o, a) != 1 || (o == 1 && a == 1))return puts("Impossible"), 0;printf("%s\n", solve(o, a).c_str());return 0;}```