Submission #67477211


Source Code Expand

Copy
#include <stdio.h>
#include <string.h>
typedef unsigned long long ull;
#define MOD_BY 2892148736031121531ULL
#define MULT 548571274668263447ULL
#define MULT_INV 1933731169782078360ULL
ull add(ull a, ull b) {
return a + b - MOD_BY * (a + b >= MOD_BY);
}
ull sub(ull a, ull b) {
return b == 0 ? a : add(a, MOD_BY - b);
}
ull mul(ull a, ull b) {
ull r = 0;
while (b > 0) {
if (b & 1) r = add(r, a);
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <string.h>

typedef unsigned long long ull;

#define MOD_BY 2892148736031121531ULL
#define MULT 548571274668263447ULL
#define MULT_INV 1933731169782078360ULL

ull add(ull a, ull b) {
	return a + b - MOD_BY * (a + b >= MOD_BY);
}

ull sub(ull a, ull b) {
	return b == 0 ? a : add(a, MOD_BY - b);
}

ull mul(ull a, ull b) {
	ull r = 0;
	while (b > 0) {
		if (b & 1) r = add(r, a);
		a = add(a, a);
		b >>= 1;
	}
	return r;
}

ull mult_pow[512];
ull mult_inv_pow[512];
ull value[512];

ull cache[512][512];

/* [s, e) のハッシュ値を求める */
ull get_hash(int s, int e) {
	if (cache[s][e]) return ~cache[s][e];
	return ~(cache[s][e] = ~mul(sub(value[e], value[s]), mult_inv_pow[s]));
}

int main(void) {
	char S[512];
	int N;
	int i, j, k, ans = 0;
	if (scanf("%511s", S) != 1) return 1;
	N = (int)strlen(S);
	mult_pow[0] = 1;
	mult_inv_pow[0] = 1;
	for (i = 1; i <= N; i++) {
		mult_pow[i] = mul(mult_pow[i - 1], MULT);
		mult_inv_pow[i] = mul(mult_inv_pow[i - 1], MULT_INV);
	}
	for (i = 0; i < N; i++) {
		value[i + 1] = add(value[i], mul((unsigned char)S[i], mult_pow[i]));
	}
	for (i = 2; i < N - 3; i++) { /* S_3 の終わりはどこか */
		for (j = 1; i - j >= 1 && i + 1 + j <= N - 2; j++) { /* S_3 は何文字か */
			if (get_hash(i - j + 1, i + 1) != get_hash(i + 1, i + 1 + j)) continue; /* S_3 != S_4 */
			for (k = 1; i - j - k >= 0 && N - k > i + 1 + j; k++) { /* S_2 は何文字か */
				if (get_hash(i - j - k + 1, i - j + 1) != get_hash(N - k, N)) continue; /* S_2 != S_6 */
				ans++;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}

/*

* S_3 の終わりはどこか
* S_3 は何文字か
* S_2 は何文字か

で全探索 O(|S|^3)

*/

Submission Info

Submission Time
Task B - NIKKEI String
User mikecat
Language C (gcc 12.2.0)
Score 300
Code Size 1773 Byte
Status AC
Exec Time 20 ms
Memory 3716 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 43
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 0 ms 1696 KiB
02.txt AC 0 ms 1696 KiB
03.txt AC 0 ms 1604 KiB
04.txt AC 1 ms 1780 KiB
05.txt AC 0 ms 1684 KiB
06.txt AC 0 ms 1636 KiB
07.txt AC 0 ms 1700 KiB
08.txt AC 0 ms 1808 KiB
09.txt AC 0 ms 1788 KiB
10.txt AC 0 ms 1668 KiB
11.txt AC 10 ms 3716 KiB
12.txt AC 10 ms 3584 KiB
13.txt AC 10 ms 3592 KiB
14.txt AC 10 ms 3716 KiB
15.txt AC 10 ms 3564 KiB
16.txt AC 10 ms 3672 KiB
17.txt AC 10 ms 3672 KiB
18.txt AC 10 ms 3576 KiB
19.txt AC 10 ms 3548 KiB
20.txt AC 10 ms 3536 KiB
21.txt AC 20 ms 3552 KiB
22.txt AC 13 ms 3548 KiB
23.txt AC 12 ms 3712 KiB
24.txt AC 13 ms 3560 KiB
25.txt AC 11 ms 3520 KiB
26.txt AC 11 ms 3568 KiB
27.txt AC 11 ms 3560 KiB
28.txt AC 10 ms 3548 KiB
29.txt AC 10 ms 3712 KiB
30.txt AC 10 ms 3532 KiB
31.txt AC 10 ms 3644 KiB
32.txt AC 10 ms 3552 KiB
33.txt AC 10 ms 3516 KiB
34.txt AC 13 ms 3560 KiB
35.txt AC 20 ms 3672 KiB
36.txt AC 14 ms 3716 KiB
37.txt AC 12 ms 3592 KiB
38.txt AC 13 ms 3648 KiB
39.txt AC 12 ms 3636 KiB
40.txt AC 12 ms 3572 KiB
s1.txt AC 0 ms 1652 KiB
s2.txt AC 0 ms 1632 KiB
s3.txt AC 0 ms 1692 KiB


2025-07-11 (Fri)
04:19:26 +09:00