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 1013. K-based Numbers. Version 3

Help Please WA#8
Posted by ABCDE 24 Mar 2006 16:12
Here is my code

#include<stdio.h>
long len[2],a[2][10000];
sum(int x)
{
    int i;
    for(i=9999;i>=10000-len[(x)%2];i--)
    {
        a[(x-1)%2][i] = a[x%2][i] + a[(x-1)%2][i];
        if(a[(x-1)%2][i]>=10)
        {
            a[(x-1)%2][i] = a[(x-1)%2][i]%10;
            a[(x-1)%2][i-1]++;
            if(i==10000-len[(x-1)%2])
                len[(x-1)%2]++;
        }
    }
    return 0;
}
multiply(int x,int k)
{
    int i,tod;
    tod=0;
    for(i=9999;i>=10000-len[(x-1)%2];i--)
    {
        a[(x-1)%2][i] = a[(x-1)%2][i] * k+tod;
        tod = 0;
        if(a[(x-1)%2][i]>=10)
        {
            tod = a[(x-1)%2][i]/10;
            a[(x-1)%2][i] = a[(x-1)%2][i]%10;
            if(i==10000-len[(x-1)%2])
                len[(x-1)%2]++;
        }
    }
    return 0;
}

main()
{
    long n,k,i,tmp;
    scanf("%ld %ld",&n,&k);
    a[0][9999] = 1;
    a[1][9999] = k-1;
    len[0] = 1;
    len[1] = 1;
    for(i=1;i<n;i++)
    {
        sum(i);
        multiply(i,k-1);
    }
    tmp = i;
    for(i=10000-len[tmp%2];i<=9999;i++)
        printf("%ld",a[tmp%2][i]);
    return 0;
}