Pagini recente » Cod sursa (job #591668) | Cod sursa (job #1327153) | Cod sursa (job #2978255) | Cod sursa (job #1740639) | Cod sursa (job #877114)
Cod sursa(job #877114)
#include <fstream>
using namespace std;
ifstream fi ("koba.in");
ofstream fo ("koba.out");
const int dim = 10005;
int N, T1, T2, T3, S[dim], viz[10][10][10];
int main ()
{
fi >> N >> T1 >> T2 >> T3;
S[1] = (T1 %= 10);
S[2] = S[1] + (T2 %= 10);
S[3] = S[2] + (T3 %= 10);
for (int i = 3, aux; i <= N; i++)
{
if (viz[T1][T2][T3])
{
int pozc = viz[T1][T2][T3];
int sumac = S[i] - S[pozc];
int lungc = i - pozc;
int s1 = ((N - pozc) / lungc) * sumac;
int s2 = S[pozc + ((N - pozc) % lungc)];
S[0] = s1 + s2;
break;
}
viz[T1][T2][T3] = i;
S[i + 1] = S[i] + (aux = (T3 + T2 * T1) % 10);
T1 = T2;
T2 = T3;
T3 = aux;
}
if (S[0] == 0)
S[0] = S[N];
fo << S[0] << '\n';
return 0;
}