QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#142638 | #6522. Digit Mode | BUET_POTATOES# | AC ✓ | 256ms | 3580kb | C++20 | 2.8kb | 2023-08-19 16:04:02 | 2023-08-19 16:04:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using pll = pair<LL, LL>;
using pii = pair<LL, LL>;
const int N = 51, K = 10;
const LL mod = 1e9 + 7;
using Ti = int;
Ti freq[K];
Ti limits[K];
Ti invf[N];
Ti f[N];
Ti C(Ti n, Ti k){
return (LL)f[n] * invf[k] % mod * invf[n - k] % mod;
}
Ti a[N], b[N], c[N];
void multiply(Ti l1, Ti l2, Ti n){
memset(c, 0, sizeof c);
for(Ti i = 0; i <= l1 && i <= n; i++){
for(Ti j = 0; j <= l2 && i + j <= n; j++){
c[i + j] = (c[i + j] + (LL)a[i] * b[j]) % mod;
}
}
}
Ti calc(Ti n){
memset(a, 0, sizeof a);
a[0] = 1;
Ti cur = 0;
for(Ti i = 0; i < K; i++){
if(limits[i] == 0)continue;
if(limits[i] < 0)return 0;
for(Ti j = 0; j <= limits[i]; j++)b[j] = invf[j];
multiply(cur, limits[i], n);
cur = min(cur + limits[i], n);
for(Ti j = 0; j <= cur; j++)a[j] = c[j];
}
return (LL)a[n] * f[n] % mod;
}
Ti solve(Ti n){
Ti ans = 0;
for(Ti mode = 1; mode < K; mode++){
for(Ti cnt = 0; cnt <= n; cnt++){
for(Ti j = 0; j < K; j++){
limits[j] = cnt + freq[mode] - freq[j] - (j > mode);
}
limits[mode] = 0;
// cout << n - cnt << " " << calc(n - cnt) << endl;
ans = (ans + (LL)calc(n - cnt) * mode % mod * C(n, cnt)) % mod;
}
// cout << ans << "\n";
}
return ans;
}
void testcase(Ti cs){
string s;
cin >> s;
Ti n = s.size();
memset(freq, 0, sizeof freq);
Ti ans = 0;
//0 er hishab
for(Ti i = 1; i < n; i++){
for(Ti j = 1; j < K; j++){
freq[j] = 1;
ans = (ans + solve(n - i - 1)) % mod;
freq[j] = 0;
}
}
// cout << solve(1) << endl;
//onno part
for(Ti i = 0; i < n; i++){
for(Ti j = (i == 0 ? 1 : 0); j < s[i] - '0'; j++){
freq[j]++;
ans = (ans + solve(n - i - 1)) % mod;
freq[j]--;
}
freq[s[i] - '0']++;
}
Ti t = 0;
for(Ti i = 0; i < K; i++)if(freq[i] >= freq[t])t = i;
ans = (ans + t) % mod;
cout << ans << "\n";
}
/*
*/
int bigmod(int a, int n){
int ans = 1;
while(n){
if(n & 1)ans = (LL)ans * a % mod;
n >>= 1;
a = (LL) a * a % mod;
}
return ans;
}
void pre(){
f[0] = 1;
for(int i = 1; i < N; i++)f[i] = (LL)f[i - 1] * i % mod;
invf[N - 1] = bigmod(f[N - 1], mod - 2);
for(int i = N - 2; i >= 0; i--)invf[i] = (LL)invf[i + 1] * (i + 1) % mod;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
pre();
int T;
cin >> T;
for(int i = 1; i <= T; i++){
testcase(i);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3448kb
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: 3452kb
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: 3536kb
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: 0ms
memory: 3472kb
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: 3ms
memory: 3456kb
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: 0ms
memory: 3524kb
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: 4ms
memory: 3516kb
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: 14ms
memory: 3452kb
input:
3 5014252832385738 8762796162648653 919997886706385
output:
892033338 297722019 462512414
result:
ok 3 number(s): "892033338 297722019 462512414"
Test #9:
score: 0
Accepted
time: 34ms
memory: 3564kb
input:
2 775701797726112292362823101 75927988177061355614
output:
371275551 566830847
result:
ok 2 number(s): "371275551 566830847"
Test #10:
score: 0
Accepted
time: 193ms
memory: 3520kb
input:
1 65760982925996012426370962570581226245366145016666
output:
661063035
result:
ok 1 number(s): "661063035"
Test #11:
score: 0
Accepted
time: 204ms
memory: 3516kb
input:
1 62597468169905757754175023836706426691470692832490
output:
9983261
result:
ok 1 number(s): "9983261"
Test #12:
score: 0
Accepted
time: 204ms
memory: 3472kb
input:
1 78912847369504885593964702297317051208901751786824
output:
817123221
result:
ok 1 number(s): "817123221"
Test #13:
score: 0
Accepted
time: 256ms
memory: 3464kb
input:
1 99999999999999999999999999999999999999999999999999
output:
25251932
result:
ok 1 number(s): "25251932"
Test #14:
score: 0
Accepted
time: 176ms
memory: 3460kb
input:
1 999999999999999999999999999999999999999999999
output:
439421821
result:
ok 1 number(s): "439421821"
Test #15:
score: 0
Accepted
time: 117ms
memory: 3520kb
input:
1 9999999999999999999999999999999999999999
output:
387537647
result:
ok 1 number(s): "387537647"
Test #16:
score: 0
Accepted
time: 255ms
memory: 3492kb
input:
1 99999999999999999999999998889999898988888889998888
output:
909431898
result:
ok 1 number(s): "909431898"
Test #17:
score: 0
Accepted
time: 255ms
memory: 3456kb
input:
1 99999999999999999999999998989899988889989889999888
output:
289727470
result:
ok 1 number(s): "289727470"
Test #18:
score: 0
Accepted
time: 255ms
memory: 3460kb
input:
1 99999999999999999999999998998988898888898889898999
output:
962896416
result:
ok 1 number(s): "962896416"
Test #19:
score: 0
Accepted
time: 116ms
memory: 3520kb
input:
1 9999999999999999999989988898888989888899
output:
995903330
result:
ok 1 number(s): "995903330"
Test #20:
score: 0
Accepted
time: 116ms
memory: 3580kb
input:
1 9999999999999999999989999889889998998898
output:
385460258
result:
ok 1 number(s): "385460258"
Test #21:
score: 0
Accepted
time: 256ms
memory: 3564kb
input:
1 99999999999999999999999999999999999999999999999999
output:
25251932
result:
ok 1 number(s): "25251932"