Agc027d - modulo matrix (structured black and white)

Keywords: C++

meaning of the title

Title Link

Construct a matrix of $n * n $, and require any two adjacent numbers $a,b $, so that $max (a,b)% min (a,b) \ not = 0$

Sol

My thoughts:

If $mod = 1 $, you can put 23 4 5 $\ dots $in the first row, and the same is true for the first column

For any position $I $, a number that must meet the requirements is a [I - 1] [J] * a [i] [J - 1] / uuu GCD (a [I - 1] [J], a [i] [J - 1]) + 1

But the last number is up to heaven...

The mark calculation is quite ingenious. First, the whole picture is colored in black and white, so the same color points do not affect each other.

Consider constructing a matrix of $mod = 1 $.

If the weight of the white dot is determined, the weight of the black dot should be $lcm$+1 of all adjacent white dots,

How to determine the weight of all white dots?

Consider using prime directly to assign a different prime number to each point on the positive and negative diagonals

Then the weight of any white point is the prime of the positive diagonal multiplied by the prime of the negative diagonal

The largest $a {ij} = 414556486388264 $, which meets the requirements

But why does the array have to be 1000 to pass???

#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int MAXN = 1e5 + 10;
int N;
int a[1001][1001], vis[MAXN], prime[MAXN], tot;
void GetPhi() {
    vis[1] = 1;
    for(int i = 2; i; i++) {
        if(!vis[i]) prime[++tot] = i;
        if(tot == 1000) break; 
        for(int j = 1; j <= tot && (i * prime[j] <= 10000); j++) {
            vis[i * prime[j]] = 1;
            if(!(i % prime[j])) break;
        }
    }
}
int lcm(int x, int y) {
    if(x == 0 || y == 0) return x + y;
    return x / __gcd(x, y) * y;
}
main() {
    GetPhi();
    cin >> N;
    if(N == 2) {
        printf("4 7\n23 10");
        return 0;
    }
    for(int i = 1; i <= N; i++) 
        for(int j = 1; j <= N; j++)
            if(!((i + j) & 1)) a[i][j] = prime[(i + j) / 2] * prime[N + (i - j) / 2 + (N + 1) / 2];
    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= N; j++)
            if(!a[i][j]) 
                a[i][j] = lcm(lcm(a[i - 1][j], a[i][j - 1]), lcm(a[i][j + 1], a[i + 1][j])) + 1;
    for(int i = 1; i <= N; i++, puts(""))
        for(int j = 1; j <= N; j++)
            cout << a[i][j] << " ";
    return 0;
}

Posted by Jackomo0815 on Mon, 23 Dec 2019 08:47:58 -0800