Cod sursa(job #30971)

Utilizator therain3rVlad Dumitrescu therain3r Data 15 martie 2007 13:05:18
Problema Pavare2 Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <stdio.h>
#include <string.h>

int main() {
	int gasit,poz,i,N,A,B,K,v[100],sol[100],cA,cB,nr=0;

	freopen("pavare2.in","r",stdin);
	scanf("%d %d %d",&N,&A,&B);
	scanf("%d",&K);
	fclose(stdin);
	memset(v,0,sizeof(v));
	for(i=A;i<N;i=i+A+1) v[i]=1;
	gasit=0;
	if (K==1) { memcpy(sol,v,sizeof(v)); K=0; gasit=1; }
	poz=N-1;
	K=K-1;
	while(poz>=0) {
		while(v[poz]==1) poz--;
		if (poz>=0) {
			v[poz]++;
			for (i=poz+1;i<N;i++) v[i]=0;
			poz=N-1;
			if (v[0]==0) { cA=1; cB=0; }
			else { cA=0; cB=1; }
			for(i=1;i<N&&cA<=A&&cB<=B;i++) {
				if ((v[i]==0)&&(v[i-1]==1)) { cA=1; cB=0; }
				if ((v[i]==0)&&(v[i-1]==0)) cA++;
				if ((v[i]==1)&&(v[i-1]==0)) { cA=0; cB=1; }
				if ((v[i]==1)&&(v[i-1]==1)) cB++;
			}
			if ((i==N)&&(cA<=A)&&(cB<=B)) { nr++; K--; }
			if ((!K)&&(!gasit)) { memcpy(sol,v,sizeof(v)); gasit=1; }
		}
	}
	freopen("pavare2.out","w",stdout);
	printf("%d\n",nr+1);
	for(i=0;i<N;i++) printf("%d",sol[i]);
	fclose(stdout);
	return 0;
}