传送门

Description

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 called k-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.

Sample Input

4 1
5 3

Sample Output

281 2 3 45 6 7 89 10 11 1213 14 15 16

855 6 17 18 199 10 23 24 257 8 20 21 223 4 14 15 161 2 11 12 13

思路

题意:

输出一个的n*n方阵,使得其每行为上升序列,方阵中1~n2的数字每个只能出现一次,方阵的第k列的和要最大

题解:

从样例出发,5*5的方阵中,使得第三列的和最大,倒过来看,为了满足上升序列,最后一行的倒三个数最大只能为23,倒二行的倒三个数只能为20,依此类推。剩下的未填数的格子,填入符合要求的数字即可

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	int x = 1,y = n*n - (n - k + 1) * n + 1;
	printf("%d\n",y*n+(n-k+1)*n*(n-1)/2);
	for (int i = 0;i < n;i++)
	{
		for (int u = 0;u < k - 1;u++)	u?printf(" %d",x++):printf("%d",x++);
		for (int u = k - 1;u < n;u++)	u?printf(" %d",y++):printf("%d",y++);
		printf("\n");
	}
	return 0;
}

  

Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)的更多相关文章

  1. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

  2. Codeforces Round #298 (Div. 2) A、B、C题

    题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...

  3. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  4. Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心

    D. Finals in arithmetic 题目连接: http://www.codeforces.com/contest/625/problem/D Description Vitya is s ...

  5. Codeforces Round #342 (Div. 2) C. K-special Tables 构造

    C. K-special Tables 题目连接: http://www.codeforces.com/contest/625/problem/C Description People do many ...

  6. Codeforces Round #342 (Div. 2)-B. War of the Corporations

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. Codeforces Round #342 (Div. 2)

    贪心 A - Guest From the Past 先买塑料和先买玻璃两者取最大值 #include <bits/stdc++.h> typedef long long ll; int ...

  8. Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心

    B. War of the Corporations 题目连接: http://www.codeforces.com/contest/625/problem/B Description A long ...

  9. Codeforces Round #342 (Div. 2) A - Guest From the Past 数学

    A. Guest From the Past 题目连接: http://www.codeforces.com/contest/625/problem/A Description Kolya Geras ...

随机推荐

  1. ajax参数设置略解

    通过ajax可以直接由页面访问到服务器.做到不刷新页面,就能刷新数据,为开发带来很大的便利. 1.ajax方式的参数及其功能: $.ajax({ type : "POST", // ...

  2. Linux命令学习总结:shutdown

    命令简介: 该命令可以安全关闭或者重新启动系统.你没有看错,shutdown命令不仅可以关闭系统.也可以重启Linux系统.   命令语法: /sbin/shutdown [-t sec] [-ark ...

  3. MS SQL 需要定期清理日志文件

    前言碎语 关于对SQL SERVER 日志文件管理方面了解不多的话,可以参考我的这篇博客文章“MS SQL 日志记录管理”,不过这篇文章只是介绍对SQL SERVER日志记录的深入认知了解,并没有提出 ...

  4. 如何查看Windows服务器运行了多长时间

    前言:有时候管理.维护Windows服务器需要定期重启服务器(为什么需要重启,你懂的),但是这个"定期"有时候会受很多因素影响,例如某台服务器忘了重启:某台服务器那个时间段业务繁忙 ...

  5. SQL SERVER CHAR ( integer_expression )各版本返回值差异的案例

    我们都知道CHAR(integer_expression)将ASCII代码转换为字符.当integer_expression介于 0 和 255 之间的整数.如果该整数表达式不在此范围内,将返回 NU ...

  6. Python学习笔记2-解析数据

    Import os; -- Python自带 print(os.getcwd()) -- 获得当前工作目录 os.chdir('/Users/longlong/Documents') -- 转换到/U ...

  7. PHP实现堆排序

    经验 工作了,面试我工作这家公司时被技术面打击得不行,因为自己的数据结构等基础学得实在太差,虽然原来是想做设计师的说...不过看在PHP写得还凑合的份上能来实习了,但还是决心恶补一下基础. 其实自己之 ...

  8. commons-math使用

    apache commons-math提供了一些常用的数学运算,包括初等数学.高等数学.线性代数.数理统计等方面的.猛击下面的链接查看其相关使用方法 Commons Math学习笔记 http://w ...

  9. 关于css的一些事情(1)

    什么情况下hidden不起作用? position设置成fixed,overflow的hidden不起作用. visibility和display 1.visibility: 规定了元素是否可见,即使 ...

  10. h3c防火墙的设置过程

    公司采购了一款h3c的防火墙,型号为F100-E-G,以前也设置过H3C的防火墙,不过这次还是设置还是有各种问题,所以把设置过程记录下来,方便以后查阅. 一.防火墙一般在0口都设置一个默认的IP地址1 ...