QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#305616#6522. Digit ModeckisekiAC ✓335ms3816kbC++234.3kb2024-01-15 18:27:452024-01-15 18:27:46

Judging History

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

  • [2024-01-15 18:27:46]
  • 评测
  • 测评结果:AC
  • 用时:335ms
  • 内存:3816kb
  • [2024-01-15 18:27:45]
  • 提交

answer

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

#define all(x) begin(x), end(x)
#ifdef local
#include <experimental/iterator>
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(const char *s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
void orange_(const char *s, auto L, auto R) {
  cerr << "\e[1;33m[ " << s << " ] = [ ";
  using namespace experimental;
  copy(L, R, make_ostream_joiner(cerr, ", "));
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

const int mod = 1000000007;
int add(int x, int y) {
  return x + y >= mod ? x + y - mod : x + y;
}
int mul(int64_t a, int64_t b) {
  return static_cast<int>(a * b % mod);
}

const int maxn = 100;
int fac[maxn], ifac[maxn], inv[maxn];
void init() {
  inv[1] = 1;
  for (int i = 2; i < maxn; i++)
    inv[i] = mul(inv[mod % i], mod - mod / i);
  fac[0] = ifac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = mul(fac[i - 1], i), ifac[i] = mul(ifac[i - 1], inv[i]);
}
int C(int n, int k) {
  if (k < 0 || n < k) return 0;
  return mul(fac[n], mul(ifac[k], ifac[n - k]));
}

/*
map<tuple<array<int, 9>, int, int>, int> mp;
int DP(array<int, 9> cnt, int rest, int mx) {
  if (cnt[8] > mx)
    return 0;
  if (rest == 0)
    return 1;
  if (auto it = mp.find({cnt, rest, mx}); it != mp.end())
    return it->second;
  int res = 0;
  for (int l = 0, r = 0; l < 9; l = r) {
    for (r = l; r < 9; r++)
      if (cnt[l] != cnt[r])
        break;
    auto c = cnt;
    c[r - 1] += 1;
    int pos = r - 1;
    while (pos + 1 < 9 && c[pos] > c[pos + 1]) swap(c[pos], c[pos + 1]);
    res = add(res, mul(r - l, DP(c, rest - 1, mx)));
  }
  return mp[{cnt, rest, mx}] = res;
}
*/

int query(array<int, 10> cnt, int rest) {
  auto vmul = [&](const vector<int> &a, const vector<int> &b) -> vector<int> {
    if (a.empty() && b.empty()) return {};
    vector<int> c(min(int(a.size() + b.size() - 1), rest + 1));
    for (size_t i = 0; i < a.size(); i++)
      for (size_t j = 0; j < b.size() && i + j < c.size(); j++)
        c[i + j] = add(c[i + j], mul(a[i], b[j]));
    return c;
  };
  int res = 0;

  const int L = ranges::max(cnt);
  const int R = rest + ranges::max(cnt);

  for (int mx = L; mx <= R; mx++) {
    vector<int> pre[11], suf[11];
    pre[0].push_back(1);
    for (int i = 0; i < 10; i++) {
      vector<int> f(mx - cnt[i] + 1);
      for (size_t j = 0; j < f.size(); j++)
        f[j] = ifac[j];
      pre[i + 1] = vmul(pre[i], f);
    }
    suf[10].push_back(1);
    for (int i = 9; i >= 0; i--) {
      vector<int> f(mx - cnt[i]);
      for (size_t j = 0; j < f.size(); j++)
        f[j] = ifac[j];
      suf[i] = vmul(suf[i + 1], f);
    }
    for (int d = 0; d < 10; d++) {
      int coef = mul(d, C(rest, mx - cnt[d]));
      if (!coef) continue;
      int sum = 0;
      const auto &A = pre[d];
      const auto &B = suf[d + 1];
      int deg = rest - (mx - cnt[d]);
      coef = mul(coef, fac[deg]);
      for (int i = 0; i < int(A.size()) && i <= deg; i++) {
        if (deg - i < int(B.size()))
          sum = add(sum, mul(A[i], B[deg - i]));
      }
      res = add(res, mul(sum, coef));
    }
  }
  return res;
}

void solve() {
  string s;
  cin >> s;

  {
    ranges::reverse(s);
    int carry = 1;
    for (size_t i = 0; i < s.size() && carry; i++) {
      int z = (s[i] - '0') + carry;
      s[i] = char(z % 10 + '0');
      carry = z / 10;
    }
    if (carry)
      s += char(carry + '0');
    ranges::reverse(s);
  }

  int ans = 0;
  array<int, 10> cnt = {};
  for (size_t i = 0; i < s.size(); i++) {
    int d = s[i] - '0';
    for (int j = (i==0 ? 1 : 0); j < d; j++) {
      auto c = cnt;
      c[j] += 1;
      ans = add(ans, query(c, s.size() - i - 1));
    }
    cnt[d] += 1;
  }
  for (size_t i = 1; i < s.size(); i++) {
    for (int j = 1; j < 10; j++) {
      array<int, 10> c = {};
      c[j] += 1;
      ans = add(ans, query(c, i - 1));
    }
  }
  cout << ans << '\n';
}

int main() {
  init();
  cin.tie(nullptr)->sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--)
    solve();

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3728kb

input:

5
9
99
999
99999
999999

output:

45
615
6570
597600
5689830

result:

ok 5 number(s): "45 615 6570 597600 5689830"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3596kb

input:

34
7
48
8
76
1
97
7
5
7
7
2
89
9
4
84
46
6
73
86
78
5
3
8
9
31
24
78
7
11
45
2
65
88
6

output:

28
236
36
420
1
597
28
15
28
28
3
525
45
10
484
221
21
399
500
435
15
6
36
45
145
104
435
28
47
215
3
341
516
21

result:

ok 34 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3724kb

input:

16
935
888
429
370
499
881
285
162
178
948
205
858
573
249
773
615

output:

6009
5618
2456
2078
2905
5562
1603
887
993
6121
1174
5378
3333
1374
4724
3631

result:

ok 16 numbers

Test #4:

score: 0
Accepted
time: 2ms
memory: 3536kb

input:

12
1242
9985
6469
9310
4191
9497
3166
3495
9711
9698
4137
2257

output:

7292
63531
37910
58047
23987
59479
18076
19675
61184
61086
23672
12913

result:

ok 12 numbers

Test #5:

score: 0
Accepted
time: 2ms
memory: 3728kb

input:

10
61195
72739
10164
79164
57851
12326
29132
55992
67377
13873

output:

337575
408170
63792
450686
316513
70493
157773
305011
374163
77954

result:

ok 10 numbers

Test #6:

score: 0
Accepted
time: 2ms
memory: 3760kb

input:

8
529983
127270
421121
291729
461233
695056
365028
271160

output:

2744573
687141
2160067
1500426
2359204
3705475
1851172
1381981

result:

ok 8 numbers

Test #7:

score: 0
Accepted
time: 3ms
memory: 3760kb

input:

7
7934351
8474057
1287369
5845624
7796773
5805755
7349121

output:

42465725
45668947
6716401
30094426
41554096
29861098
38756757

result:

ok 7 numbers

Test #8:

score: 0
Accepted
time: 13ms
memory: 3604kb

input:

3
5014252832385738
8762796162648653
919997886706385

output:

892033338
297722019
462512414

result:

ok 3 number(s): "892033338 297722019 462512414"

Test #9:

score: 0
Accepted
time: 36ms
memory: 3776kb

input:

2
775701797726112292362823101
75927988177061355614

output:

371275551
566830847

result:

ok 2 number(s): "371275551 566830847"

Test #10:

score: 0
Accepted
time: 237ms
memory: 3532kb

input:

1
65760982925996012426370962570581226245366145016666

output:

661063035

result:

ok 1 number(s): "661063035"

Test #11:

score: 0
Accepted
time: 242ms
memory: 3620kb

input:

1
62597468169905757754175023836706426691470692832490

output:

9983261

result:

ok 1 number(s): "9983261"

Test #12:

score: 0
Accepted
time: 243ms
memory: 3540kb

input:

1
78912847369504885593964702297317051208901751786824

output:

817123221

result:

ok 1 number(s): "817123221"

Test #13:

score: 0
Accepted
time: 159ms
memory: 3816kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"

Test #14:

score: 0
Accepted
time: 108ms
memory: 3488kb

input:

1
999999999999999999999999999999999999999999999

output:

439421821

result:

ok 1 number(s): "439421821"

Test #15:

score: 0
Accepted
time: 70ms
memory: 3596kb

input:

1
9999999999999999999999999999999999999999

output:

387537647

result:

ok 1 number(s): "387537647"

Test #16:

score: 0
Accepted
time: 335ms
memory: 3600kb

input:

1
99999999999999999999999998889999898988888889998888

output:

909431898

result:

ok 1 number(s): "909431898"

Test #17:

score: 0
Accepted
time: 332ms
memory: 3548kb

input:

1
99999999999999999999999998989899988889989889999888

output:

289727470

result:

ok 1 number(s): "289727470"

Test #18:

score: 0
Accepted
time: 330ms
memory: 3536kb

input:

1
99999999999999999999999998998988898888898889898999

output:

962896416

result:

ok 1 number(s): "962896416"

Test #19:

score: 0
Accepted
time: 143ms
memory: 3540kb

input:

1
9999999999999999999989988898888989888899

output:

995903330

result:

ok 1 number(s): "995903330"

Test #20:

score: 0
Accepted
time: 143ms
memory: 3744kb

input:

1
9999999999999999999989999889889998998898

output:

385460258

result:

ok 1 number(s): "385460258"

Test #21:

score: 0
Accepted
time: 159ms
memory: 3744kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"