QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#768058#5460. Sum of NumberswxhtzdyWA 18ms3804kbC++201.8kb2024-11-21 00:01:452024-11-21 00:01:52

Judging History

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

  • [2024-11-21 00:01:52]
  • 评测
  • 测评结果:WA
  • 用时:18ms
  • 内存:3804kb
  • [2024-11-21 00:01:45]
  • 提交

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 = d / 10;
        ptr += 1;
      }
      reverse(res.begin(), res.end());
      return res;
    };
    int c = (n / k) * k;
    string res = "";
    for (int mask = 0; mask < (1 << k); mask++) {
      if (c + __builtin_popcount(mask) != n) {
        continue;
      }
      for (int i = 0; i < k; i++) {
        if (mask >> i & 1) {
          g[i] += 1;
        }
      }
      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 {
          res = min(res, sum);
        }
      }
      for (int i = 0; i < k; i++) {
        if (mask >> i & 1) {
          g[i] -= 1;
        }
      }
    }
    cout << res << '\n';
  }
  return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
8 1
45455151
2 1
42

output:

9696
6

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 18ms
memory: 3620kb

input:

10
1301 6
56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...

output:

120855517662654149697334055050824696470196072685564229397740445548524409959860621848657812438004738947316997563377006735917364092696598132475542312015911120188106165065117094736481700802
03504006381952030738913876810041377591598872290446466084627611323321630894284337938733515684691769731777417331159...

result:

wrong answer 1st lines differ - expected: '286183755510664079479706773787...6909797866802717925250679901255', found: '120855517662654149697334055050...0188106165065117094736481700802'