ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1153. Supercomputer

Why wrong answer do I read the input correctly?PLS C++
Posted by Fechete Dan Ionut[dany] 31 Jan 2003 13:38
#include <stdio.h>
#include <stdlib.h>
int i,j,n,m,k;
int v[5005];
int x[5005];
int aux[5005];
int rest[5005];
int rez[5005];
int decod (char c)
{
if (c=='0') return 0;
if (c=='1') return 1;
if (c=='2') return 2;
if (c=='3') return 3;
if (c=='4') return 4;
if (c=='5') return 5;
if (c=='6') return 6;
if (c=='7') return 7;
if (c=='8') return 8;
if (c=='9') return 9;
return 11;
}
void readdata()
{
FILE *f=stdin;
char c,ce;
fscanf(f,"%c",&c);
while (!feof(f)&&decod(c)<10)
 {
 ce=c;
 v[0]++;
 v[v[0]]=decod(ce);
 fscanf (f,"%c",&c);
 }
if (v[0]==1&&v[1]==0)
 {
 printf ("0");
 exit(0);
 }
for (i=1;i<=v[0]/2;i++)
{
 k=v[i];
 v[i]=v[v[0]-i+1];
 v[v[0]-i+1]=k;
 }
for (i=1;i<=v[0];i++)
 v[i]*=8;
for (i=1;i<v[0];i++)
 {
 v[i+1]+=v[i]/10;
 v[i]%=10;
 }
if (v[i]>=10)
 {
 v[0]++;
 v[i+1]+=v[i]/10;
 v[i]%=10;
 }
i=1;
while (v[i]==9)
 {
 v[i]=0;
 i++;
 }
v[i]++;
if (v[v[0]]==10)
 {
 v[0]++;
 v[v[0]-1]=0;
 v[v[0]]=1;
 }
fclose (f);
}
void inmult2()
{
aux[0]=0;
int re=0;
aux[0]=1;
aux[1]=0;
for (i=n;i>0;i--)
 {
 aux[0]++;
 aux[n-i+2]=x[i]*2+re;
 if (aux[n-i+2]>9) re=1,aux[n-i+2]%=10;
 else re=0;
 }
if (re>0) aux[0]++,aux[aux[0]]=1;
}
int bun(int cifr)
{
aux[1]=cifr;
int re=0;
rez[0]=0;
for (j=1;j<=aux[0];j++)
 {
 rez[0]++;
 rez[j]=aux[j]*cifr+re;
 re=rez[j]/10;rez[j]%=10;
 }
if (re>0)
    rez[0]++,rez[rez[0]]=re;
for (j=rez[0]+1;j<=rest[0];j++) rez[j]=0;
for (j=1;j<=rest[0];j++)
 {
 if (rest[j]>rez[rest[0]-j+1]) return 1;
 if (rest[j]<rez[rest[0]-j+1]) return 0;
 }
return 1;
}
void scade()
{
for (j=1;j<=rez[0];j++)
 {
 if (rest[rest[0]-j+1]<rez[j])
  {
  rest[rest[0]-j+1]+=10;
  i=j+1;
  while (rest[rest[0]-i+1]==0)rest[rest[0]-i+1]=9,i++;
  rest[rest[0]-i+1]--;
  }
 rest[rest[0]-j+1]-=rez[j];
 }
}
void writedata()
{
FILE *f=stdout;
for (i=v[0];i>0;i--)
 fprintf (f,"%d",v[i]);
fclose(f);
}
int main()
{
readdata();
n=1;
k=0;
m=0;

if (v[0]%2==1) m=v[v[0]],k=v[0]-1;
else m=v[v[0]]*10+v[v[0]-1],k=v[0]-2;
for (i=1;i*i<=m;i++);
i--;
x[1]=i;
m=m-i*i;
rest[0]=2;
rest[1]=m/10%10;
rest[2]=m%10;
for (k=k;k>0;k-=2)
 {
 rest[0]++;
 rest[rest[0]]=v[k];
 rest[0]++;
 rest[rest[0]]=v[k-1];
 inmult2();
 for (i=9;i>=0;i--)
  if (bun(i)==1)  break;
 n++;
 x[n]=i;
 scade();
 }
for (i=1;i<=n/2;i++)
 {
 k=x[i];
 x[i]=x[n-i+1];
 x[n-i+1]=k;
 }
i=1;
while (x[i]==0)
 {
 x[i]=9;
 i++;
 }
x[i]--;
if (x[n]==0) n--;
for (i=0;i<=1000;i++) v[i]=0;
if (x[n]>1) v[0]=n;
else v[0]=n-1;
k=0;
for (i=n;i>0;i--)
 {
 k+=x[i];
 v[i]=k/2;
 k=k%2;
 k*=10;
 }
writedata();
return 0;
}