QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#577759#5460. Sum of Numbersxin11TL 0ms3752kbC++234.8kb2024-09-20 14:38:272024-09-20 14:38:28

Judging History

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

  • [2024-09-20 14:38:28]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3752kb
  • [2024-09-20 14:38:27]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

template <unsigned M_> struct ModInt {
    static constexpr unsigned M = M_;
    unsigned x;
    constexpr ModInt() : x(0) {}
    constexpr ModInt(unsigned x_) : x(x_ % M) {}
    constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
    constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
    constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
    ModInt &operator+=(const ModInt &a) {
        x = ((x += a.x) >= M) ? (x - M) : x;
        return *this;
    }
    ModInt &operator-=(const ModInt &a) {
        x = ((x -= a.x) >= M) ? (x + M) : x;
        return *this;
    }
    ModInt &operator*=(const ModInt &a) {
        x = (static_cast<unsigned long long>(x) * a.x) % M;
        return *this;
    }
    ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
    ModInt pow(long long e) const {
        if (e < 0)
            return inv().pow(-e);
        ModInt a = *this, b = 1;
        for (; e; e >>= 1) {
            if (e & 1)
                b *= a;
            a *= a;
        }
        return b;
    }
    ModInt inv() const {
        unsigned a = M, b = x;
        int y = 0, z = 1;
        for (; b;) {
            const unsigned q = a / b;
            const unsigned c = a - q * b;
            a = b;
            b = c;
            const int w = y - static_cast<int>(q) * z;
            y = z;
            z = w;
        }
        assert(a == 1);
        return ModInt(y);
    }
    ModInt operator+() const { return *this; }
    ModInt operator-() const {
        ModInt a;
        a.x = x ? (M - x) : 0;
        return a;
    }
    ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
    ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
    ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
    ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
    template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
    template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
    template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
    template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
    explicit operator bool() const { return x; }
    bool operator==(const ModInt &a) const { return (x == a.x); }
    bool operator!=(const ModInt &a) const { return (x != a.x); }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
// using mint = ModInt<1000000007U>;
using mint = ModInt<998244353U>;

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    k++;
    vector<int> ans;
    vector<int> lens(k);
    auto go = [&](auto self, int cur, int total) -> void { 
        if (total > n || (cur == k && total < n)) {
            return;
        }
        if (cur == k) {
			if (!ans.empty() && *max_element(lens.begin(), lens.end()) > ans.size())
				return;
            vector<int> sum(n / k + 5);
            int from = 0;
            for (int i = 0; i < k; i++) {
                int to = from + lens[i] - 1;
                int r = 0;
                int pos = 0;
                for (int j = to; j >= from; j--) {
                    sum[pos] += s[j] - '0' + r;
                    r = sum[pos] / 10;
                    sum[pos] %= 10;
                    pos++;
                }
                if (r)
                    sum[pos]++;
                from = to + 1;
            }
            if (ans.empty()) {
                ans = sum;
				while (ans.back() == 0)
        			ans.pop_back();
            } else {
                for (int j = int(sum.size()) - 1; j >= 0; j--) {
                    if (sum[j] < ans[j]) {
                        ans = sum;
						while (ans.back() == 0)
        					ans.pop_back();
                        break;
                    } else if (sum[j] > ans[j]) {
                        break;
                    }
                }
            }
            return;
        }
        for (int i = max(1, n / k - 2); i <= n / k + 2; i++) {
            lens[cur] = i;
            self(self, cur + 1, total + i);
        }
    };
    go(go, 0, 0);
    reverse(ans.begin(), ans.end());
    for (int d : ans)
        cout << d;
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    cin >> T;
    for (int tc = 1; tc <= T; tc++) {
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3752kb

input:

2
8 1
45455151
2 1
42

output:

9696
6

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10
1301 6
56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...

output:

2861837555106640794797067737879913860686764066159587941287350938727749577629356630565034353414526438507603808735990935008225192080065174423508575377930722196909797866802717925250679901255
1330897896655974774035586406544907434842835048336411271110427836483063457950873824562288934364096546537492367401...

result: