QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#556203#8941. Even or Odd Spanning Treeucup-team3519#WA 0ms3596kbC++202.1kb2024-09-10 15:52:392024-09-10 15:52:41

Judging History

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

  • [2024-09-10 15:52:41]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3596kb
  • [2024-09-10 15:52:39]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define V vector
#define pb push_back
#define all1(x) (x).begin() + 1, (x).end()
#define all0(x) (x).begin(), (x).end()
typedef long long LL;
const LL INF = 1e10;

void solve() {
    string n; cin >> n;
    int m; cin >> m;
    string sm = to_string(m);
    string sm1 = to_string(m - 1);
    reverse(all0(sm));
    reverse(all0(sm1));
    LL res = 0;
    reverse(all0(n));
    LL ten = 1;
    for(int i = 0; i < n.size(); i++) {
        res += ten * (n[i] - '0');
        res %= m;
        ten = ten * 10 % m;
    }
    LL ans = m - res;

    // cout << "a1 : " << ans << endl;

    bool add = 0;
    LL carry = 0;
    ten = 1;

    for(int i = 0; i < n.size(); i++) {
        if(i + sm1.size() <= n.size()) {
            if(n.substr(i, sm1.size()) == sm1) {
                ans = min(ans, carry + 1);
            }
        }
        if(i + sm.size() <= n.size()) {
            if(n.substr(i, sm.size()) == sm) {
                if(add) ans = 1;
            }
        }
        if(n[i] != '9') add = 1;
        carry += ('9' - n[i]) * ten;
        carry = min(INF, carry);
        ten *= 10;
        ten = min(ten, INF);
    }
    ten = 1;

    for(int i = 0; i <= 8; i++) {
        LL aim = ten * m;
        LL now = 0;
        LL cur = 1;
        for(int j = 0; j < min(i + sm.size(), n.size()); j++) {
            now += cur * (n[j] - '0');
            cur *= 10;
        }
        if(now < aim) ans = min(ans, aim - now);
        ten *= 10;

    }
    // cout << ans << endl;

    V<int> bit(n.size());
    for(int i = 0; i < n.size(); i++) {
        bit[i] = n[i] - '0';
    }
    bit[0] += ans;
    int now = 0;
    while(bit[now] >= 10) {
        if(now == bit.size() - 1) {
            bit.pb(0);
        }
        bit[now + 1] += bit[now] / 10;
        bit[now] %= 10;
        now++;
    }
    for(int i = bit.size() - 1; i >= 0; i--) cout << bit[i];
    cout << endl;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t; cin >> t;
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3596kb

input:

3
2 1
1 2 5
3 1
1 3 1
4 4
1 2 1
1 3 1
1 4 1
2 4 2

output:

3
2
6

result:

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