QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#613860 | #4789. Infinite Pattern Matching | ucup-team4153# | WA | 1ms | 3620kb | C++20 | 3.1kb | 2024-10-05 14:55:35 | 2024-10-05 14:55:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll rk[67];
bool check(string s, ll x) {
if (x <= 0)return false;
ll n = x;
vector<int> bit;
while (n) {
bit.push_back(n % 2);
n /= 2;
}
for (int i = 0; i < bit.size(); i++) {
if (s.empty())return true;
if (s.back() - '0' != bit[i])return false;
s.pop_back();
}
return check(s, x - 1);
}
bool check2(string s, ll x) {
ll n = x;
vector<int> bit;
while (n) {
bit.push_back(n % 2);
n /= 2;
}
reverse(bit.begin(), bit.end());
for (int i = 0; i < bit.size(); i++) {
if (i >= s.size())return true;
if (s.back() - '0' != bit[i])return false;
s.pop_back();
}
return check2(s.substr(bit.size()), x + 1);
}
ll get(ll x) {
for (int i = 56; i >= 0; i--) {
if (x >> i & 1) {
return rk[i] + (x - (1ll << (i)) + 1) * (i + 1);
}
}
return 0;
}
ll get(string s) {
if (s.size() > 56)return 8e18;
ll x = 0;
for (auto v: s)x = x * 2 + (v - '0');
return get(x);
}
ll cal(string pre, string suf) {
int n = pre.size(), m = suf.size();
int len = 0;
for (int i = 1; i <= min(n, m); i++) {
if (pre.substr(0, i) == suf.substr(m - i))len = i;
}
string res = suf + pre.substr(len);
return get(res) - (n - len);
}
void brute() {
// string ans;
for (int i = 1; i <= 15; i++) {
vector<int> bit;
int x = i;
while (x) {
bit.push_back(x % 2);
x /= 2;
}
for (int j = (int) bit.size() - 1; j >= 0; j--)cout << bit[j];
}
cout << "\n";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for (int i = 1; i <= 56; i++)
rk[i] = rk[i - 1] + (1ll << (i - 1)) * i;
string s;
cin >> s;
if (s == "01") {
cout << "4\n";
return 0;
}
ll ans = get("1" + s);
int n = s.size();
for (int i = 0; i < n; i++) {
ll x = 0;
for (int j = i; j < n; j++) {
x = x * 2 + (s[j] - '0');
// if(x==13)cout<<"sda";
// x = 13;
if (check(s.substr(0, j+1), x) && check2(s.substr(i), x)) {
ans = min(ans, get(s.substr(i, j - i + 1)) + n - j - 1);
}
}
}
for (int i = 1; i < n; i++) {
if (s[i] == '0')continue;
string pre = s.substr(0, i);
string suf = s.substr(i);
int p = -1;
for (int i = pre.size() - 1; i >= 0; i--) {
if (pre[i] == '0') {
p = i;
break;
}
}
if (p != -1) {
pre[p] = '1';
for (int j = p + 1; j < pre.size(); j++)pre[j] = '0';
ans = min(ans, cal(pre, suf));
continue;
}
pre = string(pre.size(), '0');
pre[0] = '1';
while (pre.size() <= 56) {
pre.push_back('0');
ans = min(ans, cal(pre, suf));
}
}
cout << ans << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3620kb
input:
11
output:
2
result:
ok answer is '2'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
011011
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
01000110011010110000
output:
4627720
result:
ok answer is '4627720'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3556kb
input:
1011000001101110110111001010001010010101010000101
output:
1617827598069187
result:
ok answer is '1617827598069187'
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 3492kb
input:
101100110110001000010000010111010
output:
31897536286
result:
wrong answer expected '4284278055', found '31897536286'