Cod sursa(job #1797329)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 4 noiembrie 2016 11:28:42
Problema Invers Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <cstdio>

#define adevaratadevarat true

#define BUF_SIZE 1<<17

#define MAXN 10000

int pos=BUF_SIZE;
char buf[BUF_SIZE];
FILE *fin;

inline char nextch(){
    if(pos==BUF_SIZE) fread(buf, BUF_SIZE, 1, fin), pos=0;
    return buf[pos++];
}

int v[MAXN+1];
char a[MAXN+1];

inline bool incearca(int st, int dr){
    if((st==1)&&(a[1]==0)) return 0;
    int p;
    for(int i=1; i<=dr; i++)
        v[i]=a[i]-'0';
    while(st<dr){
        if((v[st-1]>1)||(v[st-1]<0)) return 0;
        if(v[st-1]==1) v[st]+=10;
        if(v[st]==19) return 0;
        if(v[st]-v[dr]>=10){
            p=dr-1;
            while((p>st)&&(v[p]<=0)) v[p--]=9;
            v[p]--;
            v[st]-=10;
        }
        v[st]-=v[dr];
        st++;
        dr--;
    }
    if(v[st-1]<0) return 0;
    if(st==dr){
        if((v[st]%2==1)||(v[st-1]>1)) return 0;
    }else if(v[st-1]>0) return 0;
    return adevaratadevarat;
}

int main(){
    int t, n;
    char ch;
    FILE *fout;
    fin=fopen("invers.in", "r");
    fout=fopen("invers.out", "w");
    fscanf(fin, "%d ", &t);
    for(; t; t--){
        ch=nextch();
        n=0;
        while(ch!='\n'){
            a[++n]=ch;
            ch=nextch();
        }
        if(incearca(1, n)) fprintf(fout, "DA\n");
        else if((n>1)&&(incearca(2, n))) fprintf(fout, "DA\n");
        else fprintf(fout, "NU\n");
    }
    fclose(fin);
    fclose(fout);
    return 0;
}