[usaco] Chapter1-Getting started(Section 1.4)


/*
ID: bbezxcy1
PROG: clocks
LANG: C++
*/
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int inf=1<<30;
int cloc[20],num[20],step,vis[20];
bool check(){
	for(int i=1;i<=9;i++){
		if(num[i]!=0){
			return 0;
		}
	}
	return 1;
}
void change(int i,int fuck){
	if(fuck)num[i]=(num[i]+1)%4;
	else{
	    num[i]-=1;
	    if(num[i]<0)num[i]=3;
	}
}
void run(int n,int fuck){
 //   if(fuck)cout<<n<<" fuck
"; if(n==1){ change(1,fuck); change(2,fuck); change(5,fuck); change(4,fuck); return; } if(n==2){ change(1,fuck); change(2,fuck); change(3,fuck); return; } if(n==3){ change(2,fuck); change(3,fuck); change(5,fuck); change(6,fuck); return; } if(n==4){ change(1,fuck); change(4,fuck); change(7,fuck); return; } if(n==5){ change(2,fuck); change(4,fuck); change(5,fuck); change(6,fuck); change(8,fuck); return; } if(n==6){ change(3,fuck); change(6,fuck); change(9,fuck); return; } if(n==7){ change(4,fuck); change(5,fuck); change(7,fuck); change(8,fuck); return; } if(n==8){ change(7,fuck); change(8,fuck); change(9,fuck); return; } else{ change(5,fuck); change(6,fuck); change(8,fuck); change(9,fuck); } } int res[20],res1[20]; void dfs(int pos,int dep){ int i,j,k; if(pos==10){ if(check()&&dep<=step){ for(i=1;i<=9;i++){ res1[i]=res[i]; } step=dep; } return; } for(i=0;i<=3;i++){ res[pos]=i; for(j=0;j<i;j++){ run(pos,1); } dfs(pos+1,dep+i); for(j=0;j<i;j++){ run(pos,0); } } } int main(){ int i,j,a,b,c; freopen("clocks.in","r",stdin ); freopen("clocks.out","w",stdout ); while(scanf("%d",&cloc[1])!=EOF){ step=inf; memset(vis,0,sizeof(vis)); memset(res,0,sizeof(res)); memset(res1,0,sizeof(res1)); num[1]=cloc[1]=(cloc[1]/3)%4; for(i=2;i<=9;i++){ scanf("%d",&cloc[i]); num[i]=cloc[i]=(cloc[i]/3)%4; } step=inf; dfs(1,0); // printf("%d
",step); bool cnm=0; for(i=1;i<=9;i++) { for(j=0;j<res1[i];j++) { if(cnm)cout<<" "; printf("%d",i); cnm=1; } }cout<<endl; } return 0; }
 
     ,        

/*
ID: 123ldss2
PROG: clocks
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int data[10];
int re[10];
int re2[10];

int map[10][11] = {{4,0,1,3,4},{3,0,1,2},{4,1,2,4,5},{3,0,3,6},{5,1,3,4,5,7},{3,2,5,8},
    {4,3,4,6,7},{3,6,7,8},{4,4,5,7,8}
};

bool check()
{
    for(int i = 0; i < 9; i++)
        if(data[i] != 0) return 0;
    return 1;
}
int mi;
void dfs(int pos,int dep)
{
    if(pos >= 9)
    {
        if(dep < mi && check())
        {
            mi = dep;
            for(int i = 0; i < 9; i++)
                re[i] = re2[i];
        }
        return;
    }
    for(int i = 3; i >= 0; i--)
    {
        re2[pos] = i;

//int t = data[pos];
//data[pos] = (data[pos] + i)%4;
        for(int j = 1; j <= map[pos][0]; j++)
            data[ map[pos][j] ] = (data[ map[pos][j] ] + i)%4;
        dfs(pos+1,dep+i);
//data[pos] = t;
        for(int j = 1; j <= map[pos][0]; j++)
            data[ map[pos][j] ] = (data[ map[pos][j] ] - i + 4)%4;
        re2[pos] = 0;
    }
}
int main()
{
    freopen("clocks.in","r",stdin );
    freopen("clocks.out","w",stdout );
    int n;
    for(int i = 0; i < 9; i++)
    {
        cin >> n;
        data[i] = n/3%4;
    }
    memset(re,0,sizeof(re));
    memset(re2,0,sizeof(re2));
    mi = 999999999;
    dfs(0,0);
    if(mi == 999999999)
    {
        cout << "NONE
"; return 0; } bool b = 0; for(int i = 0; i < 9; i++) { for(int j = 0; j < re[i]; j++) { if(b) cout << " "; cout << i+1; b = 1; } } cout << "
"; return 0; }

 
/*
ID: bbezxcy1
PROG: ariprog
LANG: C++
*/
#include<iostream>
#include<cstring>
#include <algorithm>
#include<cstdio>
using namespace std;
const int nMax=500000;
int num[nMax];
bool mark[nMax];
int top,n,m;
class node{
	public:
		int a,b;
}res[nMax];
bool flag;
bool check(int a,int b){
	if(!mark[a])return 0;
    for(int i=0;i<n-1;i++){
		a+=b;
		if(!mark[a]||a>top){
			return 0;
		}
	}
	flag=1;
	return 1;
}

bool cmp(node aa,node bb)
{
    if(aa.b<bb.b)return 1;
    else
    {
        if(aa.b==bb.b)
        {
            if(aa.a<=bb.a)
            {
                return 1;
            }
        }
    }
    return 0;
}
int main(){
    int i,j,a,b,c;
    freopen("ariprog.in","r",stdin );
    freopen("ariprog.out","w",stdout );
	while(scanf("%d%d",&n,&m)!=EOF){
	    flag=0;
		a=0;
		top=m*m*2;
		memset(mark,0,sizeof(mark));
		memset(num,0,sizeof(num));
		for(i=0;i<=m;i++){
			for(j=0;j<=i;j++){
				b=i*i+j*j;
//				cout<<i<<" "<<j<<" "<<b<<endl;
//				cout<<b<<endl;
				num[a++]=b;
				mark[b]=1;
			}
		}
		c=0;
		for(i=0;i<=m*m;i++){
			for(j=1;;j++){
			    if(i+(n-1)*j>top){
					break;
				}
//				cout<<i<<" "<<j<<endl;
				if(check(i,j)){
					res[c].a=i;
					res[c++].b=j;
				}
			}
		}
		if(!flag)
		{
		    printf("NONE
"); } // cout<<"dawd "<<c<<endl; sort(res,res+c,cmp); for(i=0;i<c;i++){ printf("%d %d
",res[i].a,res[i].b); } } return 0; }

  /*
    ID:bbezxcy1
    PROG: milk3
    LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
bool vis[30][30][30];
bool  res[100];
int maxa,maxb,maxc;
void dfs(int a,int b,int c){
	int na,nb,nc;
	if(vis[a][b][c]==1){
		return;
	}
	vis[a][b][c]=1;
	if(a==0){
		res[c]=1;
	}
	if(a!=0){
		if(a>=maxb-b){
			na=a-(maxb-b);
			nb=maxb;
			dfs(na,nb,c);
		}
		else{
			na=0;
			nb=b+a;
			dfs(na,nb,c);
		}
		if(a>=maxc-c){
			na=a-(maxc-c);
			nc=maxc;
			dfs(na,b,nc);
		}
		else{
			na=0;
			nc=c+a;
			dfs(na,b,nc);
		}
	}
	if(b!=0){
		if(b>=maxa-a){
			nb=b-(maxa-a);
			na=maxa;
			dfs(na,nb,c);
		}
		else{
			nb=0;
			na=a+b;
			dfs(na,nb,c);
		}
		if(b>=maxc-c){
			nb=b-(maxc-c);
			nc=maxc;
			dfs(a,nb,nc);
		}
		else{
			nb=0;
			nc=c+b;
			dfs(a,nb,nc);
		}
	}
	if(c!=0){
		if(c>=maxa-a){
			nc=c-(maxa-a);
			na=maxa;
			dfs(na,b,nc);
		}
		else{
			nc=0;
			na=a+c;
			dfs(na,b,nc);
		}
		if(c>=maxb-b){
			nc=c-(maxb-b);
			nb=maxb;
			dfs(a,nb,nc);
		}
		else{
			nc=0;
			nb=b+c;
			dfs(a,nb,nc);
		}
	}
}
int main(){
	int i,j,a,b,c;
    freopen("milk3.in","r",stdin );
    freopen("milk3.out","w",stdout );
	while(scanf("%d%d%d",&maxa,&maxb,&maxc)!=EOF){
		memset(vis,0,sizeof(vis));
		dfs(0,0,maxc);
		b=0;
		for(i=0;i<100;i++)
		{
		    if(res[i]==1)
		    {
		        if(b)printf(" ");
		        printf("%d",i);
		        b=1;
		    }
		}printf("
"); } return 0; }