Cod sursa(job #587101)

Utilizator deneoAdrian Craciun deneo Data 3 mai 2011 21:39:48
Problema Perb Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.41 kb
# include <cstdio>

using namespace std;

int n, m, div, nr, sol, _max, nprim;

const int prim[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 
73, 79 ,83 ,89, 97, 101 ,103 ,107 ,109 ,113 ,127, 131, 137, 139, 149 ,151, 157, 163, 167, 173, 
179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269 ,271 ,277, 281 ,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389 ,397, 401 ,409 ,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541 ,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659 };

int x, y, a[610][610];
char s[610];
int ap[30];
int main (){
	int i, j, mxt, lim;
	
	freopen ("perb.in", "r", stdin);
	freopen ("perb.out", "w", stdout);
	
	scanf ("%d%d", &n, &m);
	scanf (" %s", s + 1);
	
	for(i = 1; i <= n; ++i)
		for(j = 1; j <= n; ++j)
			a[i][j] = -1;
	
	for (; m > 0; --m){
		scanf ("%d%d", &x, &y);
		if (a[x][y] != -1) printf ("%d\n", a[x][y]);
		else{
			int num;
            num = y - x + 1;
			
			if (num == 1){
				printf ("0\n");
				continue ;
			}
			sol = 2000000000;
			for (nr = 0; prim[nr] < num; ++nr)
					if (num % prim[nr] == 0){
						mxt = 0;
						
						nprim = num / prim[nr];
						lim = x + prim[nr] - 1;
						for (j = x; j <= lim; ++j){
							ap['A' - 'A'] = 0;
							ap['C' - 'A'] = 0;
							ap['G' - 'A'] = 0;
							ap['T' - 'A'] = 0;
							
							for (i = 0; i < nprim; ++i)
								++ap[s[j + i * prim[nr]] - 'A'];
							
							_max = ap['A' - 'A'];
							if (_max < ap['C' - 'A']) _max = ap['C' - 'A'];
							if (_max < ap['G' - 'A']) _max = ap['G' - 'A'];
							if (_max < ap['T' - 'A']) _max = ap['T' - 'A'];
							mxt = mxt + nprim - _max;
						}
						if (sol > mxt && mxt >= 0) sol = mxt;
					}
						
						mxt = 0;
						nprim = num;
						lim = x;
						for (j = x; j <= lim; ++j){
							ap['A' - 'A'] = 0;
							ap['C' - 'A'] = 0;
							ap['G' - 'A'] = 0;
							ap['T' - 'A'] = 0;
							
							for (i = 0; i < nprim; ++i)
								++ap[s[j + i] - 'A'];
							
							_max = ap['A' - 'A'];
							if (_max < ap['C' - 'A']) _max = ap['C' - 'A'];
							if (_max < ap['G' - 'A']) _max = ap['G' - 'A'];
							if (_max < ap['T' - 'A']) _max = ap['T' - 'A'];
							mxt = mxt + nprim - _max;
						}
						
				if (sol > mxt && mxt >= 0) sol = mxt;
			printf ("%d\n", sol);
			a[x][y] = sol;
		}
	}
	return 0;
}