QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#235195 | #670. K-th String | ckiseki# | AC ✓ | 1ms | 3528kb | C++20 | 2.6kb | 2023-11-02 15:52:48 | 2023-11-02 15:52:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#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 modadd(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
int modsub(int a, int b) {
return a - b < 0 ? a - b + mod : a - b;
}
int modmul(int64_t a, int64_t b) {
return static_cast<int>(a * b % mod);
}
int brute(int n, int k, string s) {
int ans = 0;
string p;
for (int i = 0; i < n; i++)
p += char(i + 'a');
do {
vector<string> sub;
for (int i = 0; i < n; i++)
for (int len = 1; i + len <= n; len++)
sub.emplace_back(p.substr(i, len));
sort(all(sub));
if (sub[k - 1] == s)
++ans;
} while (next_permutation(all(p)));
return ans;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, k;
cin >> n >> k;
string s;
cin >> s;
// int brute_ans = brute(n, k, s);
// debug(brute_ans);
const int ch = s[0] - 'a';
bool used[26] = {};
for (char c : s)
used[c - 'a'] = true;
int small = 0, big = 0;
for (int i = 0; i < ch; i++)
if (!used[i])
small += 1;
for (int i = ch + 1; i < n; i++)
if (!used[i])
big += 1;
const int S = n * (n + 1) / 2;
int ans = 0;
for (int A = 0; A + s.size() <= n; A++) {
auto dp = vector(small + 1, vector(S + 1, 0));
int z = k - int(s.size());
for (size_t j = 1; j < s.size(); j++) {
if (s[j] < s[0]) {
z -= A + (s.size() - j);
}
}
if (z < 0) {
continue;
}
dp[0][0] = 1;
for (int w = 1; w <= n; w++) {
// A + 1, A + 2, ... A + s.size()
if (A + 1 <= w && w <= A + s.size()) continue;
for (int c = small; c >= 1; c--) {
for (int j = S; j >= w; j--) {
dp[c][j] = modadd(dp[c][j], dp[c - 1][j - w]);
}
}
}
int cur = dp[small][z];
debug(cur);
ans = modadd(ans, cur);
}
for (int j = 1; j <= small; j++)
ans = modmul(ans, j);
for (int j = 1; j <= big; j++)
ans = modmul(ans, j);
debug(small, big);
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3484kb
input:
7 12 caedgfb
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3424kb
input:
2 3 b
output:
1
result:
ok single line: '1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3456kb
input:
8 4 febadh
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3460kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
4 9 bca
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3460kb
input:
3 2 bc
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3408kb
input:
8 8 acgefbdh
output:
1
result:
ok single line: '1'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3460kb
input:
4 7 cb
output:
2
result:
ok single line: '2'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
8 36 hgfaec
output:
2
result:
ok single line: '2'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3428kb
input:
8 28 feadch
output:
1
result:
ok single line: '1'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3452kb
input:
3 6 cba
output:
1
result:
ok single line: '1'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3432kb
input:
8 24 fhgdce
output:
2
result:
ok single line: '2'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3408kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
8 17 dhcfbae
output:
1
result:
ok single line: '1'
Test #16:
score: 0
Accepted
time: 1ms
memory: 3456kb
input:
8 26 ebcgdfha
output:
1
result:
ok single line: '1'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
2 2 ab
output:
1
result:
ok single line: '1'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3404kb
input:
6 5 abdef
output:
2
result:
ok single line: '2'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3412kb
input:
6 10 bfaecd
output:
1
result:
ok single line: '1'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
8 26 fgbceah
output:
1
result:
ok single line: '1'
Test #21:
score: 0
Accepted
time: 0ms
memory: 3456kb
input:
8 8 afeghdbc
output:
1
result:
ok single line: '1'
Test #22:
score: 0
Accepted
time: 1ms
memory: 3460kb
input:
8 33 gaedbhfc
output:
1
result:
ok single line: '1'
Test #23:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
8 36 hdegbacf
output:
1
result:
ok single line: '1'
Test #24:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #25:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
2 3 ba
output:
1
result:
ok single line: '1'
Test #26:
score: 0
Accepted
time: 0ms
memory: 3412kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #27:
score: 0
Accepted
time: 0ms
memory: 3408kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #28:
score: 0
Accepted
time: 0ms
memory: 3408kb
input:
8 24 dafbge
output:
1
result:
ok single line: '1'
Test #29:
score: 0
Accepted
time: 0ms
memory: 3480kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #30:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
8 28 fahdecgb
output:
1
result:
ok single line: '1'
Test #31:
score: 0
Accepted
time: 0ms
memory: 3412kb
input:
2 2 b
output:
1
result:
ok single line: '1'
Test #32:
score: 0
Accepted
time: 0ms
memory: 3400kb
input:
8 14 beahcfdg
output:
1
result:
ok single line: '1'
Test #33:
score: 0
Accepted
time: 0ms
memory: 3420kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #34:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
8 13 chfgebad
output:
1
result:
ok single line: '1'
Test #35:
score: 0
Accepted
time: 0ms
memory: 3396kb
input:
4 9 cad
output:
1
result:
ok single line: '1'
Test #36:
score: 0
Accepted
time: 0ms
memory: 3428kb
input:
8 19 dcghfbea
output:
1
result:
ok single line: '1'
Test #37:
score: 0
Accepted
time: 0ms
memory: 3392kb
input:
7 13 cagfde
output:
1
result:
ok single line: '1'
Test #38:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
8 15 caefdhg
output:
1
result:
ok single line: '1'
Test #39:
score: 0
Accepted
time: 0ms
memory: 3456kb
input:
8 6 agchbe
output:
6
result:
ok single line: '6'
Test #40:
score: 0
Accepted
time: 0ms
memory: 3420kb
input:
8 24 ecdgbf
output:
2
result:
ok single line: '2'
Test #41:
score: 0
Accepted
time: 0ms
memory: 3420kb
input:
5 9 dac
output:
1
result:
ok single line: '1'
Test #42:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
8 16 cgehaf
output:
1
result:
ok single line: '1'
Test #43:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
6 15 ebfd
output:
2
result:
ok single line: '2'
Test #44:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
8 31 gcdebh
output:
2
result:
ok single line: '2'
Test #45:
score: 0
Accepted
time: 0ms
memory: 3404kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #46:
score: 0
Accepted
time: 0ms
memory: 3412kb
input:
8 12 bfheagcd
output:
1
result:
ok single line: '1'
Test #47:
score: 0
Accepted
time: 0ms
memory: 3456kb
input:
3 2 b
output:
2
result:
ok single line: '2'
Test #48:
score: 0
Accepted
time: 0ms
memory: 3408kb
input:
9 7 bcidafg
output:
0
result:
ok single line: '0'
Test #49:
score: 0
Accepted
time: 0ms
memory: 3460kb
input:
2 1 a
output:
2
result:
ok single line: '2'
Test #50:
score: 0
Accepted
time: 0ms
memory: 3468kb
input:
18 94 pgamldbkqncrfeji
output:
0
result:
ok single line: '0'
Test #51:
score: 0
Accepted
time: 0ms
memory: 3408kb
input:
22 199 rilhdvfuecpbakjtoqsn
output:
0
result:
ok single line: '0'
Test #52:
score: 0
Accepted
time: 1ms
memory: 3408kb
input:
4 5 dba
output:
0
result:
ok single line: '0'
Test #53:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
2 1 ba
output:
0
result:
ok single line: '0'
Test #54:
score: 0
Accepted
time: 0ms
memory: 3388kb
input:
2 1 a
output:
2
result:
ok single line: '2'
Test #55:
score: 0
Accepted
time: 0ms
memory: 3388kb
input:
26 154 jrlnfwachzebpoktudivmqysxg
output:
1
result:
ok single line: '1'
Test #56:
score: 0
Accepted
time: 1ms
memory: 3468kb
input:
3 3 b
output:
2
result:
ok single line: '2'
Test #57:
score: 0
Accepted
time: 1ms
memory: 3504kb
input:
26 281 ucptxmzojbeygsqkadhvwrfnil
output:
1
result:
ok single line: '1'
Test #58:
score: 0
Accepted
time: 0ms
memory: 3428kb
input:
11 40 gbhiackjdfe
output:
1
result:
ok single line: '1'
Test #59:
score: 0
Accepted
time: 0ms
memory: 3424kb
input:
26 179 kobzgdjyticevuqwmprafhxnsl
output:
1
result:
ok single line: '1'
Test #60:
score: 0
Accepted
time: 1ms
memory: 3480kb
input:
5 5 bda
output:
2
result:
ok single line: '2'
Test #61:
score: 0
Accepted
time: 0ms
memory: 3384kb
input:
22 204 rkhtbvpgfqoudmjniaclse
output:
1
result:
ok single line: '1'
Test #62:
score: 0
Accepted
time: 0ms
memory: 3400kb
input:
16 95 lpnhgdmebjcoka
output:
2
result:
ok single line: '2'
Test #63:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
26 94 fczijtnmdoxeskhypwbvquglar
output:
1
result:
ok single line: '1'
Test #64:
score: 0
Accepted
time: 0ms
memory: 3404kb
input:
20 42 dskhoptejmnbcrgalqif
output:
1
result:
ok single line: '1'
Test #65:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
8 19 efhacd
output:
2
result:
ok single line: '2'
Test #66:
score: 0
Accepted
time: 0ms
memory: 3408kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #67:
score: 0
Accepted
time: 1ms
memory: 3400kb
input:
26 148 lyvxhnpstqwfzjbdegumcakor
output:
1
result:
ok single line: '1'
Test #68:
score: 0
Accepted
time: 1ms
memory: 3480kb
input:
12 23 cfbgdahilj
output:
2
result:
ok single line: '2'
Test #69:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
26 311 wpiazehgvcsxjobrqfkmtldyun
output:
1
result:
ok single line: '1'
Test #70:
score: 0
Accepted
time: 0ms
memory: 3460kb
input:
26 302 vhixsutaewdlfrbjqmngczyokp
output:
1
result:
ok single line: '1'
Test #71:
score: 0
Accepted
time: 0ms
memory: 3420kb
input:
26 261 swtflxorcpehjdzguivkaqyb
output:
2
result:
ok single line: '2'
Test #72:
score: 0
Accepted
time: 0ms
memory: 3460kb
input:
10 49 iaejgcdfh
output:
1
result:
ok single line: '1'
Test #73:
score: 0
Accepted
time: 0ms
memory: 3456kb
input:
26 82 enyqsdujflzrcbwhgtaxkivopm
output:
1
result:
ok single line: '1'
Test #74:
score: 0
Accepted
time: 1ms
memory: 3472kb
input:
5 5 bed
output:
1
result:
ok single line: '1'
Test #75:
score: 0
Accepted
time: 0ms
memory: 3392kb
input:
26 96 fzrbpehcvywjgxknuoqtalimsd
output:
1
result:
ok single line: '1'
Test #76:
score: 0
Accepted
time: 0ms
memory: 3428kb
input:
10 14 bhdcjaige
output:
1
result:
ok single line: '1'
Test #77:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
26 342 yqsbrektndmxuolaivzfchgpw
output:
1
result:
ok single line: '1'
Test #78:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
13 16 bedigmalcfk
output:
2
result:
ok single line: '2'
Test #79:
score: 0
Accepted
time: 1ms
memory: 3396kb
input:
26 219 qzbgpfalmvdunjsotrcwkihe
output:
2
result:
ok single line: '2'
Test #80:
score: 0
Accepted
time: 0ms
memory: 3388kb
input:
22 56 dniktuajmpceqlbohrgs
output:
2
result:
ok single line: '2'
Test #81:
score: 0
Accepted
time: 1ms
memory: 3412kb
input:
26 77 dcgilpkznbrvuwehtaxmfyoqsj
output:
1
result:
ok single line: '1'
Test #82:
score: 0
Accepted
time: 0ms
memory: 3456kb
input:
5 3 ace
output:
6
result:
ok single line: '6'
Test #83:
score: 0
Accepted
time: 0ms
memory: 3468kb
input:
26 159 krzcwglqfjpbnaiumsyhdvotxe
output:
1
result:
ok single line: '1'
Test #84:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
3 1 a
output:
6
result:
ok single line: '6'
Test #85:
score: 0
Accepted
time: 0ms
memory: 3404kb
input:
26 251 tpvyjcaluxefgqwbnrmikszo
output:
2
result:
ok single line: '2'
Test #86:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
16 127 olbemcpafjndkgi
output:
1
result:
ok single line: '1'
Test #87:
score: 0
Accepted
time: 1ms
memory: 3456kb
input:
4 9 d
output:
6
result:
ok single line: '6'
Test #88:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
8 4 fg
output:
0
result:
ok single line: '0'
Test #89:
score: 0
Accepted
time: 0ms
memory: 3396kb
input:
8 19 g
output:
0
result:
ok single line: '0'
Test #90:
score: 0
Accepted
time: 0ms
memory: 3480kb
input:
6 14 dc
output:
16
result:
ok single line: '16'
Test #91:
score: 0
Accepted
time: 1ms
memory: 3472kb
input:
8 14 cefgd
output:
4
result:
ok single line: '4'
Test #92:
score: 0
Accepted
time: 1ms
memory: 3484kb
input:
8 24 fegb
output:
12
result:
ok single line: '12'
Test #93:
score: 0
Accepted
time: 1ms
memory: 3512kb
input:
8 14 d
output:
4320
result:
ok single line: '4320'
Test #94:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
8 14 cbhf
output:
18
result:
ok single line: '18'
Test #95:
score: 0
Accepted
time: 1ms
memory: 3372kb
input:
8 19 eb
output:
612
result:
ok single line: '612'
Test #96:
score: 0
Accepted
time: 1ms
memory: 3424kb
input:
5 5 b
output:
24
result:
ok single line: '24'
Test #97:
score: 0
Accepted
time: 1ms
memory: 3472kb
input:
8 6 be
output:
600
result:
ok single line: '600'
Test #98:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
8 16 degb
output:
16
result:
ok single line: '16'
Test #99:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
8 16 e
output:
2880
result:
ok single line: '2880'
Test #100:
score: 0
Accepted
time: 1ms
memory: 3424kb
input:
6 17 e
output:
96
result:
ok single line: '96'
Test #101:
score: 0
Accepted
time: 1ms
memory: 3436kb
input:
22 41 urbv
output:
0
result:
ok single line: '0'
Test #102:
score: 0
Accepted
time: 1ms
memory: 3440kb
input:
19 53 bksora
output:
0
result:
ok single line: '0'
Test #103:
score: 0
Accepted
time: 1ms
memory: 3408kb
input:
8 17 a
output:
0
result:
ok single line: '0'
Test #104:
score: 0
Accepted
time: 1ms
memory: 3416kb
input:
26 310 wxglnbcsfim
output:
707619005
result:
ok single line: '707619005'
Test #105:
score: 0
Accepted
time: 0ms
memory: 3476kb
input:
14 57 femchdbnlgj
output:
2
result:
ok single line: '2'
Test #106:
score: 0
Accepted
time: 1ms
memory: 3484kb
input:
26 190 n
output:
793426072
result:
ok single line: '793426072'
Test #107:
score: 0
Accepted
time: 1ms
memory: 3428kb
input:
19 177 q
output:
384492431
result:
ok single line: '384492431'
Test #108:
score: 0
Accepted
time: 1ms
memory: 3436kb
input:
26 148 lwu
output:
147045489
result:
ok single line: '147045489'
Test #109:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
14 62 i
output:
267468772
result:
ok single line: '267468772'
Test #110:
score: 0
Accepted
time: 0ms
memory: 3392kb
input:
14 48 ga
output:
327196800
result:
ok single line: '327196800'
Test #111:
score: 0
Accepted
time: 0ms
memory: 3476kb
input:
26 181 okvubjzwdxqmc
output:
432166393
result:
ok single line: '432166393'
Test #112:
score: 0
Accepted
time: 1ms
memory: 3444kb
input:
26 310 y
output:
576292958
result:
ok single line: '576292958'
Test #113:
score: 0
Accepted
time: 0ms
memory: 3408kb
input:
26 102 gmoecsvxbqfptdukwrnyaj
output:
24
result:
ok single line: '24'
Test #114:
score: 0
Accepted
time: 0ms
memory: 3408kb
input:
15 71 gdbjakomcle
output:
6
result:
ok single line: '6'
Test #115:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
26 94 hrjcuao
output:
922356513
result:
ok single line: '922356513'
Test #116:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
9 1 a
output:
362880
result:
ok single line: '362880'
Test #117:
score: 0
Accepted
time: 1ms
memory: 3428kb
input:
26 81 fbxgtinmuohswrk
output:
6773760
result:
ok single line: '6773760'
Test #118:
score: 0
Accepted
time: 1ms
memory: 3480kb
input:
15 76 jdlkiboacn
output:
48
result:
ok single line: '48'
Test #119:
score: 0
Accepted
time: 1ms
memory: 3448kb
input:
26 225 s
output:
298955907
result:
ok single line: '298955907'
Test #120:
score: 0
Accepted
time: 1ms
memory: 3480kb
input:
26 75 hysotxlfcg
output:
629258561
result:
ok single line: '629258561'
Test #121:
score: 0
Accepted
time: 1ms
memory: 3392kb
input:
26 169 p
output:
942577163
result:
ok single line: '942577163'
Test #122:
score: 0
Accepted
time: 0ms
memory: 3408kb
input:
14 88 kh
output:
89268480
result:
ok single line: '89268480'
Extra Test:
score: 0
Extra Test Passed