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 1012. K-based Numbers. Version 2

Question!!!
Posted by Promix17 16 Oct 2010 00:11
What's wrong??? What's 6 WA??? Why???
My programm enables big numbers!!! Could you tell me some tests with right answers?
Re: Question!!!
Posted by Promix17 16 Oct 2010 00:12
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

char* add(char *p1, char *p2)
{
    char *p;
    char temp=0, end1=0, end2=0;
    int len,i;
    len=strlen(p1)>strlen(p2)?strlen(p1)+2:strlen(p2)+2;
    p=(char *)malloc(len*sizeof(char));
    for(i=0; i<len-1; ++i)
    {
        if(!p1[i]) end1=1;
        if(!p2[i]) end2=1;
        if(end1&&end2&&!temp) break;
        if(!end1) temp+=(p1[i]-'0');
        if(!end2) temp+=(p2[i]-'0');
        p[i]=temp%10+'0';
        temp/=10;
    }
    p[i]=0;
    while(p[--i]=='0'&& &p[i]!=p) p[i]=0;
    return p;
}

char* sub(char *p1, char *p2)
{
    char *p;
    char temp=0, end1=0, end2=0;
    int len,i;
    len=strlen(p1)>strlen(p2)?strlen(p1)+2:strlen(p2)+2;
    p=(char *)malloc(len*sizeof(char));
    for(i=0; i<len-1; ++i)
    {
        if(!p1[i]) end1=1;
        if(!p2[i]) end2=1;
        if(end1&&end2&&!temp) break;
        if(!end1) temp+=(p1[i]-'0');
        if(!end2) temp-=(p2[i]-'0');
        p[i]=(temp+100)%10+'0';
        if(temp<0)
        {
            temp/=10;
            temp--;
        }
        else temp=0;
    }
    p[i]=0;
    while(p[--i]=='0'&& &p[i]!=p) p[i]=0;
    return p;
}

char* mul(char *p1, int k)
{
    char *p;
    char temp=0, end1=0;
    int len,i;
    len=strlen(p1)+2;
    p=(char *)malloc(len*sizeof(char));
    if(k==10)
    {
        p[0]='0';
        p[1]=0;
        strcat(p,p1);
        return p;
    }
    for(i=0; i<len-1; ++i)
    {
        if(!p1[i]) end1=1;
        if(end1&&!temp) break;
        if(!end1) temp+=(p1[i]-'0')*k;
        p[i]=temp%10+'0';
        temp/=10;
    }
    p[i]=0;
    while(p[--i]=='0'&& &p[i]!=p) p[i]=0;
    return p;
}

void printb(char *p)
{
    int i;
    for(i=strlen(p)-1;i>=0; i--) printf("%c", p[i]);
}

int main()
{
    int N, K;
    int i;
    char* result, *t1, *t2;
    char* Nulls[2000];

    printf("Enter N and K: ");
    scanf("%d%d",&N,&K);

    Nulls[0]="0";
    result=(char *)malloc(2*sizeof(char));
    result[0]=K-1+'0';
    result[1]=0;

    try
    {

    for(i=1; i<N;++i)
    {
        Nulls[i]=sub(result,Nulls[i-1]);
        t1=mul(Nulls[i],K);
        t2=mul(Nulls[i-1],(K-1));
        result=add(t1,t2);
        free(t1);
        free(t2);
    }

    }
    catch(...)
    {
        printf("Error");
        while(!kbhit());
        return 0;
    }

    printf("Result = ");
    printb(result);
    free(result);
    while(!kbhit());
    return 0;
}