题意:要求构造一个矩阵,使得每行每列的平方和仍为一个平方数
题解: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; }