Pagini recente » Cod sursa (job #675296) | Cod sursa (job #2657085) | Cod sursa (job #2131688) | Cod sursa (job #1717601) | Cod sursa (job #2658956)
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream fin( "koba.in" );
ofstream fout( "koba.out" );
vector<int> u;
int f[10][10][10];
int main() {
int n, t1, t2, t3, aux1, aux2;
long long sum = 0, sp = 0;
fin >> n >> t1 >> t2 >> t3;
t1 %= 10;
t2 %= 10;
t3 %= 10;
u.push_back( t1 );
u.push_back( t2 );
u.push_back( t3 );
int h = 2, i = 0;
long long a = t1 + t2 + t3;
while ( i < n && f[t1][t2][t3] == 0 ) {
f[t1][t2][t3] = h;
aux1 = t3;
t3 = (t3 + t1 * t2) % 10;
aux2 = t2;
t2 = aux1;
t1 = aux2;
++h;
u.push_back( t3 );
a += t3;
}
if ( i < n ) {
int k = f[t1][t2][t3] + 1, cntp = 0;
for ( int i = k; i < u.size(); ++i ) {
sp += u[i];
++cntp;
}
k = f[t1][t2][t3] - 2;
int cnti = k;
for ( int i = 0; i < k; ++i ) {
sum += u[i];
}
int g = (n - cnti + 1) % cntp;
for ( int i = f[t1][t2][t3] - 2; i < g + f[t1][t2][t3] - 3; ++i ) {
sum += u[i];
}
sum += (n - cnti) / cntp * sp;
fout << sum;
} else {
fout << a;
}
fin.close();
fout.close();
return 0;
}