codeforces #342 div 2 C. K-special Tables

http://codeforces.com/contest/625/problem/C 题意:构造一个矩阵。。满足三个条件。。。 思路:简单构造。。。看代码把。。。。

/* ***********************************************
Author :111qqz
Created Time :2016年02月07日 星期日 17时49分15秒
File Name :code/cf/#342/C.cpp
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair

using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=5E2+7;

int n;
int k;
int ans[N][N];
int main()
{
	#ifndef  ONLINE_JUDGE 
	freopen("code/in.txt","r",stdin);
  #endif

	cin>>n>>k;
	int cnt = 0;
	ms(ans,0);
	for ( int i = 1 ; i <= n ; i++)
	    for ( int j = 1 ; j <= k-1 ; j++)
	    {
		cnt++;
		ans[i][j] = cnt;
	    }
	for ( int i = 1 ; i <= n  ;i++)
	    for ( int j = k ; j <=n  ;j++)
	    {
		cnt++;
		ans[i][j]=cnt;
	    }
	int sum = 0 ;
//	cout<<"k:"<<k<<endl;
	for ( int i = 1 ; i <= n ; i++)
	{
	    sum +=ans[i][k];
	    
	   // cout<<"sum:"<<sum<<endl;
	}

	cout<<sum<<endl;
	for ( int i = 1 ; i <= n ; i++)
	{
	    for ( int j = 1 ; j <= n-1 ; j++)
	    {
		printf("%d ",ans[i][j]);
	    }
	    printf("%d\n",ans[i][n]);
	}

  #ifndef ONLINE_JUDGE  
  fclose(stdin);
  #endif
    return 0;
}