首页 技术 正文
技术 2022年11月9日
0 收藏 480 点赞 2,709 浏览 2163 个字

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

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

【题目链接】: http://codeforces.com/contest/625/problem/C

【题解】

贪心.

尽量让小的数字先在前k-1列出现.剩下的数按顺序每一行从左往右安排就可以了.;

看样例也能看出来了.

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)typedef pair<int,int> pii;
typedef pair<LL,LL> pll;const int MAXN = 500+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);int n,k;
int a[MAXN][MAXN];int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(k);
int now = 0;
rep1(i,1,n)
rep1(j,1,k-1)
a[i][j] = ++now;
rep1(i,1,n)
rep1(j,k,n)
a[i][j] = ++now;
LL temp = 0;
rep1(i,1,n)
temp += a[i][k];
cout << temp << endl;
rep1(i,1,n)
rep1(j,1,n)
{
printf("%d",a[i][j]);
if (j==n)
puts("");
else
putchar(' ');
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902