QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#343425 | #8058. Binary vs Ternary | ucup-team045# | TL | 0ms | 3820kb | C++20 | 2.2kb | 2024-03-02 15:49:20 | 2024-03-02 15:49:22 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<cassert>
#include<algorithm>
using namespace std;
using LL = long long;
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while(T--){
string s, t;
cin >> s >> t;
if (s == t){
cout << 0 << '\n';
continue;
}
if (t == "1" || s == "1"){
cout << -1 << '\n';
continue;
}
vector<pair<int, int> > ans;
auto op = [&](int l, int r){
ans.push_back({l, r});
int val = 0;
for(int i = l; i <= r; i++){
val = val * 3 + (s[i] - '0');
}
string str;
while(val){
str.push_back('0' + val % 2);
val /= 2;
}
reverse(str.begin(), str.end());
if (str == "") str = "0";
s.erase(s.begin() + l, s.begin() + r + 1);
s.insert(s.begin() + l, str.begin(), str.end());
};
while(s != string(s.size(), '1')){
int l = 0;
while(l < s.size() && s[l] == '1') l++;
if (l == s.size()) break;
int r = l;
while(r + 1 < s.size() && s[r + 1] == '0') r++;
if (r == s.size() - 1){
op(l, r);
op(s.size() - 2, s.size() - 1);
}
else{
op(l, r + 1);
}
}
while(s.size() < t.size()){
op(0, 1);
op(0, 1);
op(1, 2);
}
while(s.size() > t.size()){
op(0, 1);
op(2, 4);
}
for(int i = 0; i < s.size(); i++){
if (t[i] == '0'){
op(i - 1, i);
op(i, i + 1);
}
}
assert(s == t);
cout << ans.size() << '\n';
for(auto [l, r] : ans){
cout << l + 1 << ' ' << r + 1 << '\n';
}
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
3 1 111 110110 1101010 1111 111111
output:
-1 15 3 4 5 5 4 5 1 2 1 2 2 3 1 2 1 2 2 3 2 3 3 4 4 5 5 6 6 7 7 8 6 1 2 1 2 2 3 1 2 1 2 2 3
result:
ok Haitang Suki (3 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
1000 11100 111 1 11110 10001 10 1011 1111 10 1110 1100 11 11010 11 110 11 1 10001 10110 10 10 11111 10000 1001 10 1 11 10111 11 10 1 100 11 10100 1 10 101 11 1100 110 11 1110 1 1001 1 11111 10 10010 10 11001 110 1010 10011 1110 10100 1001 1001 101 100 1 1001 11 101 11 101 1001 1 1 1011 1 10 10 1011 ...