You are not logged in. Please login at www.codechef.com to post your questions!

×

Most Horrible Code Ever Written By Me

29
2

Problem

#include <bits/stdc++.h>
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<D;r++){

                    DP[i][j][k][l][m][n][o][p][q][r] = min(min(min(min(min(min(min(min((i-1<0 ? inf : DP[i-1][j][k][l][m][n][o][p][q][r]),(j-1<0 ? inf : DP[i][j-1][k][l][m][n][o][p][q][r])),min((k-1<0 ? inf : DP[i][j][k-1][l][m][n][o][p][q][r]),(l-1<0 ? inf : DP[i][j][k][l-1][m][n][o][p][q][r]))),m-1<0 ? inf : DP[i][j][k][l][m-1][n][o][p][q][r]),n-1 < 0 ? inf : DP[i][j][k][l][m][n-1][o][p][q][r]),o-1<0 ? inf : DP[i][j][k][l][m][n][o-1][p][q][r]),p-1<0 ? inf : DP[i][j][k][l][m][n][o][p-1][q][r]),q-1<0 ? inf : DP[i][j][k][l][m][n][o][p][q-1][r]),r-1<0 ? inf : DP[i][j][k][l][m][n][o][p][q][r-1]) ;
                            if(DP[i][j][k][l][m][n][o][p][q][r] == inf)
                                DP[i][j][k][l][m][n][o][p][q][r] = 0 ;
                            DP[i][j][k][l][m][n][o][p][q][r] += (1LL*(i^j^k^l^m^n^o^p^q^r)*(i+j+k+l+m+n+o+p+q+r)) ;
                         }
                        }   
                        }
                    }
                }           
            }
        }
        }}} 
        return DP[D-1][D-1][D-1][D-1][D-1][D-1][D-1][D-1][D-1][D-1] ;
    }
}
int main(){

    cin >> T ;
    while(T--){
        cin >> N >> D ;
        cout << solve(N,D) << endl ;                
    }
    return 0 ;
}

I don't like what i did ..

asked 25 Jan '15, 14:17

ma5termind's gravatar image

3★ma5termind
1.7k11730
accept rate: 11%

2

5 stars for your code!

(25 Jan '15, 14:20) sampritipanda5★
2

He He :D good joke (better than RRJOKE )...

(25 Jan '15, 14:23) ma5termind3★

14

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

link

answered 25 Jan '15, 14:31

aalhadkulkarni's gravatar image

2★aalhadkulkarni
4422519
accept rate: 8%

2

he he :D ..

(25 Jan '15, 14:34) ma5termind3★

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

link

answered 25 Jan '15, 14:26

animesh_f's gravatar image

6★animesh_f ♦
8831422
accept rate: 9%

edited 25 Jan '15, 14:28

1

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

(25 Jan '15, 14:30) ma5termind3★

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

(25 Jan '15, 14:32) animesh_f ♦6★

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

link

answered 25 Jan '15, 17:22

gvaibhav21's gravatar image

7★gvaibhav21
947210
accept rate: 25%

thanx .. :D

(25 Jan '15, 18:26) ma5termind3★

@gvaibhav21 Can you please explain your code..??

(26 Jan '15, 23:31) damn_me3★

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

link

answered 25 Jan '15, 14:54

ma5termind's gravatar image

3★ma5termind
1.7k11730
accept rate: 11%

:o still glitch free

link

answered 25 Jan '15, 14:29

root8950's gravatar image

5★root8950
634
accept rate: 0%

1

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

(25 Jan '15, 14:31) ma5termind3★

Salute . Nice way :P

link

answered 25 Jan '15, 15:12

adityakhanna1999's gravatar image

1★adityakhanna1999
1315
accept rate: 14%

Thanks but it is not nice at all :D

(25 Jan '15, 15:16) ma5termind3★

What would you do if N < 20? :DD

You are my master ;) Anyway, congrats for Accepted.

link

answered 25 Jan '15, 15:18

pkacprzak's gravatar image

5★pkacprzak ♦♦
75485097
accept rate: 12%

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

(25 Jan '15, 15:24) ma5termind3★

I know, just kidding

(25 Jan '15, 23:44) pkacprzak ♦♦5★

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

link

answered 25 Jan '15, 16:41

dragonemperor's gravatar image

3★dragonemperor
89321135
accept rate: 10%

thanx dude ..

(25 Jan '15, 18:27) ma5termind3★

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

link

answered 25 Jan '15, 21:48

shubham201's gravatar image

3★shubham201
513
accept rate: 8%

edited 25 Jan '15, 21:52

thanx .. :D

(26 Jan '15, 00:24) ma5termind3★

great...:) 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 .:p

link

answered 26 Jan '15, 00:17

abhishekssj5's gravatar image

3★abhishekssj5
291
accept rate: 0%

1

it was given total number of cells will not exceed 10^6 ..

(26 Jan '15, 00:24) ma5termind3★

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

link

answered 30 Jan '15, 20:10

siddharths067's gravatar image

1★siddharths067
8027
accept rate: 4%

He He Good Joke :D

(31 Jan '15, 01:51) ma5termind3★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,688
×205
×31
×17

question asked: 25 Jan '15, 14:17

question was seen: 3,583 times

last updated: 31 Jan '15, 01:51