Cod sursa(job #176112)

Utilizator MciprianMMciprianM MciprianM Data 10 aprilie 2008 19:08:26
Problema Koba Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include<fstream>
using namespace std;
int a, b, c, d, n, s,d2;
int l, t[1024], p[1024];
inline int u(int x){
  return x%10;
}

int main(){
  int i;
  ifstream f("koba.in");
  f>>n>>a>>b>>c;
  f.close();
  l=0;
  d=u(a)*100+u(b)*10+u(c);
  p[l++]=u(a);
  p[l++]=u(b);
  p[l++]=u(c);
  s=0;
  s+=u(a)+u(b)+u(c);
  for(i=4;i<=n&&!t[d];i++){
    t[d]=1;
    d-=u(a)*100;
    d2=u(c)+u(a)*u(b);
    d2=u(d2);
    d=d*10+d2;
    p[l++]=d2;
    a=b;b=c;c=d2;s+=d2;
  }
  if(t[d]){
    s=0; d2=0;
    for(i=0;i<l;i++)
      if((p[i]*100+p[i+1]*10+p[i+2])==d)
       {	d=i;break;}
      else{
	s+=p[i];
	d2++;
      }
      int s2=0, d3=0;
      l-=3;
      for(i=d;i<l;i++){
	s2+=p[i];
	d3++;
      }
      n-=d2;
      int x=n/d3;
      s+=x*s2;
      x=n%d3;
      for(i=d;x>0;i++,x--)
	s+=p[i];
  }
  ofstream g("koba.out");
  g<<s<<'\n';
  g.close();
  return 0;
}