Cod sursa(job #550882)

Utilizator Addy.Adrian Draghici Addy. Data 10 martie 2011 00:05:50
Problema Diviz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <cstdio>
#include <cstring>

#define NMAX 205
#define KMAX 105
#define MOD 30103

char S[NMAX];
int A[NMAX][KMAX], B[NMAX][KMAX], F[10][NMAX], V[10][NMAX], sol, n, m, p,  k, a, b, c, i, j, r, x;

int main () {
	
	freopen ("diviz.in", "r", stdin);
	freopen ("diviz.out", "w", stdout);
	
	S[0] = ' ';
	scanf ("%d %d %d\n%s", &k, &a, &b, S + 1);
	n = strlen (S) - 1;
	
	for (c = 0; c < 10; c++) {
        F[c][n+1] = n+1;
        for (i = n; i >= 1; i--)
            if (S[i] - '0' != c)
				F[c][i] = F[c][i+1];
            else F[c][i] = i;
        for (i = 1; i <= n; i++)
            V[c][i] = V[c][i-1] + (S[i] - '0' == c);
    }
	
	for (c = 1; c <= 9; c++) A[ F[c][1] ][c % k] = 1;
	
	if (a == 1)
		for (j = 1; j <= n; j++) {
			sol += A[j][0];
			if (sol >= MOD) sol -= MOD;
		}
	
	for (i = 1; i < b; i++) {
		for (j = i; j < n; j++)
			for (r = 0; r < k; r++)
				for (c = 0; c <= 9; c++)
					if (A[j][r] && F[c][j+1] != n + 1) {
						
						p = F[c][j+1];
						if (V[c][j+1] - V[c][p] > 0) continue;
						
						m = ((r * 10) + c) % k;
						
						x = B[p][m], x += A[j][r];
						if (x >= MOD) x -= MOD;
						
						B[p][m] = x;
					}
		
		if (a <= i + 1)
			for (j = 1; j <= n; j++) {
				sol += B[j][0];
				if (sol >= MOD) sol -= MOD;
			}
		
		memcpy (A, B, sizeof (B));
		memset (B, 0, sizeof (B));
	}
	
	printf ("%d", sol);
	
	return 0;
}