Cod sursa(job #1253814)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 1 noiembrie 2014 20:38:06
Problema Koba Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include<fstream>
using namespace std;
int n, a, b, c, x, m, t1, t2, t3, i, d, j;
long long sum, s;
int v[1000], f[100000];
ifstream fin("koba.in");
ofstream fout("koba.out");
int main(){
	fin>> n >> t1 >> t2 >> t3;
	a = t1 % 10;
	b = t2 % 10;
	c = t3 % 10;
	x = a * 100 + b * 10 + c;
	v[x] = 1;
	f[1] = x;
	d = (a * b + c) % 10;
	x = b * 100 + c * 10 + d;
	v[x] = 2;
	s = a + b + c + d;
	f[2] = x;
	for(i = 3; i <= n; i++){
		a = b;
		b = c;
		c = d;
		d = (a * b + c) % 10;
		s += d;
		x = b * 100 + c * 10 + d;
		f[i] = x;
		if(v[x] != 0){
			break;
		}
		else{
			v[x] = i;
		}
	}
	s-= (x / 100 + x / 10 % 10 + x % 10);
	if(v[x] <= 2){
		m = i - v[x];
		for(j = v[x] + 1; j <= i; j++){
			sum += f[j] % 10;
		}
		n -= (i-1);
		s += (n / m) * sum;
		for(j = v[x] + 1; j <= n % m + v[x]; j++){
			s += f[j] % 10;
		}
	}
	else{
		m = i - v[x];
    for(j = v[x] - 2; j < v[x] + m - 2; j++){
        sum += f[j] % 10;
    }
    n -= (i-1);
    s += (n / m) * sum;
    for(j = v[x] - 2; j < n % m + v[x] - 2; j++){
        s += f[j] % 10;
    }
	}
	fout<< s;
	return 0;
}