Problem:
Score : 300 points

Problem Statement

You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:

Choose two among A, B and C, then increase both by 1.

Choose one among A, B and C, then increase it by 2.

It can be proved that we can always make A, B and C all equal by repeatedly performing these operations.

Constraints

0 \leq A,B,C \leq 50

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the minimum number of operations required to make A, B and C all equal.

Sample Input 1

2 5 4

Sample Output 1

2

We can make A, B and C all equal by the following operations:

Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.

Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.

Sample Input 2

2 6 3

Sample Output 2

5

Sample Input 3

31 41 5

Sample Output 3

23

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <set>
#include <cstring>
#include <unordered_set>
using namespace std;
/*
int hs(const int *s, int n) {
	int rt = 0;
	for (int i = 0; i < n; i++) 
		rt = rt * 3 + s[i];
	return rt;
}

unordered_set<int> S;

void dfs(int *s, int n) {
	int id = hs(s, n);
	if (S.count(id)) return ;
	S.insert(id);
	for (int i = 1; i < n; i++) 
		if (s[i] != s[i - 1]) {
			int tmp1 = s[i - 1], tmp2 = s[i];
			s[i] = s[i - 1] = 3 - s[i] - s[i - 1];
			dfs(s, n);
			s[i] = tmp2; s[i - 1] = tmp1;
		}
}
*/

const int mod = 998244353;
const int maxn = 2e5 + 7;
char s[maxn];

void add(int &a, int b) {
	a = (a + b) % mod;
}

int f[maxn][3][3][2];
int main() {
	scanf("%s", s);
	int n = strlen(s);
	if (n == 1) printf("1\n");
	else if (n == 2) printf("%d\n", (s[0] == s[1]) ? 1 : 2);
	else if (n == 3) {
		if (s[0] == s[1] && s[0] == s[2]) printf("1\n");
		else if (s[0] != s[1] && s[1] != s[2] && s[2] != s[0]) printf("3\n");
		else if (s[0] != s[1] && s[0] == s[2]) printf("13\n");
		else printf("6\n");
	}
	else {
		int cnt = 0;
		for (int i = 0; i < n; i++) cnt = (cnt + s[i] - 'a') % 3;
		
		f[1][1][1][0] = 1; // (pos, last, mod 3, is diff)
		f[1][2][2][0] = 1;
		f[1][0][0][0] = 1;
		for (int i = 2; i <= n; i++) {
			for (int last = 0; last < 3; last++) 
			for (int last_cnt = 0; last_cnt < 3; last_cnt++)
			for (int is_diff = 0; is_diff < 2; is_diff++) 
			for (int cur = 0; cur < 3; cur++) {
//				if (i == 2 && last == 1 && cur == 1 && is_diff == 0) 
				int nxt_cnt = (last_cnt + cur) % 3;
				int cur_diff = 0;
				if (last == cur) cur_diff = 1;
				add(f[i][cur][nxt_cnt][max(cur_diff, is_diff)], f[i - 1][last][last_cnt][is_diff]);
			}
		}
		
		
		int ans = ((f[n][0][cnt][1] + f[n][1][cnt][1]) % mod + f[n][2][cnt][1]) % mod;
		ans = (ans + 1) % mod;
		for (int i = 1; i < n; i++) 
			if (s[i] == s[i - 1]) ans = (ans + mod - 1) % mod;
		printf("%d\n", ans);
//		printf("%d\n", ((f[n][0][cnt][1] + f[n][1][cnt][1]) % mod + f[n][2][cnt][1]) % mod);
	}
	return 0;
}