QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#668599#7720. Even-dominant Numbersucup-team173#WA 141ms26548kbC++203.5kb2024-10-23 15:08:362024-10-23 15:08:40

Judging History

你现在查看的是最新测评结果

  • [2024-10-23 15:08:40]
  • 评测
  • 测评结果:WA
  • 用时:141ms
  • 内存:26548kb
  • [2024-10-23 15:08:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
constexpr int N = 1e6;

void solve() {
    vector<ll> pw10(15);
    pw10[0] = 1;
    for(int i = 1; i < 15; i++) pw10[i] = pw10[i - 1] * 10;
    auto sign = [&](int dig) { return dig % 2 ? -1 : 1; };
    vector h(15, vector(31, 0ll)), p(15, vector(31, 0ll));
    h[0][15] = 1;
    for(int i = 0; i < 14; i++) {
        for(int j = 0; j <= 30; j++) {
            if(h[i][j]) {
                h[i + 1][j + 1] += h[i][j] * 5;
                h[i + 1][j - 1] += h[i][j] * 5;
            }
        }
    }
    {
        vector h(15, vector(31, array<ll, 2>{0, 0}));
        h[0][15][0] = 1;
        for(int i = 0; i < 14; i++) {
            for(int j = 0; j <= 30; j++) {
                if(h[i][j][0]) {
                    h[i + 1][j][0] += h[i][j][0];
                    h[i + 1][j + 1][1] += h[i][j][0] * 4;
                    h[i + 1][j - 1][1] += h[i][j][0] * 5;
                    p[i][j] += h[i][j][0];
                }
                if(h[i][j][1]) {
                    h[i + 1][j + 1][1] += h[i][j][0] * 5;
                    h[i + 1][j - 1][1] += h[i][j][0] * 5;
                    p[i][j] += h[i][j][1];
                }
            }
        }
    }
    for(int i = 0; i <= 14; i++) {
        for(int j = 30; j; j--) {
            h[i][j - 1] += h[i][j];
            p[i][j - 1] += p[i][j];
        }
    }
    auto g = [&](ll r, ll x) {
        ll res = 0;
        for(int i = 12, ty = 0; i >= 0; i--) {
            int dig = r / pw10[i] % 10;
            if(dig) {
                if(ty) { // has >0
                    res += ((dig + 1) / 2) * h[i][15 + x - 1];
                    res += (dig / 2) * h[i][15 + x + 1];
                } else {
                    res += ((dig - 1) / 2) * h[i][15 + x - 1];
                    res += p[i][15 + x];
                    res += (dig / 2) * h[i][15 + x + 1];
                }
                ty = 1;
            }
            if(ty) x -= sign(dig);
        }
        return res + (x < 0);
    };
    auto f = [&](ll l, ll r, ll x) {
        // cerr << l << ' ' << r << ' ' << x << '\n';
        return g(r, x) - g(l - 1, x);
    };
    vector<ll> x(N + 2), s(N + 1), t(N + 1);
    // cerr << 1. * clock() / CLOCKS_PER_SEC << '\n';
    for(ll i = 1; i <= N; i++) {
        x[i] = i * i;
        t[i] = 1;
        for(ll j = i; j; j /= 10) t[i] -= sign(j % 10);
    }
    x[N + 1] = ll(1e12) + 1;
    for(int i = 1; i <= N; i++) {
        s[i] = s[i - 1] + f(x[i], x[i + 1] - 1, t[i]);
    }
    // cerr << 1. * clock() / CLOCKS_PER_SEC << '\n';
    // for(int i = 1; i <= 10; i++) {
    //     cerr << x[i] << ' ' << s[i] << ' ' << t[i] << '\n';
    // }
    // for(int i = 1; i <= 40; i++) {
    //     int x = i, sq = sqrt(i);
    //     int cnt = 0;
    //     while(x) cnt += sign(x % 10), x /= 10;
    //     while(sq) cnt += sign(sq % 10), sq /= 10;
    //     cerr << i << ' ' << (cnt > 0) << '\n';
    // }
    int q; cin >> q;
    while(q--) {
        ll l, r;
        cin >> l >> r;
        int xl = lower_bound(x.begin(), x.end(), l) - x.begin();
        int xr = prev(upper_bound(x.begin(), x.end(), r)) - x.begin();
        ll ans = s[xr - 1] - s[xl - 1];
        if(l < x[xl]) ans += f(l, x[xl] - 1, t[xl - 1]);
        if(x[xr] <= r) ans += f(x[xr], r, t[xr + 1]);
        cout << ans << endl;
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 140ms
memory: 26396kb

input:

1
1 10

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: -100
Wrong Answer
time: 141ms
memory: 26548kb

input:

10
2 5
7 9
5 5
2 4
2 2
7 9
3 9
1 10
5 8
1 10

output:

0
1
-1
0
2
1
3
3
-1
3

result:

wrong answer 1st numbers differ - expected: '1', found: '0'