题解:P15632 [2019 KAIST RUN Spring] A Plus Equals B
NoneOfTheAbove · · 题解
link
Solution
如果直接正向处理,会有大数处理,有可能要高精度。但是我们可以发现,操作 B+=B 等价为 A/=2 的高精度版,这样可以去掉高精度。
我们还可以发现,如果保持 A+=B,然后进行 B+=B,等价于把
因为这组操作要求
:::info[证明正确性]
如果原来
显然一组操作最劣时
| 因为 |
|---|
Code
:::success[code]
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18;
const i128 Inf=1e35;
ll a,b,cnt;
bool fl;
vector<str> vec;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>a>>b;
while(a%2==0) a/=2,vec.push_back("B+=B\n");
while(b%2==0) b/=2,vec.push_back("A+=A\n");
if(a<b) swap(a,b),fl=1;
while(a-b>0)
{
a+=b;
vec.push_back((fl?"B+=A\n":"A+=B\n"));
while(a%2==0)
{
a/=2;
vec.push_back((fl?"A+=A\n":"B+=B\n"));
}
if(a<b) swap(a,b),fl=!fl;
}
cout<<vec.size()<<"\n";
for(str s:vec) cout<<s;
}
:::