CodeForces 710C Magic Odd Square

构造。

先只考虑用$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[ {\begin{array}{*{20}{c}}
1&0&1&0&1\\
0&0&1&0&0\\
1&1&1&1&1\\
0&0&1&0&0\\
1&0&1&0&1
\end{array}} \right]$。

输出的时候我们再将$1$至${n^2}$这些数替换$0$和$1$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}

int a[55][55],n,st;

void work(int r1,int c1,int r2,int c2,int f)
{
    for(int j=c1;j<=c2;j++) a[r2][j]=a[r1][j]=f, f=f^1;
    f=f^1;for(int i=r1;i<=r2;i++) a[i][c2]=a[i][c1]=f,f=f^1;
}

int main()
{
    scanf("%d",&n);
    if(n%4==1) st=1; else st=0;
    int x1=1,y1=1,x2=n,y2=n;
    while(1)
    {
        work(x1,y1,x2,y2,st);
        if(x1==x2&&y1==y2) break;
        x1++; y1++; x2--; y2--; st=st^1;
    }

    int k1=2,k2=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(a[i][j]%2==0) a[i][j]=k1, k1=k1+2;
            else a[i][j]=k2, k2=k2+2;
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-12 23:47:51

CodeForces 710C Magic Odd Square的相关文章

CodeForces - 710C Magic Odd Square(奇数和幻方构造)

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

【模拟】Codeforces 710C Magic Odd Square

题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. 题目思路: [模拟] 分为奇幻方.单偶幻方和双偶幻方三种构造. 具体分类可以查看百度.幻方的N种构造方法 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algori

codeforces 710C Magic Odd Square(构造或者n阶幻方)

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

CodeForces 710C Magic Odd Square (n阶奇幻方)

题意:给它定一个n,让你输出一个n*n的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数. 析:这个题可以用n阶奇幻方来解决,当然也可以不用,如果不懂,请看:http://www.cnblogs.com/dwtfukgv/articles/5797527.html 剩下的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <str

codeforces 710C C. Magic Odd Square(构造)

题目链接: 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

Codeforces 710 C. Magic Odd Square(构造)——Educational Codeforces Round 16

传送门 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 dif

CodeForces - 670D2 Magic Powder - 2 (二分&amp;模拟)

CodeForces - 670D2 Magic Powder - 2 Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The term of this problem is the same as the previous one, the only exception - increased restrictions. Input Th

CodeForces - 670D1 Magic Powder - 1 (模拟)

CodeForces - 670D1 Magic Powder - 1 Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description This problem is given in two versions that differ only by constraints. If you can solve this problem in large c

[递推+矩阵快速幂]Codeforces 1117D - Magic Gems

传送门: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的方案数,容易得到递推式: (总方案数 = 最右边的m