提交时间:2023-12-09 08:44:49

运行 ID: 24045

// 40pts #include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 5e3 + 9; int _, m; char s[N]; namespace SpeC { bool chk() { for (int i = 1; i <= m; i++) if (s[i] != '0' && s[i] != '1') return 0; return 1; } int cntx[N]; void Main() { LL ans = 0; for (int i = 1; i <= m; i++) { cntx[i] = 0; if (s[i] == '1') cntx[i]++, cntx[i - 1]--; } for (int i = 1; i <= m; i++) { ans += 1ll * i * abs(cntx[i]); } printf("%lld\n", ans); } } int ans_less_than_100[] = {0, 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11, 12, 12, 11, 10, 9, 8, 9, 10, 11, 12, 13, 14, 14, 13, 12, 11, 10, 11, 12, 13, 14, 15, 16, 16, 15, 14, 13, 12, 11, 12, 13, 14, 15, 15, 14, 13, 12, 11, 10, 9, 10, 11, 12, 13, 13, 12, 11, 10, 9, 8, 7, 8, 9, 10, 11, 11, 10, 9, 8, 7, 6}; signed main() { scanf("%d", &_); while (_--) { scanf("%s", s + 1); m = strlen(s + 1); if (m <= 2) { int n = s[1] - '0'; if (m == 2) n = n * 10 + (s[2] - '0'); printf("%d\n", ans_less_than_100[n]); continue; } for (int l = 1, r = m; l < r; l++, r--) swap(s[l], s[r]); if (SpeC::chk()) { SpeC::Main(); continue; } } fclose(stdin); fclose(stdout); return 0; }