#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 …