QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#137659 | #6522. Digit Mode | mshcherba# | AC ✓ | 151ms | 4856kb | C++17 | 2.9kb | 2023-08-10 16:00:08 | 2023-08-10 16:00:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FILL(a, b) memset(a, b, sizeof(a))
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int mod = 1'000'000'007;
const int MAX = 52;
void updAdd(int& x, int val) {
x += val;
if (x >= mod) {
x -= mod;
}
}
int mult(int a, int b) {
return (LL)a * b % mod;
}
int fact[MAX], invFact[MAX];
int dp[11][11][MAX][MAX];
int cnt[10];
void calcDP(int maxLen, bool needZero) {
dp[10][10][0][0] = 1;
RFOR(curDigit, 11, 1) {
FOR(mxDigit, curDigit, 11) {
FOR(mxCnt, 0, maxLen + 1) {
FOR(len, mxCnt, maxLen + 1) {
int val = dp[curDigit][mxDigit][mxCnt][len];
if (val == 0) {
continue;
}
//cerr << curDigit << " " << mxDigit << " " << mxCnt << " " << len << endl;
FOR(i, 0, maxLen + 1 - len - cnt[curDigit - 1]) {
int cur = mult(invFact[i], val);
int newCnt = cnt[curDigit - 1] + i;
int newLen = len + newCnt;
if (needZero && curDigit - 1 == 0) {
cur = mult(cur, len);
}
if (newCnt > mxCnt) {
updAdd(dp[curDigit - 1][curDigit - 1][newCnt][newLen], cur);
}
else {
updAdd(dp[curDigit - 1][mxDigit][mxCnt][newLen], cur);
}
}
}
}
}
}
}
void clearDP(int maxLen) {
memset(dp, 0, sizeof dp);
/*FOR(curDigit, 0, 11) {
FOR(mxDigit, curDigit, 11) {
FOR(mxCnt, 0, maxLen + 1) {
FOR(len, mxCnt, maxLen + 1) {
dp[curDigit][mxDigit][mxCnt][len] = 0;
}
}
}
}*/
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
invFact[1] = 1;
FOR(i, 2, MAX) {
invFact[i] = mult(mod - mod / i, invFact[mod % i]);
assert(mult(invFact[i], i) == 1);
}
fact[0] = invFact[0] = 1;
FOR(i, 1, MAX) {
fact[i] = mult(fact[i - 1], i);
invFact[i] = mult(invFact[i - 1], invFact[i]);
assert(mult(invFact[i], fact[i]) == 1);
}
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
FOR(i, 0, 10) {
cnt[i] = 0;
}
calcDP(SZ(s) - 1, true);
int ans = 0;
FOR(mxDigit, 0, 10) {
FOR(mxCnt, 1, SZ(s)) {
FOR(len, mxCnt, SZ(s)) {
updAdd(ans, mult(mult(fact[len - 1], dp[0][mxDigit][mxCnt][len]), mxDigit));
}
}
}
clearDP(SZ(s) - 1);
FOR(i, 0, SZ(s)) {
FOR(d, i == 0 ? 1 : 0, s[i] - '0' + (i == SZ(s) - 1 ? 1 : 0)) {
cnt[d]++;
calcDP(SZ(s), false);
FOR(mxDigit, 0, 10) {
FOR(mxCnt, 1, SZ(s) + 1) {
updAdd(ans, mult(mult(fact[SZ(s) - i - 1], dp[0][mxDigit][mxCnt][SZ(s)]), mxDigit));
}
}
clearDP(SZ(s));
cnt[d]--;
}
cnt[s[i] - '0']++;
}
cout << ans << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 4732kb
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: 11ms
memory: 4728kb
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: 10ms
memory: 4728kb
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: 9ms
memory: 4848kb
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: 9ms
memory: 4804kb
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: 8ms
memory: 4748kb
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: 9ms
memory: 4796kb
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: 12ms
memory: 4796kb
input:
3 5014252832385738 8762796162648653 919997886706385
output:
892033338 297722019 462512414
result:
ok 3 number(s): "892033338 297722019 462512414"
Test #9:
score: 0
Accepted
time: 25ms
memory: 4744kb
input:
2 775701797726112292362823101 75927988177061355614
output:
371275551 566830847
result:
ok 2 number(s): "371275551 566830847"
Test #10:
score: 0
Accepted
time: 139ms
memory: 4792kb
input:
1 65760982925996012426370962570581226245366145016666
output:
661063035
result:
ok 1 number(s): "661063035"
Test #11:
score: 0
Accepted
time: 145ms
memory: 4740kb
input:
1 62597468169905757754175023836706426691470692832490
output:
9983261
result:
ok 1 number(s): "9983261"
Test #12:
score: 0
Accepted
time: 144ms
memory: 4732kb
input:
1 78912847369504885593964702297317051208901751786824
output:
817123221
result:
ok 1 number(s): "817123221"
Test #13:
score: 0
Accepted
time: 143ms
memory: 4740kb
input:
1 99999999999999999999999999999999999999999999999999
output:
25251932
result:
ok 1 number(s): "25251932"
Test #14:
score: 0
Accepted
time: 106ms
memory: 4808kb
input:
1 999999999999999999999999999999999999999999999
output:
439421821
result:
ok 1 number(s): "439421821"
Test #15:
score: 0
Accepted
time: 71ms
memory: 4856kb
input:
1 9999999999999999999999999999999999999999
output:
387537647
result:
ok 1 number(s): "387537647"
Test #16:
score: 0
Accepted
time: 145ms
memory: 4728kb
input:
1 99999999999999999999999998889999898988888889998888
output:
909431898
result:
ok 1 number(s): "909431898"
Test #17:
score: 0
Accepted
time: 149ms
memory: 4728kb
input:
1 99999999999999999999999998989899988889989889999888
output:
289727470
result:
ok 1 number(s): "289727470"
Test #18:
score: 0
Accepted
time: 148ms
memory: 4796kb
input:
1 99999999999999999999999998998988898888898889898999
output:
962896416
result:
ok 1 number(s): "962896416"
Test #19:
score: 0
Accepted
time: 67ms
memory: 4804kb
input:
1 9999999999999999999989988898888989888899
output:
995903330
result:
ok 1 number(s): "995903330"
Test #20:
score: 0
Accepted
time: 70ms
memory: 4740kb
input:
1 9999999999999999999989999889889998998898
output:
385460258
result:
ok 1 number(s): "385460258"
Test #21:
score: 0
Accepted
time: 151ms
memory: 4796kb
input:
1 99999999999999999999999999999999999999999999999999
output:
25251932
result:
ok 1 number(s): "25251932"