QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#613989 | #4789. Infinite Pattern Matching | ucup-team4153# | WA | 0ms | 3556kb | C++20 | 3.4kb | 2024-10-05 15:16:56 | 2024-10-05 15:19:18 |
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 8.2e18;
ll x = 0;
for (auto v: s)x = x * 2 + (v - '0');
return get(x);
}
ll now;
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);
if (res.size() <= 56) {
now = 0;
for (auto v: res)now = now * 2 + v - '0';
now--;
}
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;
if (s[i] == '0')continue;
for (int j = i; j < n; j++) {
x = x * 2 + (s[j] - '0');
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 = 2; 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';
ll res = cal(pre, suf);
now >>= i;
if (s[0] == '0' && !now)continue;
ans = min(ans, res);
continue;
}
pre = string(pre.size(), '0');
pre[0] = '1';
while (pre.size() <= 56) {
pre.push_back('0');
ll res = cal(pre, suf);
now >>= pre.size();
if (!now)continue;
ans = min(ans, res);
}
}
cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3556kb
input:
11
output:
5
result:
wrong answer expected '2', found '5'