QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#768095#5460. Sum of NumberswxhtzdyTL 0ms3812kbC++202.1kb2024-11-21 00:32:462024-11-21 00:32:46

Judging History

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

  • [2024-11-21 00:32:46]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3812kb
  • [2024-11-21 00:32:46]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, k;
    cin >> n >> k;
    k += 1;
    string s;
    cin >> s;
    vector<int> g;
    for (int i = 0; i < k; i++) {
      g.push_back(n / k);
    }
    auto Add = [&](string s, string t) {
      reverse(s.begin(), s.end());
      reverse(t.begin(), t.end());
      int carry = 0;
      int ptr = 0;
      string res = "";
      while (ptr < int(s.size()) || ptr < int(t.size()) || carry != 0) {
        int ds = (ptr < int(s.size()) ? int(s[ptr] - '0') : 0);
        int dt = (ptr < int(t.size()) ? int(t[ptr] - '0') : 0);
        int d = (ds + dt + carry) % 10;
        res += char('0' + d);
        carry = (ds + dt + carry) / 10;
        ptr += 1;
      }
      reverse(res.begin(), res.end());
      return res;
    };
    int c = (n / k) * k;
    string res = "";
    int pw = 1;
    for (int i = 0; i < k; i++) {
      pw *= 5;
    }
    for (int t = 0; t < pw; t++) {
      int x = t;
      vector<int> f;
      for (int i = 0; i < k; i++) {
        f.push_back(x % 5);
        x /= 5;
      }
      int len = c;
      for (int i = 0; i < k; i++) {
        len += (f[i] - 2);
      }
      if (len != n) {
        continue;
      }
      for (int i = 0; i < k; i++) {
        g[i] += (f[i] - 2);
      }
      if (*min_element(g.begin(), g.end()) > 0) {
        int ptr = 0;
        string sum = "0";
        for (int i = 0; i < k; i++) {
          string cur = "";
          for (int j = 0; j < g[i]; j++) {
            cur += s[ptr];
            ptr += 1;
          }
          sum = Add(sum, cur);
        }
        if (res.empty()) {
          res = sum;
        } else {
          if (int(res.size()) > int(sum.size())) {
            res = sum;
          } else if (int(res.size()) == int(sum.size())) {
            res = min(res, sum);
          }
        }
      } 
      for (int i = 0; i < k; i++) {
        g[i] -= (f[i] - 2);
      }
    }
    cout << res << '\n';
  }
  return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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:


result: