QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#236633 | #7518. GCD of Pattern Matching | szlhc | WA | 0ms | 1664kb | C++14 | 2.7kb | 2023-11-04 09:14:25 | 2023-11-04 09:14:25 |
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];
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 = 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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 1516kb
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: 1660kb
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: -100
Wrong Answer
time: 0ms
memory: 1664kb
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 1 1000001 730 3 196611 1 2 1 13 111 1229782938247303441
result:
wrong answer 2nd numbers differ - expected: '11', found: '1'