codeforces 625C K-special Tables

C. K-special Tables
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.

Alis is among these collectors. Right now she wants to get one of k-special tables. In case you forget, the table n × n is calledk-special if the following three conditions are satisfied:

  • every integer from 1 to n2 appears in the table exactly once;
  • in each row numbers are situated in increasing order;
  • the sum of numbers in the k-th column is maximum possible.

Your goal is to help Alice and find at least one k-special table of size n × n. Both rows and columns are numbered from 1 to n, with rows numbered from top to bottom and columns numbered from left to right.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 500, 1 ≤ k ≤ n) — the size of the table Alice is looking for and the column that should have maximum possible sum.

Output

First print the sum of the integers in the k-th column of the required table.

Next n lines should contain the description of the table itself: first line should contains n elements of the first row, second line should contain n elements of the second row and so on.

If there are multiple suitable table, you are allowed to print any.

Examples
input
4 1
output
28
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
input
5 3
output
85
5 6 17 18 19
9 10 23 24 25
7 8 20 21 22
3 4 14 15 16
1 2 11 12 13

题意:给两个数n,k,要求输出一个n*n的矩阵,要求1、其中的数是1到n*n 要求2、输出的每一行严格递增,要求3、在满足前两个条件的情况下第k列的和最大

题解:我们只需要找出第k列的数的值即可得出整个矩阵的值,
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 510
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
int s[MAX];
int map[MAX][MAX];
using namespace std;
int main()
{
	int n,m,j,i,k;
	int a,b,c,d,o;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		//ans=n*n;
		memset(s,0,sizeof(s));
		memset(map,0,sizeof(map));
		a=n*(k-1);//从第k列往后最小的数的值 
		b=a+1;//比b大的数都是第k列之后的数 
		c=n-k+1;//k列之后还有c列(包括第k列)
		int sum=0;
		for(i=1;i<=n;i++)
		{
			s[i]=b+(i-1)*c;//第k列的值 
			sum+=s[i];
		}    
		d=b-1;//第k列前边的数中的最大数 
		o=d/n;
		int num=1;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=o;j++) 
				map[i][j]=num++;
		} 
		for(i=1;i<=n;i++)
		{
			int p=0;
			for(j=o+1;j<=n;j++)
				map[i][j]=s[i]+p++;
		}
		printf("%d\n",sum); 
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++) 
				printf("%d ",map[i][j]);
			printf("\n");
		} 
	}
	return 0;
} 

  

posted @ 2016-03-07 19:21  非我非非我  阅读(232)  评论(0编辑  收藏  举报