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 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 test(s)
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 题意:1~n*n 排列在n*n的矩阵 要求每个数字出现一次
每行递增 第k列的和尽可能的大
题解:模拟 以第k列为分界线 左边为 1~n*(k-1)的排列
右边为n*(k-1)+1~n*n 的排列
简单模拟
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL __int64
using namespace std;
int n,k;
int main()
{
scanf("%d%d",&n,&k);
int exm;
int ans=0;
exm=n*(k-1);
int gg=1,ggg=exm+1;
for(int i=exm+1;i<=n*n;i=i+n-k+1)
ans+=i;
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
for(int j=1;j<k;j++)
{
if(j==1)
printf("%d",gg);
else
printf(" %d",gg);
gg++;
}
for(int j=k;j<=n;j++)
{
if(j==1)
printf("%d",ggg);
else
printf(" %d",ggg);
ggg++;
}
printf("\n");
}
return 0;
}

  

Codeforces Round #342 (Div. 2) C的更多相关文章

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

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

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

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

  5. Codeforces Round #342 (Div. 2) E. Frog Fights set 模拟

    E. Frog Fights 题目连接: http://www.codeforces.com/contest/625/problem/E Description stap Bender recentl ...

  6. Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)

    传送门 Description Vitya is studying in the third grade. During the last math lesson all the pupils wro ...

  7. Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)

    传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn ...

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

    传送门 Description A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Go ...

  9. Codeforces Round #342 (Div. 2) A. Guest From the Past(贪心)

    传送门 Description Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the detai ...

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

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

随机推荐

  1. 使用flask_limiter设定API配额

    前言 闲来无事,突然想到了以前做过的关于后台API安全方面的事,关于接口访问配额的设置,flask有没有很好的库支持呢?一找还真有!主要是对照了库的官方文档自己写了下dome,以供参考. # -*- ...

  2. Qt+opencv:读取、显示图像

    GitHub:点击下载完整代码 本文主要是使用Qt与opencv将图像进行显示在QT界面上. 程序运行后的界面如下所示: (由于只有打开图像之后,才能对图像进行翻转,所以程序设置为读取图像成功之后才能 ...

  3. Redis 数据结构服务器

    Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...

  4. 谜题 (Puzzle,ACM/ICPC World Finals 1993,UVa227)

    题目描述:算法竞赛入门经典习题3-5 题目思路:模拟题 #include <stdio.h> #include <string.h> #define maxn 55 char ...

  5. Java中定时器相关实现的介绍与对比之:Timer和TimerTask

    Timer和TimerTask JDK自带,具体的定时任务由TimerTask指定,定时任务的执行调度由Timer设定.Timer和TimerTask均在包java.util里实现. 本文基于java ...

  6. RNN概述-深度学习 -神经网络

    一 RNN概述    前面我们叙述了BP算法, CNN算法, 那么为什么还会有RNN呢?? 什么是RNN, 它到底有什么不同之处? RNN的主要应用领域有哪些呢?这些都是要讨论的问题. 1) BP算法 ...

  7. HADOOP docker(八):hadoop本地库

    前言2. Native Hadoop Library3. 使用本地库4. 本地库组件5. 支持的平台6. 下载7. 编译8. 运行时观察9. 检查本地库10. 如果共享本地库 小伙伴还记得每次启动hd ...

  8. PHP查询网站

    1.w3school http://www.w3school.com.cn/php/ 2.PHP官网 http://php.net/manual/en/funcref.php 3.国内的类似w3csh ...

  9. 父类与子类的转换as,is

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. 图像检测算法Halcon 10的使用

    安装完成HALCON之后,在VS项目中添加动态链接库配置项目,并修改此项目属性的包含目录.库目录和链接器.