您的位置:首页 > 其它

codeforces E Square Table

2015-12-08 21:22 246 查看
Description

While resting on the ship after the “Russian Code Cup” a boy named Misha invented an interesting game. He promised to give his quadrocopter to whoever will be the first one to make a rectangular table of size n × m, consisting of positive integers such that the sum of the squares of numbers for each row and each column was also a square.

Since checking the correctness of the table manually is difficult, Misha asks you to make each number in the table to not exceed 108.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the size of the table.

Output

Print the table that meets the condition: n lines containing m integers, separated by spaces. If there are multiple possible answers, you are allowed to print anyone. It is guaranteed that there exists at least one correct answer.

Sample Input

Input

1 1

Output

1

Input

1 2

Output

3 4

题意:给出n和m,要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数。

构造

aaab

aaab

cccd

即可;

解释:设条件A:该列或该行的元素的平方总和是一个平方数

假设在4*4矩阵中,很容易构造满足条件A,全部相等即可也就是

aaaa

aaaa

aaaa

aaaa

但是如果n和m不同时,如4*3,如果还像上面那样构造就不能保证3个a与4个a同时满足条件A;

所以我们对于4*4格式的矩阵得写成

aaab

aaab

aaab

aaab

这时还没完,保证了aaab 4个满足条件A,但是列向,aaaa 4个并不能满足,所以还得在列向修改一下,

aaab

aaab

aaab

cccc

这时候,aaab,aaac都满足条件A,还有bbbc不能满足;那我们有得加一个变量d,变成:

aaab

aaab

aaab

cccd

这样的4个变量就能最少的满足条件A。

实现代码如下,暴力即可,,随机数也行。

[code]#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int pd[1000001];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        memset(pd,0,sizeof(pd));
        for(int i=1; i<=1000; i++)
            pd[i*i]=1;
        int a,b,c,d;
        for(a=1; a<=100; a++)
            for(b=1; b<=100; b++)
                for(c=1; c<=100; c++)
                    for(d=1; d<=100; d++)
                    {
                        int s1=(m-1)*a*a+b*b;
                        int s2=(n-1)*a*a+c*c;
                        int s3=(n-1)*b*b+d*d;
                        int s4=(m-1)*c*c+d*d;
                        if(pd[s1]&&pd[s2]&&pd[s3]&&pd[s4])
                        goto next;
                    }
        next:
        for(int i=1; i<n; i++)
        {
            for(int j=1; j<m; j++)
            {
                cout<<a<<" ";
            }
            cout<<b<<endl;
        }
        for(int i=1; i<m; i++)
        {
            cout<<c<<" ";
        }
        cout<<d<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: