Cod sursa(job #586655)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 2 mai 2011 18:13:37
Problema Perb Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
# include <cstdio>
# include <cstring>
using namespace std;

int n, m, i, j, div, nr, l, sol, _max, nprim;
int prim[] = {0, 1, 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 };

int x, y, a[610][610];
char s[610];
int ap[100];
int main (){
	freopen ("perb.in", "r", stdin);
	freopen ("perb.out", "w", stdout);
	
	scanf ("%d%d", &n, &m);
	scanf (" %s", s + 1); l = strlen (s) - 1;
	
	for (; m > 0; --m){
		scanf ("%d%d", &x, &y);
		if (a[x][y]) printf ("%d", a[x][y]);
		else{
			int num;
            num = y - x + 1;
			sol = 2000000000;
			for (nr = 1; prim[nr] < num; ++nr){
				int mxt = 0;
				nprim = num / prim[nr];
				if (num % prim[nr] == 0){
					int 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 += nprim - _max;
					}
					if (sol > mxt) sol = mxt;
				}
			}
			
			printf ("%d\n", sol);
			a[x][y] = sol;
		}
	}
	return 0;
}