Pagini: [1]   În jos
  Imprimă  
Ajutor Subiect: 439. TMUL  (Citit de 5442 ori)
0 Utilizatori şi 1 Vizitator pe acest subiect.
cmiN
Strain


Karma: 4
Deconectat Deconectat

Mesaje: 11



Vezi Profilul
« : Mai 07, 2011, 13:55:48 »

Nu am nici cea mai mica idee de ce obtin WA pe TMUL (dupa ce trec de asta vreau sa nu scot TLE pe MUL). L-am testat in toate felurile si da bine.

Cod:
#include <stdio.h>
#include <ctype.h>
#define base 1000000000
#define len 9
#define N 1113

unsigned long long first[N], second[N], last[2 * N];

inline void init()
{
    char chr;
    unsigned short cnt;
    unsigned long total;
    first[0] = second[0] = 0;
    cnt = 0;
    total = getchar() - '0';
    while (!isspace(chr = getchar())) {
        if (++cnt != len) {
            total = total * 10 + chr - '0';
        } else {
            first[++first[0]] = total;
            total = chr - '0';
            cnt = 0;
        }
    }
    first[++first[0]] = total;
    cnt = 0;
    total = getchar() - '0';
    while (!(isspace(chr = getchar()) || chr == EOF)) {
        if (++cnt != len) {
            total = total * 10 + chr - '0';
        } else {
            second[++second[0]] = total;
            total = chr - '0';
            cnt = 0;
        }
    }
    second[++second[0]] = total;
}

inline void zfill(unsigned long nr, char str[])
{
    unsigned short cnt = 0, i = 0;
    if (!nr) {
        cnt = 1;
    } else {
        while (nr > 0) {
            ++cnt;
            nr /= 10;
        }
    }
    for (; i < len - cnt; ++i) {
        str[i] = '0';
    }
    str[i] = 0;
}

inline void process()
{
    unsigned long long temp = 0;
    unsigned short i, j;
    char str[10];
    if (!first[1] || !second[1]) {
        putchar('0');
    } else {
        last[0] = first[0] + second[0] - 1;
        for (i = first[0]; i > 0; --i) {
            for (j = second[0]; j > 0; --j) {
                last[i + j - 1] += first[i] * second[j] + temp;
                temp = last[i + j - 1] / base;
                last[i + j - 1] %= base;
            }
        }
        if (temp) {
            printf("%lu", (unsigned long) temp);
        }
        for (i = 1; i <= last[0]; ++i) {
            if (i != 1) {
                zfill((unsigned long) last[i], str);
            } else {
                str[0] = 0;
            }
            printf("%s%lu", str, (unsigned long) last[i]);
            last[i] = 0;
        }
    }
    putchar('\n');
}

int main()
{
    short nr;
    freopen("mul.in", "rt", stdin);
    scanf("%hd\n", &nr);
    while (nr-- > 0) {
        init();
        process();
    }
    return 0;
}

Edit ... am rezolvat, nu aveam grija la mutiplul lungimii bazei si la transport.
http://pastebin.com/iMmPUa4d
« Ultima modificare: Mai 20, 2011, 20:48:09 de către Cosmin Poieana » Memorat
Pagini: [1]   În sus
  Imprimă  
 
Schimbă forumul:  

Powered by SMF 1.1.19 | SMF © 2006-2013, Simple Machines