Most Horrible Code Ever Written By Me

Problem

#include 
using namespace std ;
#define LL long long 
#define inf LLONG_MAX
int T,N,D ;

LL solve(int N,int D){

	if(N == 1){
		LL DP[D] ;
		for(int i=0;i<D;i++){
			DP[i] = (i-1<0 ? inf : DP[i-1]) ;
			if(DP[i] == inf)
				DP[i] = 0 ;			
			DP[i] += (1LL*i*i) ;						
		}
		return DP[D-1] ;
				
	}else if(N == 2){
		LL DP[D][D] ;
		for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				DP[i][j] = min((i-1<0 ? inf : DP[i-1][j]),(j-1<0 ? inf : DP[i][j-1])) ;	
				if(DP[i][j] == inf)
					DP[i][j] = 0 ;			
				DP[i][j] += (1LL*(i^j)*(i+j)) ;
			}
		}	
		return DP[D-1][D-1] ;
	}else if(N == 3){
		LL DP[D][D][D] ;
		for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				for(int k=0;k<D;k++){
					DP[i][j][k] = min(min((i-1<0 ? inf : DP[i-1][j][k]),(j-1<0 ? inf : DP[i][j-1][k])),k-1<0 ? inf : DP[i][j][k-1]) ;
					if(DP[i][j][k] == inf)
						DP[i][j][k] = 0 ;
					DP[i][j][k] += (1LL*(i^j^k)*(i+j+k)) ;
				}			
			}
		}
		return DP[D-1][D-1][D-1] ;
	}else if(N == 4){
		LL DP[D][D][D][D] ;
		for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				for(int k=0;k<D;k++){
					for(int l=0;l<D;l++){
						DP[i][j][k][l] = min(min((i-1<0 ? inf : DP[i-1][j][k][l]),(j-1<0 ? inf : DP[i][j-1][k][l])),min((k-1<0 ? inf : DP[i][j][k-1][l]),(l-1<0 ? inf : DP[i][j][k][l-1]))) ;
						if(DP[i][j][k][l] == inf)
							DP[i][j][k][l] = 0;
						DP[i][j][k][l] += (1LL*(i^j^k^l)*(i+j+k+l)) ;
					}
				}			
			}
		}
		return DP[D-1][D-1][D-1][D-1] ;
	}else if(N == 5){
		LL DP[D][D][D][D][D] ;
		for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				for(int k=0;k<D;k++){
					for(int l=0;l<D;l++){
						for(int m=0;m<D;m++){
							DP[i][j][k][l][m] = min(min(min((i-1<0 ? inf : DP[i-1][j][k][l][m]),(j-1<0 ? inf : DP[i][j-1][k][l][m])),min((k-1<0 ? inf : DP[i][j][k-1][l][m]),(l-1<0 ? inf : DP[i][j][k][l-1][m]))),m-1<0 ? inf : DP[i][j][k][l][m-1]) ;
							if(DP[i][j][k][l][m] == inf)
								DP[i][j][k][l][m] = 0 ;
							DP[i][j][k][l][m] += (1LL*(i^j^k^l^m)*(i+j+k+l+m)) ;
						}
					}
				}			
			}
		}
		return DP[D-1][D-1][D-1][D-1][D-1] ;
	}else if(N == 6){
		LL DP[D][D][D][D][D][D] ;
		for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				for(int k=0;k<D;k++){
					for(int l=0;l<D;l++){
						for(int m=0;m<D;m++){
							for(int n=0;n<D;n++){
								DP[i][j][k][l][m][n] = min(min(min(min((i-1<0 ? inf : DP[i-1][j][k][l][m][n]),(j-1<0 ? inf : DP[i][j-1][k][l][m][n])),min((k-1<0 ? inf : DP[i][j][k-1][l][m][n]),(l-1<0 ? inf : DP[i][j][k][l-1][m][n]))),m-1<0 ? inf : DP[i][j][k][l][m-1][n]),n-1 < 0 ? inf : DP[i][j][k][l][m][n-1]) ;
							if(DP[i][j][k][l][m][n] == inf)
								DP[i][j][k][l][m][n] = 0 ;
							DP[i][j][k][l][m][n] += (1LL*(i^j^k^l^m^n)*(i+j+k+l+m+n)) ;
						}
						}
					}
				}			
			}
		}		
		return DP[D-1][D-1][D-1][D-1][D-1][D-1] ;
	}else if(N == 7){
		LL DP[D][D][D][D][D][D][D] ;
		for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				for(int k=0;k<D;k++){
					for(int l=0;l<D;l++){
						for(int m=0;m<D;m++){
							for(int n=0;n<D;n++){
								for(int o=0;o<D;o++){
									DP[i][j][k][l][m][n][o] = min(min(min(min(min((i-1<0 ? inf : DP[i-1][j][k][l][m][n][o]),(j-1<0 ? inf : DP[i][j-1][k][l][m][n][o])),min((k-1<0 ? inf : DP[i][j][k-1][l][m][n][o]),(l-1<0 ? inf : DP[i][j][k][l-1][m][n][o]))),m-1<0 ? inf : DP[i][j][k][l][m-1][n][o]),n-1 < 0 ? inf : DP[i][j][k][l][m][n-1][o]),o-1<0 ? inf : DP[i][j][k][l][m][n][o-1]) ;
							if(DP[i][j][k][l][m][n][o] == inf)
								DP[i][j][k][l][m][n][o] = 0 ;
							DP[i][j][k][l][m][n][o] += (1LL*(i^j^k^l^m^n^o)*(i+j+k+l+m+n+o)) ;
						}
					}
				}			
			}
		}	
		}}
		return DP[D-1][D-1][D-1][D-1][D-1][D-1][D-1] ;
	}else if(N == 8){
		LL DP[D][D][D][D][D][D][D][D] ;
			for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				for(int k=0;k<D;k++){
					for(int l=0;l<D;l++){
						for(int m=0;m<D;m++){
							for(int n=0;n<D;n++){
								for(int o=0;o<D;o++){
									for(int p=0;p<D;p++){		
							DP[i][j][k][l][m][n][o][p] = min(min(min(min(min(min((i-1<0 ? inf : DP[i-1][j][k][l][m][n][o][p]),(j-1<0 ? inf : DP[i][j-1][k][l][m][n][o][p])),min((k-1<0 ? inf : DP[i][j][k-1][l][m][n][o][p]),(l-1<0 ? inf : DP[i][j][k][l-1][m][n][o][p]))),m-1<0 ? inf : DP[i][j][k][l][m-1][n][o][p]),n-1 < 0 ? inf : DP[i][j][k][l][m][n-1][o][p]),o-1<0 ? inf : DP[i][j][k][l][m][n][o-1][p]),p-1<0 ? inf : DP[i][j][k][l][m][n][o][p-1]) ;
							if(DP[i][j][k][l][m][n][o][p] == inf)
								DP[i][j][k][l][m][n][o][p] = 0 ;
							DP[i][j][k][l][m][n][o][p] += (1LL*(i^j^k^l^m^n^o^p)*(i+j+k+l+m+n+o+p)) ;
						 }
						}	
					}
				}			
			}
		}
		}
	}
		return DP[D-1][D-1][D-1][D-1][D-1][D-1][D-1][D-1] ;
	}else if(N == 9){
		LL DP[D][D][D][D][D][D][D][D][D] ;
			for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				for(int k=0;k<D;k++){
					for(int l=0;l<D;l++){
						for(int m=0;m<D;m++){
							for(int n=0;n<D;n++){
								for(int o=0;o<D;o++){
									for(int p=0;p<D;p++){		
										for(int q=0;q<D;q++){		
					DP[i][j][k][l][m][n][o][p][q] = min(min(min(min(min(min(min((i-1<0 ? inf : DP[i-1][j][k][l][m][n][o][p][q]),(j-1<0 ? inf : DP[i][j-1][k][l][m][n][o][p][q])),min((k-1<0 ? inf : DP[i][j][k-1][l][m][n][o][p][q]),(l-1<0 ? inf : DP[i][j][k][l-1][m][n][o][p][q]))),m-1<0 ? inf : DP[i][j][k][l][m-1][n][o][p][q]),n-1 < 0 ? inf : DP[i][j][k][l][m][n-1][o][p][q]),o-1<0 ? inf : DP[i][j][k][l][m][n][o-1][p][q]),p-1<0 ? inf : DP[i][j][k][l][m][n][o][p-1][q]),q-1<0 ? inf : DP[i][j][k][l][m][n][o][p][q-1]) ;
							if(DP[i][j][k][l][m][n][o][p][q] == inf)
								DP[i][j][k][l][m][n][o][p][q] = 0 ;
						 	DP[i][j][k][l][m][n][o][p][q] += (1LL*(i^j^k^l^m^n^o^p^q)*(i+j+k+l+m+n+o+p+q)) ;
						 }
						}	
					}
				}			
			}
		}
		}}}	
		return DP[D-1][D-1][D-1][D-1][D-1][D-1][D-1][D-1][D-1] ;
	}else if(N == 10){
		LL DP[D][D][D][D][D][D][D][D][D][D] ;
		for(int i=0;i<D;i++){
			for(int j=0;j<D;j++){
				for(int k=0;k<D;k++){
					for(int l=0;l<D;l++){
						for(int m=0;m<D;m++){
							for(int n=0;n<D;n++){
								for(int o=0;o<D;o++){
									for(int p=0;p<D;p++){		
										for(int q=0;q<D;q++){		
											for(int r=0;r> T ;
	while(T--){
		cin >> N >> D ;
		cout << solve(N,D) << endl ;				
	}
	return 0 ;
}

I don’t like what i did …

28 Likes

This is brilliant :wink:
I was trying the same thing. I got tired and angry after n==7 and then I shut my editor and deleted the file ! :stuck_out_tongue:

4 Likes

:o still glitch free

Thank God its AC. For a moment there I thought you were asking to find a bug :stuck_out_tongue:

14 Likes

Tell me writing such code is just waste of time … right ??

1 Like

Salute . Nice way :stuck_out_tongue:

What would you do if N < 20? :DD

You are my master :wink: Anyway, congrats for Accepted.

May be a horrible code, but the approach is awesome. Hats off to your patience in coding this.

Hilarious!! :stuck_out_tongue: :stuck_out_tongue:

though you could have simply used recursion, and let the function do all the work! :stuck_out_tongue: My solution

2 Likes

10 dimensional array :stuck_out_tongue: .great patience…hats off for that…i don’t have enough to read :stuck_out_tongue: btw in real life, u also wudn’t have, to imagine 10th dimension :stuck_out_tongue:

great…:slight_smile: there was no condition on d… how u dared to think even that would get accepted it would have overflow for n=9 d =9 .:stuck_out_tongue:

You Sir , Deserve a Medal :stuck_out_tongue: , A True Hero :stuck_out_tongue:

5 stars for your code!

2 Likes

He He :smiley: good joke (better than RRJOKE )…

2 Likes

I joined the contest very lately almost after (1.5 hours). so at first attempt i clicked with this idea and started coding without giving it a second thought … although it is not good … i feel

1 Like

lolz it was not but i had to make it to get it accepted …

1 Like

LOL.

I left it in the middle . I should have done it only :stuck_out_tongue:

he he :smiley:

2 Likes

Thanks but it is not nice at all :smiley:

I did this because N <= 10 … and anyways thanks …