C. Magic Odd Square time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.…
题意:给它定一个n,让你输出一个n*n的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数. 析:这个题可以用n阶奇幻方来解决,当然也可以不用,如果不懂,请看:http://www.cnblogs.com/dwtfukgv/articles/5797527.html 剩下的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <str…
Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n (1 ≤ n ≤ 49). Output Print n lines with n integers. All the integers…
Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n (1 ≤ n ≤ 49). Output Print n lines with n integers. All the integers should be differ…
题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. 题目思路: [模拟] 分为奇幻方.单偶幻方和双偶幻方三种构造. 具体分类可以查看百度.幻方的N种构造方法 // //by coolxxx //#include<bits/stdc++.h> #include<iostream> #include<algorithm> #i…
构造. 先只考虑用$0$和$1$构造矩阵. $n=1$,$\left[ 1 \right]$. $n=3$,(在$n=1$的基础上,最外一圈依次标上$0$,$1$,$0$,$1$......) $\left[ {\begin{array}{*{20}{c}}0&1&0\\1&1&1\\0&1&0\end{array}} \right]$. $n=5$,(在$n=3$的基础上,最外一圈依次标上$1$,$0$,$1$,$0$......) $\left[ {\b…
题目链接: C. Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n (1 ≤ n ≤ 49). Output Print n lines with n integers. All the…
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. 输入 The only line contains odd integer n (1 ≤ n ≤ 49). 输出 Print n lines…
1 奇数阶幻方构造法 (1) 将1放在第一行中间一列; (2) 从2开始直到n×n止各数依次按下列规则存放:按 45°方向行走,向右上,即每一个数存放的行比前一个数的行数减1,列数加1 (3) 如果行列范围超出矩阵范围,则回绕.例如1在第1行,则2应放在最下一行,列数同样加1; (4) 如果按上面规则确定的位置上已有数,或上一个数是第1行第n列时,则把下一个数放在上一个数的下面. #include<iostream> #include<algorithm> #include<…
Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n (1 ≤ n ≤ 49). Output Print n lines with n integers. All the integers should be differ…
Content 构造出一个 \(n\times n\) 的矩阵,使得这个矩阵由 \(1\sim n^2\) 这些数字组成,并且这个矩阵的每行,每列,以及对角线的和都为奇数. 数据范围:\(1\leqslant n\leqslant 49\).\(n\) 为奇数. Solution 这题其实就是 \(\texttt{NOIP2015}\) 提高组的 \(\texttt{D1T1}\),因为--都是奇数幻方,都是要求每行,每列,以及对角线的和都相等.只不过这道题目要求和都为奇数罢了. 但是,只要你利…
C. Magic Odd Square time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.…
一.设计的流程图如下所示 二.Java 语言的代码实现 package MagicSquare; //奇数幻方的实现 public class Magic_Odd { //n 为幻方的阶数 public static int[][] magicOdd(int n) { //构造一个(n+2)*(n+2)阶的方阵 int[][] square = new int[n + 1][n + 1]; int i = 0; int j = (n + 1) / 2; //从第一行的中间那个数字(是1)开始填幻…
魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说夏禹治水时,河南洛阳附近的大河里浮出了一只乌龟,背上有一个很奇怪的图形,古人认为是一种祥瑞,预示着洪水将被夏禹王彻底制服.后人称之为"洛书"或"河图",又叫河洛图. 南宋数学家杨辉,在他著的<续古摘奇算法>里介绍了这种方法:只要将九个自然数按照从小到大的递增次序斜排,然后把上.下两数对调,左.右两数也对调:最后再把中部四数各向外面挺出,幻方就出现了. (摘自<趣味数学辞典>) 在西方…
一.关于单偶数阶幻方和双偶数阶幻方 (一)单偶数阶幻方(即当n=4k+2时) 任何4k+2 阶幻方都可由2k+1阶幻方与2×2方块复合而成,6是此类型的最小阶. 以6阶为例,可由3阶幻方与由0,1,2,3组成的2×2的小方块拼成一个6×6的大方块复合而成. 其中,3阶幻方(洛书)如下图1所示, (图1) 800x600 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Sty…
这题就是一个sb题,本来很水,硬是说得很含混.奇数阶幻方构造其实有好多方法,这题既不special judge,也不说清楚,以为这样能把水题变成难题似的,简直想骂出题人. /* * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #i…
题目链接 Codeforces Round #448 C. Square Subsets 题解 质因数 *质因数 = 平方数,问题转化成求异或方程组解的个数 求出答案就是\(2^{自由元-1}\) ,高消求一下矩阵的秩,完了 或者 由于数很小, 我们只需要对于每个数的质因数装压 对这组数求线性基,n - 线性基中的数就是自由元个数 代码 #include<bits/stdc++.h> using namespace std; inline int read() { int x = 0,f =…
前序 最近在学习一些经典的算法,搞得头昏脑涨,就想换换脑子.在家里的旧书堆里面乱翻,无意中将一本具有十多年历史的小学数学奥林匹克竞赛的书发掘了出来,能放到现在挺不容易的,就拿起来随便翻翻.看了看目录,一个个熟悉的问题又一次的展现在了我的面前,看着看着就翻到了n阶幻方这块(其实那时候我们不这么叫).记得当时学这个问题的时候就感觉特别有意思,现在看看也是如此,于是乎便诞生了本片文章.        本文用较大的篇幅先介绍了n阶幻方,因为这个问题挺有意思.但是本文的重点却在第二节---由n阶幻方引发的…
转载自:http://blog.csdn.net/fengchaokobe/article/details/7437767 目录        第一节 n阶幻方问题       第二节 由n阶幻方引发的思考 正文 第一节 n阶幻方问题 所谓n阶幻方问题,俗称“横竖斜相加和相等”(我们当时就是这么叫的).用术语说就是:在一个N行N列的方格表中,有1,2,3......N*N-1,N*N这N*N个整数,且其对角线.横行.纵行的数字和都相等.       好了,在具体详解该问题之前,我们先看个例子,熟…
由一道数学题的联想然后根据网上的做法瞎jb乱打了一下,居然对了代码精心附上了注释,有兴趣的童鞋可以看一看..不说了,上代码!(自认为结构很清晰易懂) 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192…
算法原理请参考:https://www.zhihu.com/question/23531676 先定义一些通用的函数,比如创建空幻方,删除幻方,打印幻方. 创建幻方 int **NewMagicS(int n) { int **magic = new int*[n]; for (int k = 0; k < n; k++) magic[k] = new int[n]; return magic; } 删除幻方 void DeleteMagicS(int **magic, int n) { for…
题目链接:http://codeforces.com/problemset/problem/710/C 题目大意:输入一个奇数n,则生成n*n矩阵,要求矩阵的行.列还有斜着,所有元素之和为奇数. 解题思路:要符合行.列还有斜着,所有元素之和为奇数,则保证行.列或者斜着,有奇数个奇数 和 偶数个偶数即可. 32 4 8 5 2 4 6 8 10 3 5 7 12 9 11 13 15 17 14 19 21 23 16 18 20 22 24 7 2 4 6 8 10 12 14 16 3 5 7…
解决方法:1.第一个元素放在第一行中间一列 2.下一个元素存放在当前元素的上一行.下一列. 3.如果上一行.下一列已经有内容,则下一个元素的存放位置为当前列的下一行. 在找上一行.下一行或者下一列的时候,必须把这个矩阵看成是回绕的. 代码中,为了判断,当前位置是否有元素,我引入与魔方矩阵规模相同的另一个矩阵,如果魔方矩阵一个位置不为空,相应另一个矩阵那个位置为1,否则为0. /*程序的版权和版本声明部分: **Copyright(c) 2016,电子科技大学本科生 **All rights re…
D1. Magic Powder - 1 time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: standard output This problem is given in two versions that differ only by constraints. If you can solve this problem in large constr…
传送门:Educational Codeforces Round 60 – D   题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem,现在需要N个gem,可以选择一定的magic gem,指定每一个分裂或不分裂,问一共有多少种方案 两种分裂方案不同当且仅当magic gem的数量不同,或者分裂的magic gem的索引不同. 思路: 1.首先从dp的角度出发 设F(i)为最终需要i个gem的方案数,容易得到递推式: (总方案数 = 最右边…
http://codeforces.com/problemset/problem/716/C codeforces716C. Plus and Square Root 这个题就是推,会推出来规律,发现和等级有关系,等级的平方然后减去数字啥的,忘记了,推的草稿纸找不到了... 推出来就是类似a+b的题目. 代码: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath&…
F - Expected Square Beauty 思路:https://codeforces.com/blog/entry/68111 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define…
A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, ' + ' (plus) an…
D2. Magic Powder - 2 The term of this problem is the same as the previous one, the only exception — increased restrictions. Input The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the…
题目链接 给一个n*n的矩阵, 问是否对角线上的元素全都为0, a[i][j]是否等于a[j][i], a[i][j]是否小于等于max(a[i][k], a[j][k]), k为任意值. 前两个都好搞, 我们来看第三个. 第三个的意思是, 对于a[i][j], 它小于等于第i行和第j行每一列的两个元素的最大值. 我们将矩阵中的每一个元素的值以及x, y坐标都加到一个数组里面, 然后从小到大排序. 从0到n-1枚举每一个i, 如果一个元素pos比i小, 那么就将b[pos的x][pos的y]这个…