Pagini recente » Cod sursa (job #1245987) | Cod sursa (job #388958) | Cod sursa (job #2214571) | Cod sursa (job #1592954) | Cod sursa (job #217599)
Cod sursa(job #217599)
#include <stdio.h>
#include <string>
#define maxn 10010
int N;
char a[maxn], t[maxn];
int checkForInverse(int start)
{
int i, j, aux;
t[1] = start == 2;
t[N] = 0;
for (i=start, j=N; i<j; i++)
{
t[i] = a[i] - a[j] + t[j];
if (t[i] < 0) t[i] += 10;
if (t[i] < 0 || t[i] > 1) return 0;
if (t[i-1] == 0 && t[i] == 1 && a[i-1] == '0') return 0;
if (t[i-1] == 1 && t[i] == 0 && a[i] == '9') return 0;
j--;
if (a[i] - t[i] + t[j+1] - '0' < 0) aux = t[i-1] - 1;
else aux = t[i-1];
if (i != j) t[j] = aux;
else if (t[i] != aux) return 0;
if (t[j] < 0 || t[j] > 1) return 0;
if (t[j] == 0 && t[j+1] == 1 && a[j] == '0') return 0;
if (t[j-1] == 1 && t[j] == 0 && a[j] == '9') return 0;
}
if (start == 1 && t[1] == 1 && a[1] == '1') return 0;
int l = N - start + 1;
if (l&1 && (a[l/2+start]-'0'-t[l/2+start])&1) return 0;
return 1;
}
int main()
{
freopen("invers.in", "r", stdin);
freopen("invers.out", "w", stdout);
int T;
for (scanf("%d ", &T); T; T--)
{
scanf("%s ", a+1);
N = strlen(a+1);
if (checkForInverse(1) || (a[1] == '1' && N > 1 && checkForInverse(2))) printf("DA\n");
else printf("NU\n");
}
return 0;
}