Cod sursa(job #3316296)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 18 octombrie 2025 11:07:11
Problema Koba Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("koba.in");
ofstream fout("koba.out");

const int MOD = 10;

int n, sum_before_cycle, sum_cycle, sum_full;
array<int, 3> t;
bool visited[MOD + 1][MOD + 1][MOD + 1];
vector<array<int, 3>> cycle;

void go_next(array<int, 3>& v) {
	v = {v[1], v[2], (v[2] + v[1] * v[0]) % MOD};
}

int main() {
	fin >> n >> t[0] >> t[1] >> t[2];
	t[0] %= MOD; t[1] %= MOD; t[2] %= MOD;
	
	if(n <= 3) {
		int answer = 0;
		for(int i = 0; i < n; i++) {
			answer += t[i];
		}
		fout << answer << '\n';
		return 0;
	}

	sum_before_cycle = t[0] + t[1] + t[2];
	n -= 3;
	visited[t[0]][t[1]][t[2]] = 1;
	go_next(t);
	while(n && !visited[t[0]][t[1]][t[2]]) {
		visited[t[0]][t[1]][t[2]] = 1;
		sum_before_cycle += t[2];
		go_next(t);
		n--;
	}

	if(n == 0) {
		fout << sum_before_cycle << '\n';
		return 0;
	}

	array<int, 3> start = t;
    cycle.push_back(t);
    sum_cycle += t[2];
    go_next(t);
	while(t != start) {
		sum_cycle += t[2];
		cycle.push_back(t);
		go_next(t);
	}
	
	n--; // 0-indexing
	sum_full = sum_before_cycle + n / cycle.size() * sum_cycle;
	for(int i = 0; i <= n % (int) cycle.size(); i++) {
		sum_full += cycle[i][2];
	}
	fout << sum_full << '\n';
	return 0;
}