CodeForces 417E (构造)

题意:要求构造一个矩阵,使得每行每列的平方和仍为一个平方数

题解:cf的官方题解:

  • If n = 1, then take [1].
  • If n = 2, then take [3, 4].
  • If n is even, then take .
  • If n is odd, then take .
  • We are given two numbers n and m. Let array a corresponds to n, and array b corresponds to m. The we will build the answer array cas follows cij = ai·bj.
  • 构造真的好巧妙啊      0.0
  • 代码如下:
  • #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cstdlib>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <set>
    #include <list>
    #include <queue>
    #include <map>
    #include <stack>
    using namespace std;
    #define L(i) i<<1
    #define R(i) i<<1|1
    #define INF  0x3f3f3f3f
    #define pi acos(-1.0)
    #define eps 1e-3
    #define maxn 100010
    #define MOD 1000000007
    
    int n,m;
    
    void slove(int x,int a[])
    {
        if(x == 1)
        {
            a[0] = 1;
            return;
        }
        if(x == 2)
        {
            a[0] = 3;
            a[1] = 4;
            return;
        }
        if(x&1)
        {
            a[0] = 2;
            for(int i = 1; i < x-1; i++)
                a[i] = 1;
            a[x-1] = (x+1)/2;
        }
        else
        {
            for(int i = 0; i < x-1; i++)
                a[i] = 1;
            a[x-1] = (x-2)/2;
        }
    }
    int main()
    {
        int t,C = 1;
        while(scanf("%d%d",&n,&m) != EOF)
        {
            int a[110],b[110];
            slove(n,a);
            slove(m,b);
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                    printf("%d ",a[i]*b[j]);
                printf("\n");
            }
        }
        return 0;
    }
    

    还有一种比较暴力的解法:
  • #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cstdlib>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <set>
    #include <list>
    #include <queue>
    #include <map>
    #include <stack>
    using namespace std;
    #define L(i) i<<1
    #define R(i) i<<1|1
    #define INF  0x3f3f3f3f
    #define pi acos(-1.0)
    #define eps 1e-3
    #define maxn 100010
    #define MOD 1000000007
    
    int n,m;
    
    int ok(int a,int b,int c,int d)
    {
        int k1 = a * a * (m-1) + b * b;
        int k2 = a * a * (n-1) + c * c;
        int k3 = c * c * (m-1) + d * d;
        int k4 = b * b * (n-1) + d * d;
        int kk1 = sqrt(k1);
        int kk2 = sqrt(k2);
        int kk3 = sqrt(k3);
        int kk4 = sqrt(k4);
        if(kk1 * kk1 != k1)
            return 0;
        if(kk2 * kk2 != k2)
            return 0;
        if(kk3 * kk3 != k3)
            return 0;
        if(kk4 * kk4 != k4)
            return 0;
        return 1;
    }
    
    int main()
    {
        int t,C = 1;
        while(scanf("%d%d",&n,&m) != EOF)
        {
            int flag = 0,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++)
                        {
                            if(ok(a,b,c,d))
                                flag = 1;
                            if(flag)
                                break;
                        }
                        if(flag)
                            break;
                    }
                    if(flag)
                        break;
                }
                if(flag)
                    break;
            }
            for(int i = 0; i < n-1; i++)
            {
                for(int j = 0; j < m-1; j++)
                    printf("%d ",a);
                printf("%d\n",b);
            }
            for(int j = 0; j < m-1; j++)
                printf("%d ",c);
            printf("%d\n",d);
        }
        return 0;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值