QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#236654 | #7518. GCD of Pattern Matching | szlhc | WA | 0ms | 3900kb | C++14 | 3.4kb | 2023-11-04 09:32:41 | 2023-11-04 09:32:42 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
namespace io {
char ibuf[1 << 22], *ihd = ibuf, *itl = ibuf;
char getc() {
if (ihd == itl) {
ihd = ibuf;
itl = ibuf + fread(ibuf, 1, 1 << 22, stdin);
if (ihd == itl) return EOF;
}
return *(ihd++);
}
char obuf[1 << 22], *otl = obuf;
void putc(char c) {
if (otl - obuf == 1 << 22) {
fwrite(obuf, 1, 1 << 22, stdout);
otl = obuf;
}
*(otl++) = c;
}
void flush() { fwrite(obuf, 1, otl - obuf, stdout); }
}
using io::putc;
template <typename T>
void read(T &x) {
x = 0;
bool sgn = false;
char c = io::getc();
for (; c < '0' || '9' < c; c = io::getc()) sgn ^= (c == '-');
for (; '0' <= c && c <= '9'; c = io::getc()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
template <typename T>
void write(T x) {
static char stk[40];
int tp = 0;
if (x < 0) {
io::putc('-');
x = -x;
}
do {
stk[++tp] = x % 10 + '0';
x /= 10;
} while (x);
while (tp) io::putc(stk[tp--]);
}
using namespace std;
__int128 gcd (__int128 a, __int128 b) {return b ? gcd(b, a % b) : a;}
int T, n, nxt[20], cnt[30];
char s[20];
__int128 pw[20];
vector <char> vec[20];
bool check (vector <char> x, vector <char> y) {
sort(x.begin(), x.end()), sort(y.begin(), y.end());
for (int i = 0; i < x.size(); i++) if (x[i] != y[i]) return 0;
return 1;
}
int main() {
scanf ("%d", &T);
while (T--) {
scanf ("%d%s", &n, s + 1);
int len = strlen(s + 1);
if (n == 2) {
__int128 x = 0;
for (int i = 1; i <= len; i++) x = x * 2 + (s[i] == s[1]);
write(x), putc('\n');
continue;
}
for (int i = 2, j = 0; i <= len; i++) {
while (j && s[j + 1] != s[i]) j = nxt[j];
if (s[j + 1] == s[i]) j++;
nxt[i] = j;
}
pw[0] = 1;
for (int i = 1; i <= len; i++) pw[i] = n * pw[i - 1];
__int128 x = 0;
if (len % (len - nxt[len]) == 0) {
for (int i = 0; i < len / (len - nxt[len]); i++) x += pw[i * (len - nxt[len])];
} else x = 1;
for (int i = len; i >= 1; i--) if (len % i == 0) {
for (int j = 0; j < i; j++) vec[j].clear();
for (int j = 0; j < len; j++) vec[j % i].push_back(s[j + 1]);
bool flag = 1;
for (int j = 0; j < i; j++) flag &= check(vec[0], vec[j]);
if (flag) {
__int128 y = 0;
for (int j = 0; j < i; j++) y += pw[j];
x = x * y / gcd(x, y);
break;
}
}
for (int i = 1; i <= len; i++) cnt[s[i] - 'a']++;
int y = 1;
for (int j = 1; j < n; j++) if ((n - 1) % j == 0) {
int num = 0, lst = 0;
for (int i = 0; i < 26; i++) if (cnt[i] && (!lst || cnt[i] % j == lst)) {
num++;
lst = cnt[i] % j;
}
if (!lst || (num == n && (n * (n - 1) / 2 * lst) % j == 0)) y = y * j / gcd(y, j);
}
x = x * y / gcd(x, y);
write(x), putc('\n');
for (int i = 0; i < 26; i++) cnt[i] = 0;
}
return io::flush(), 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3648kb
input:
5 10 ccpcccpc 10 cpcpcp 10 cpc 4 cpccpc 4 dhcp
output:
10001 10101 1 65 3
result:
ok 5 number(s): "10001 10101 1 65 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
30 2 ab 3 abc 4 abcd 5 abcde 6 abcdef 7 abcdefg 8 abcdefgh 9 abcdefghi 10 abcdefghij 11 abcdefghijk 12 abcdefghijkl 13 abcdefghijklm 14 abcdefghijklmn 15 abcdefghijklmno 16 abcdefghijklmnop 16 a 16 ab 16 abc 16 abcd 16 abcde 16 abcdef 16 abcdefg 16 abcdefgh 16 abcdefghi 16 abcdefghij 16 abcdefghijk ...
output:
2 1 3 2 5 3 7 4 9 5 11 6 13 7 15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
result:
ok 30 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 3900kb
input:
12 10 abccbaabc 10 abcdeedcba 10 abccbaabccba 3 abccbaabccba 4 abcddcba 4 abcddcbaabcddcba 5 online 5 onlie 6 online 3 ccc 10 ccc 16 aaaaaaaaaaaaaaaa
output:
3 11 11000011 2920 15 983055 1 2 1 13 111 1229782938247303441
result:
ok 12 numbers
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3652kb
input:
21 13 abcccbbaccca 11 abcdcdeebdcdca 15 abcdcdeebdcdca 11 abcdcdeebdcacd 15 abcdcdeebdcacd 12 abcdcdeebbae 14 abcbadcbabcd 14 abcbaccbacbb 14 aaaaaabaabbab 10 aaaaabaababbb 9 aaaaabaababb 14 aaaaabaabbaba 10 aaaaabbaabaab 11 aaaaabbabbaabb 14 aaaababbbbbaba 12 aaaabbababbb 12 aaabaabbabab 13 aabaaab...
output:
183 2 2 2 2 1 1 1 1 1 820 1 1 12 8108731 1 1 1 266 1 1
result:
wrong answer 2nd numbers differ - expected: '4', found: '2'