Cod sursa(job #217595)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 29 octombrie 2008 01:25:30
Problema Invers Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#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;
		
		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;
	}

	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;
}