QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#110694#6522. Digit Modehos_lyricAC ✓178ms4180kbC++146.3kb2023-06-03 12:41:032023-06-03 12:41:07

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-03 12:41:07]
  • 评测
  • 测评结果:AC
  • 用时:178ms
  • 内存:4180kb
  • [2023-06-03 12:41:03]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  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 = 1U; 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 == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; 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; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 1000000007;
using Mint = ModInt<MO>;

constexpr int LIM_INV = 1010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];

void prepare() {
  inv[1] = 1;
  for (int i = 2; i < LIM_INV; ++i) {
    inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
  }
  fac[0] = invFac[0] = 1;
  for (int i = 1; i < LIM_INV; ++i) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(Int n, Int k) {
  if (n < 0) {
    if (k >= 0) {
      return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
    } else {
      return 0;
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM_INV);
      return fac[n] * invFac[k] * invFac[n - k];
    } else {
      return 0;
    }
  }
}


int L;
char S[60];

int F[11];

Mint dp[11][60][60];
Mint DP[11][60][60];

Mint solve(int n) {
// cerr<<"solve "<<n<<"; ";pv(F,F+10);
  for (int a = 0; a <= 10; ++a) {
    for (int i = 0; i <= n; ++i) {
      fill(dp[a][i], dp[a][i] + (L + 1), 0);
      fill(DP[a][i], DP[a][i] + (L + 1), 0);
    }
  }
  dp[0][0][0] = 1;
  for (int a = 0; a < 10; ++a) {
    for (int i = 0; i <= n; ++i) for (int j = 0; j <= L; ++j) if (dp[a][i][j]) {
      for (int k = 0; i + k <= n; ++k) {
        const Mint t = dp[a][i][j] * invFac[k];
        const int jj = F[a] + k;
        dp[a + 1][i + k][max(j, jj)] += t;
        if (j <= jj) {
          DP[a + 1][i + k][jj] += t * a;
        }
      }
    }
    for (int i = 0; i <= n; ++i) for (int j = 0; j <= L; ++j) if (DP[a][i][j]) {
      for (int k = 0; i + k <= n && F[a] + k < j; ++k) {
        DP[a + 1][i + k][j] += DP[a][i][j] * invFac[k];
      }
    }
  }
  Mint ret = 0;
  for (int j = 0; j <= L; ++j) {
// cerr<<"  j = "<<j<<": "<<DP[10][n][j]*fac[n]<<endl;
    ret += DP[10][n][j];
  }
  ret *= fac[n];
// cerr<<"  ret = "<<ret<<endl;
  return ret;
}

int main() {
  prepare();
  
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%s", S);
    L = strlen(S);
    
    memset(F, 0, sizeof(F));
    Mint ans = 0;
    for (int l = 1; l < L; ++l) {
      for (int a = 1; a < 10; ++a) {
        ++F[a];
        ans += solve(l - 1);
        --F[a];
      }
    }
    for (int i = 0; i < L; ++i) {
      const int a = S[i] - '0';
      for (int b = 0; b < a; ++b) {
        if (i == 0 && b == 0) continue;
        ++F[b];
        ans += solve(L - 1 - i);
        --F[b];
      }
      ++F[a];
    }
    ans += solve(0);
    printf("%u\n", ans.x);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3680kb

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: 0ms
memory: 3928kb

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: 2ms
memory: 3712kb

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: 1ms
memory: 3736kb

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: 3748kb

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: 1ms
memory: 3716kb

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: 1ms
memory: 3764kb

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: 8ms
memory: 3732kb

input:

3
5014252832385738
8762796162648653
919997886706385

output:

892033338
297722019
462512414

result:

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

Test #9:

score: 0
Accepted
time: 21ms
memory: 3916kb

input:

2
775701797726112292362823101
75927988177061355614

output:

371275551
566830847

result:

ok 2 number(s): "371275551 566830847"

Test #10:

score: 0
Accepted
time: 132ms
memory: 3932kb

input:

1
65760982925996012426370962570581226245366145016666

output:

661063035

result:

ok 1 number(s): "661063035"

Test #11:

score: 0
Accepted
time: 135ms
memory: 3996kb

input:

1
62597468169905757754175023836706426691470692832490

output:

9983261

result:

ok 1 number(s): "9983261"

Test #12:

score: 0
Accepted
time: 132ms
memory: 4000kb

input:

1
78912847369504885593964702297317051208901751786824

output:

817123221

result:

ok 1 number(s): "817123221"

Test #13:

score: 0
Accepted
time: 167ms
memory: 3916kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"

Test #14:

score: 0
Accepted
time: 114ms
memory: 3932kb

input:

1
999999999999999999999999999999999999999999999

output:

439421821

result:

ok 1 number(s): "439421821"

Test #15:

score: 0
Accepted
time: 76ms
memory: 4088kb

input:

1
9999999999999999999999999999999999999999

output:

387537647

result:

ok 1 number(s): "387537647"

Test #16:

score: 0
Accepted
time: 169ms
memory: 4180kb

input:

1
99999999999999999999999998889999898988888889998888

output:

909431898

result:

ok 1 number(s): "909431898"

Test #17:

score: 0
Accepted
time: 178ms
memory: 3948kb

input:

1
99999999999999999999999998989899988889989889999888

output:

289727470

result:

ok 1 number(s): "289727470"

Test #18:

score: 0
Accepted
time: 171ms
memory: 4160kb

input:

1
99999999999999999999999998998988898888898889898999

output:

962896416

result:

ok 1 number(s): "962896416"

Test #19:

score: 0
Accepted
time: 74ms
memory: 4160kb

input:

1
9999999999999999999989988898888989888899

output:

995903330

result:

ok 1 number(s): "995903330"

Test #20:

score: 0
Accepted
time: 73ms
memory: 3896kb

input:

1
9999999999999999999989999889889998998898

output:

385460258

result:

ok 1 number(s): "385460258"

Test #21:

score: 0
Accepted
time: 176ms
memory: 3936kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"