QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#282679#7518. GCD of Pattern MatchingrgnerdplayerWA 0ms3660kbC++201.2kb2023-12-12 19:29:122023-12-12 19:29:12

Judging History

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

  • [2023-12-12 19:29:12]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3660kb
  • [2023-12-12 19:29:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int t;
    cin >> t;

    auto solve = [&]() {
        int m;
        string p;
        cin >> m >> p;

        int n = p.size();
        vector<i64> pw(n);
        pw[0] = 1;
        for (int i = 1; i < n; i++) {
            pw[i] = pw[i - 1] * m;
        }

        if (m == 2) {
            i64 ans = 0;
            for (int i = 0; i < n; i++) {
                if (p[i] == p[0]) {
                    ans += pw[n - 1 - i];
                }
            }
            cout << ans << '\n';
            return;
        }

        map<char, i64> mp;
        for (int i = 0; i < n; i++) {
            mp[p[i]] += pw[n - 1 - i];
        }

        vector<pair<char, i64>> vec(mp.begin(), mp.end());
        vec.resize(m);
        sort(vec.begin(), vec.end());

        i64 ans = 0;
        for (int i = 0; i < m; i++) {
            ans += i * vec[i].second;
        }

        for (int i = 1; i < m; i++) {
            ans = gcd(ans, vec[i].second - vec[0].second);
        }

        cout << ans << '\n';
    };

    while (t--) {
        solve();
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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
1

result:

wrong answer 12th numbers differ - expected: '1229782938247303441', found: '1'