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;
}

  

codeforces 625C K-special Tables的更多相关文章

  1. 【77.78%】【codeforces 625C】K-special Tables

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. 【CodeForces 625C】K-special Tables

    题意 把1到n*n填在n*n的格子里.要求每一行都是递增的,使第k列的和最大. 分析 第k列前的格子1 2 .. 按要求填到满格,然后第k列及后面的格子,都从左到右填递增1的数. 第k列的和再加起来, ...

  3. Codeforces gym102152 K.Subarrays OR

    传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...

  4. codeforces 1133E K Balanced Teams

    题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的 ...

  5. Codeforces 1133E - K Balanced Teams - [DP]

    题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...

  6. codeforces 1269E K Integers (二分+树状数组)

    链接:https://codeforces.com/contest/1269/problem/E 题意:给一个序列P1,P2,P3,P4....Pi,每次可以交换两个相邻的元素,执行最小次数的交换移动 ...

  7. codeforces 1282B2. K for the Price of One (Hard Version) (dp)

    链接 https://codeforces.com/contest/1282/problem/B2 题意: 商店买东西,商店有n个物品,每个物品有自己的价格,商店有个优惠活动,当你买恰好k个东西时可以 ...

  8. Codeforces 544E K Balanced Teams (DP)

    题目: You are a coach at your local university. There are nn students under your supervision, the prog ...

  9. Codeforces Gym101502 K.Malek and Summer Semester

    K. Malek and Summer Semester   time limit per test 1.0 s memory limit per test 256 MB input standard ...

随机推荐

  1. 通俗易懂的讲解iphone视图控制器的生命周期

    IOS 视图控制器的生命周期非常非常重要,所以我有必要写个文章来和大家一起探讨问题. 今天在学习视图控制器的生命周期,也看了一下网上的一些资料,但总觉得不是那么好理解,首先我们来看一张图: 先粗略讲一 ...

  2. jenkins mac slave 设置

    1.在jenkins上增加节点, 2,在mac系统中将ssh的服务打开在偏好设置- 互联网与无线 - 共享中 3,使用mac root用户修改sshd-config的鉴权方式 首先获取到root用户登 ...

  3. 基于XMPP的即时通信系统的建立(四)— 组件介绍

    服务端 服务器 许可证 操作系统 是否支持任意客户端登录 备注 ejabberd 开源 Elang 是 支持虚拟主机和集群 Openfire Apache Java 是 Tigase GPLv3 Ja ...

  4. LA 5059 (找规律 SG函数) Playing With Stones

    题意: 有n堆石子,两个人轮流取,每次只能取一堆的至少一个至多一半石子,直到不能取为止. 判断先手是否必胜. 分析: 本题的关键就是求SG函数,可是直接分析又不太好分析,于是乎找规律. 经过一番“巧妙 ...

  5. Jqgrid入门-Jqgrid设置二级表头(六)

    上一章已经说明了Jqgrid结合Struts2+json展示数据,这一章主要探讨Jqgrid如何设置二级表头,类似这样的效果.如:           要实现这个功能,其实也不难.通过Jqgrid的s ...

  6. phonegap archive 报错 Cordova/CDVViewController.h' file not found

    在BuildSettings->Header Search Paths  增加如下路径,问题解决 $(OBJROOT)/UninstalledProducts/include "$(O ...

  7. Java [Leetcode 172]Factorial Trailing Zeroes

    题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  8. POJ:最长上升子序列

    Title: http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2  ...

  9. cimge 这次能够图片大小尺寸

    CImage imSrc,imDest;imSrc.Load(……);//读入原始图片imDest.Create(……)//创建新大小的图片imSrc.StretchBlt(imDest.GetDC( ...

  10. session服务器Nginx+Tomcat+Memcached集群Session共享

    cookie是怎样工作的? 例如,我们创立了一个名字为login的Cookie来包含访问者的信息,创立Cookie时,服务器端的Header如下面所示,这里假设访问者的注册名是“Michael Jor ...