トピック
題名
回答
- A行列の最大要素は16であり、1e6未満の要素を直接見つけます。その係数には、Aのすべての要素が含まれているため、最初の2つの条件を直接16で満たすことができます。1e6を超えるため、lcm {1,2,3,4,5、…16}を探します。計算された最小公倍数は、条件を満たす。
- 3番目の条件では、転位法を使用して、中央の要素を元の配列の4乗に追加できます。
コード
#include<bits/stdc++.h>
using namespace std;
int main() {
int lcm = 1;
for (int i = 2; i <= 16; i++) {
int maxn = __gcd(i, lcm);
lcm = lcm * i / maxn;
}
int n, m, x;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> x;
if ((i + j) & 1) cout << lcm << " ";
else cout << lcm + pow(x, 4) << " ";
if (j==m) cout<<endl;
}
}
return 0;
}