Skip to content

Instantly share code, notes, and snippets.

@Sharif1301
Created March 23, 2016 15:58
Show Gist options
  • Save Sharif1301/40d8c76d4ff1e9a1154e to your computer and use it in GitHub Desktop.
Save Sharif1301/40d8c76d4ff1e9a1154e to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
#define maxn 500
int main()
{
int mat[maxn+5][maxn+5], n, k, sum=0;
scanf("%d %d", &n, &k);
int row = n;
int value = n*n;
int fixed = (n-k)+1;
int leftvalue = value-(fixed*n);
// cout << " fixed = " << fixed << " leftvalue = " << leftvalue << endl;
// cout << " row " <<
while(row > 0){
for(int i=0, k=n; i<fixed; ++i, --k, --value){
mat[row][k] = value;
// cout << " mat[ " << row << " ][ " << k << " ] = " << mat[row][k] << " ";
}
// cout << endl;
for(int i=n-fixed; i>0; --i, --leftvalue){
mat[row][i] = leftvalue;
// cout << " mat[ " << row << " ][ " << i << " ] = " << mat[row][i] << " ";
}
// cout << endl;
--row;
}
for(int i=1; i<=n; ++i){
sum += mat[i][k];
}
printf("%d\n", sum);
for(int i=1; i<=n; ++i){
for(int j=1; j<=n; ++j)
printf("%d ", mat[i][j]);
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment