QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#708556#7518. GCD of Pattern MatchingDeltaxWA 0ms3908kbC++141.3kb2024-11-03 23:45:452024-11-03 23:45:47

Judging History

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

  • [2024-11-03 23:45:47]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3908kb
  • [2024-11-03 23:45:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
typedef pair <int, int> pii;
typedef unsigned long long ull;

inline int read() {
	int x = 0, f = 0;
	char c = getchar();
	while (!isdigit(c)) {if (c == '-') f = 1; c = getchar();}
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c & 15), c = getchar();
	return f? -x : x;
}

ull sum[30];
template <class T>
T gcd(T a, T b) {return b? gcd(b, a % b) : a;}

template <class T>
inline T sub(T x, T y) {return x > y? x - y : y - x;}
int main() {
	int T = read();
	while (T--) {
		int m, p;
		m = read();
		static char str[20];
		scanf("%s", str + 1);
		p = strlen(str + 1);
		ull t = 1;
		for (int i = 1; i <= p; ++i) {
			sum[str[i] - 'a'] += t;
			t = t * m;
		}
		ull ss = 0;
		int cnt = 0;
		for (int i = 0; i < 26; ++i)
			if (sum[i]) ss += sum[i] * cnt, ++cnt;
		ull d = 0;
		if (cnt < m) {
			for (int i = 0; i < 26; ++i)
				if (sum[i]) d = gcd(d, sum[i]);
		}
		else {
			if (m == 2) {
				d = 1ull << (p - 1);
			}
			else {
				ull pre = 0;
				for (int i = 0; i < 26; ++i) {
					if (sum[i]) {	
						if (pre) d = gcd(d, sub(sum[i], pre));
						pre = sum[i];
					}
				}
				d = gcd(d, ss);
			}
		}
		for (int i = 0; i < 26; ++i) sum[i] = 0;
		printf("%llu\n", d);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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

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:

915
4
4
4
4
5
38221
183
157
79
5740
157
79
516
24326193
1015
665
785
1064
11
1015

result:

wrong answer 1st numbers differ - expected: '183', found: '915'