QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#241161#7518. GCD of Pattern MatchingszlhcWA 0ms1608kbC++142.5kb2023-11-05 23:54:022023-11-05 23:54:02

Judging History

你现在查看的是最新测评结果

  • [2023-11-05 23:54:02]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:1608kb
  • [2023-11-05 23:54:02]
  • 提交

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;
#define i128 __int128
i128 gcd (i128 a, i128 b) {return b ? gcd(b, a % b) : a;}
int T, n, mp[30];
char s[20];
i128 pw[20], val[30];
int main() {
    scanf ("%d", &T);
    while (T--) {
        scanf ("%d%s", &n, s + 1);
        int len = strlen(s + 1);
        if (n == 2) {
            i128 x = 0;
            for (int i = 1; i <= len; i++) x = x * 2 + (s[i] == s[1]);
            write(x), putc('\n');
            continue;
        }
        pw[0] = 1;
        for (int i = 1; i <= len; i++) pw[i] = n * pw[i - 1];
        for (int i = 0; i < 26; i++) val[i] = 0;
        for (int i = 1; i <= len; i++) val[s[i] - 'a'] += pw[len - i];
        sort(val, val + 26);
        int cnt = 0;
        i128 ans = 0;
        for (int i = 0; i < 26; i++) if (val[i]) cnt++;
        if (cnt != n) {
            for (int i = 0; i < 26; i++) if (val[i]) ans = gcd(ans, val[i]);
        } else {
            for (int i = 0; i < 26; i++) mp[i] = 0;
            for (int i = 1, j = 0; i <= len; i++) {
                if (!mp[s[i] - 'a']) mp[s[i] - 'a'] = j++;
                ans = ans * n + mp[s[i] - 'a'];
            }
            for (int i = 0; i < 25; i++) if (val[i]) ans = gcd(ans, val[i + 1] - val[i]);
        }
        write(ans), putc('\n');
    }
    return io::flush(), 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 1608kb

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: 1480kb

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: 1568kb

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
1
1
9
1
2
1
13
111
1229782938247303441

result:

wrong answer 4th numbers differ - expected: '2920', found: '1'