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 when I submit this program server write Running 0.02s 106kb for so long time (>20 min)I suppose that this action is incorrect
Posted by -)ToT Mup Mou 24 Jul 2002 07:42
#include<iostream.h>
#include<string.h>
#define MAX 2000
    int srav(int a[MAX+100],int b[MAX+100]);
    int sqr();
    int pr[MAX+100],result[MAX+100];
void main(){
    char str[MAX+100];
    int t,j,p[MAX+100],q[MAX+100],mass[MAX+100];
    for(int i=0;i<MAX;i++)
    {
        mass[i]=0;
        str[i]=0;
        p[i]=0;
        q[i]=0;
    }
    cin >> str;
    //for(i=MAX-1;i>=0;i--)
    //    if(str[i])
    int pm=strlen(str);
    if((pm==1)&&(str[0]=='0'))
    {
        cout << 0;
        return ;
    }
    for(i=0;i<pm;i++)
         mass[i]=int(str[pm-i-1])-48;
    int l=0;
    for(i=0;i<MAX;i++)
    {
        t=mass[i];
        mass[i]=(t*2+l)%10;
        l=(t*2+l)/10;
    }
    for(i=MAX-1;i>=0;i--)
        if(mass[i]!=0)
        {
            pm=i+1;
            break;
        }
    q[pm/2+1]=1;
    while(srav(p,q)==1)
    {
        for(i=MAX-1;i>=0;i--)
            pr[i]=0;
        l=0;
        for(i=0;i<MAX;i++)
        {
            pr[i]=(p[i]+q[i]+l)%10;
            l=(p[i]+q[i]+l)/10;
        }
        for(i=MAX-1;i>=0;i--)
            if(pr[i]!=0)
            {
                for(j=i;j>0;j--)
                {
                    if(pr[j]%2!=0)
                        pr[j-1]+=10;
                    pr[j]/=2;
                }
                pr[0]/=2;
                break;
            }
        l=sqr();
        if(l>pm)
            for(i=0;i<MAX;i++)
                q[i]=pr[i];
        if(l<pm)
            for(i=0;i<MAX;i++)
                p[i]=pr[i];
        if(l==pm)
        {
            if(srav(result,mass)==1)
                for(i=0;i<MAX;i++)
                    p[i]=pr[i];
            if(srav(result,mass)==0)
                break;
            if(srav(result,mass)==-1)
                for(i=0;i<MAX;i++)
                    q[i]=pr[i];
        }
    }
    for(i=MAX-1;i>=0;i--)
        if(pr[i]!=0)
        {
            for(j=i;j>=0;j--)
                cout << pr[j];
            break;
        }
}
int srav(int a[MAX],int b[MAX]){
    for(int i=MAX-1;i>=0;i--)
    {
        if(a[i]<b[i])
            return 1;
        if(a[i]>b[i])
            return -1;
    }
    return 0;
}
int sqr(){
    int l,i,j,t;
    for(i=MAX-1;i>=0;i--)
        if(pr[i]!=0)
        {
            l=i+1;
            break;
        }
    for(i=MAX-1;i>=0;i--)
        result[i]=0;
    for(i=0;i<l;i++)
        for(j=0;j<l;j++)
            if(i+j<MAX-5)
            {
                t=pr[i]*pr[j];
                result[i+j]+=t;
            }
            else
                return MAX+200;
    l=0;
    for(i=0;i<MAX-1;i++)
    {
        t=result[i];
        result[i]=(result[i]+l)%10;
        l=(t+l)/10;
    }
    if(l!=0)
        return MAX+200;
    l=0;
    for(i=0;i<MAX;i++)
    {
        t=result[i];
        result[i]=(l+result[i]+pr[i])%10;
        l=(l+t+pr[i])/10;
    }
    if(l!=0)
        return MAX+200;
    for(i=MAX-1;i>=0;i--)
        if(result[i]!=0)
            return i+1;
    return 0;
}