codeforces 625C K-special Tables
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.
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.
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.
4 1
28
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
5 3
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列的数的值即可得出整个矩阵的值,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #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; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络