Cod sursa(job #1925558)

Utilizator giotoPopescu Ioan gioto Data 13 martie 2017 13:25:24
Problema Sarpe Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <cstdio>
#include <cstring>
using namespace std;

int a[1005], b[1005], c[1005];
char s[1001];
int main()
{
    freopen("sarpe.in", "r", stdin);
    freopen("sarpe.out", "w", stdout);
    scanf("%s", s);
    int L = strlen(s);
    if(L == 1 && s[0] == '1'){
        printf("2");
        return 0;
    }
    for(int i = 1; i <= L ; ++i){
        a[L - i + 1] = s[i - 1] - '0';
        b[L - i + 1] = s[i - 1] - '0';
    }
    a[0] = b[0] = L;
    int t = 0;
    for(int i = 1; i <= L ; ++i){
        a[i] = a[i] * 2 + t;
        t = a[i] / 10;
        a[i] = a[i] % 10;
    }
    if(t) a[++a[0]] = t;
    c[0] = b[0] * 2 - 1;
    for(int i = 1; i <= L ; ++i){
        for(int j = 1; j <= L ; ++j)
            c[i + j - 1] += 2 * b[i] * b[j];
    }
    t = 0;
    for(int i = 1; i <= c[0] ; ++i){
        c[i] += t;
        t = c[i] / 10;
        c[i] %= 10;
    }
    if(t) c[++c[0]] = t;

    t = 0;
    for(int i = 1; i <= a[0] ; ++i){
        c[i] = -a[i] + c[i] - t;
        if(c[i] < 0) c[i] += 10, t = 1;
        else t = 0;
    }
    int i = a[0] + 1;
    while(t){
        c[i] -=t;
        if(c[i] < 0) c[i] += 10, t = 1;
        else t = 0;
        ++i;
    }
    while(c[c[0]] == 0 && c[0] > 0) --c[0];
    c[1] += 4;
    t = c[1] / 10;
    int j = 1;
    c[1] = c[1] % 10;
    ++j;
    while(t){
        c[j] = c[j] + t;
        t = c[j] / 10;
        c[j] = c[j] % 10;
        if(j > c[0]) c[0] = j;
        ++j;
    }
    for(int i = c[0]; i >= 1 ; --i)
        printf("%d", c[i]);
    return 0;
}