Cod sursa(job #270095)

Utilizator Addy.Adrian Draghici Addy. Data 3 martie 2009 19:08:25
Problema Koba Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <stdio.h>

int v[1001];
long long n,i,t1,t2,t3,x,p,a,b,c,j,s,t,w,sc,r,aux;

int main(){

  FILE *f = fopen("koba.in", "r");
  FILE *g = fopen("koba.out", "w");

  fscanf(f,"%lld %lld %lld %lld",&n,&t1,&t2,&t3);

  a = t1; b = t2; c = t3;

  for (i=1; i<=1001; i++) {
    t1 %= 10; t2 %= 10; t3 %= 10;
    x = t1*100 + t2*10 + t3;
    if (!v[x])
      v[x] = x;
    else {
      p = v[x];
      break;
    }
    aux = t3;
    t3 = t3 + t2*t1;
    t1 = t2;
    t2 = aux;
  }

  a %= 10;  b %= 10;  c %= 10;
  j = a*100 + b*10 + c;
  s = a+b;

  while (j!=p) {
    s += c;
    w++;
    aux = c;
    c = c + a*b;
    a = b;
    b = aux;
    a %= 10;  b %= 10;  c %= 10;
    j = c + b*10 + a*100;
  }

  sc = a+b;
  do {
    t++;
    sc += c;
    aux = c;
    c = c + a*b;
    a = b;
    b = aux;
    a %= 10; b %= 10; c %= 10;
    j = a*100 + b*10 +c;
  }
  while (j!=p);

  r = (n-w) % t;

  s += a+b;
  for (i=1; i<=r; i++) {
    s += c;
    aux = c;
    c = c + a*b;
    a = b;
    b = aux;
    a %= 10; b %= 10; c %= 10;
  }

  s += (n-w)/t*sc;

  fprintf(g,"%lld",s);


  fclose(f);
  fclose(g);

  return 0;
}