Pagini recente » Cod sursa (job #2429466) | Cod sursa (job #3152279) | Cod sursa (job #2797574) | Cod sursa (job #1134397) | Cod sursa (job #172468)
Cod sursa(job #172468)
#include <stdio.h>
int a[12][10][10],m,a1,a2,a3,s=0,poz,c1,c2,c3,n;
int caut(){
int i,a4,x1,x2,x3;
s=a1+a2+a3;
x1=a1;x2=a2;x3=a3;
a[a1][a2][a3]=1;
for(i=2;i<=n;i++){
if(i>3){
x1=x2;x2=x3;x3=a1;
}
a4=(a3+a1*a2)%10;
a1=a2;a2=a3;a3=a4;
if(a[a1][a2][a3]!=0) {
s-=a1+a2;
poz=a[a1][a2][a3]-1;
a1=x1;a2=x2;a3=x3;
return i-1;
}
a[a1][a2][a3]=i;
s+=a3;
}
return 0;
}
int stg(){
int i,sum=c1+c2+c3,x;
if(poz==0) return 0;
if(poz==1) return c1;
if(poz==2) return c1+c2;
if(poz==3) return c1+c2+c3;
for(i=4;i<=poz;i++){
x=(c3+c1*c2)%10;
c1=c2;c2=c3;c3=x;
sum+=c3;
}
return sum;
}
int main(){
int i,fin,sum,a4;
freopen("koba.in","r",stdin);
freopen("koba.out","w",stdout);
scanf("%d%d%d%d",&n,&a1,&a2,&a3);
a1%=10;a2%=10;a3%=10;
c1=a1;c2=a2;c3=a3;
fin=caut();
sum=s-stg();
n-=fin;
s+=sum*(n/(fin-poz));
for(i=1;i<=n%(fin-poz);i++){
a4=(a3+a1*a2)%10;
a1=a2;a2=a3;a3=a4;
s+=a3;
}
printf("%d",s);
}