1 条题解

  • 0
    @ 2026-4-13 18:39:33

    首先由于有 11 的存在,所以这个 gcd⁡\operatorname{gcd} 一定都是 11。

    注意到相邻正整数互素,所以我们可以这样构造:

    n=4n=4 时:

    1 2 $ $
    $ 3 4 $
    $ $ 5 6
    8 $ $ 7
    

    n=5n=5 时:

    1 2 $ $ $
    $ 3 4 $ $
    $ $ 5 6 $
    $ $ $ 7 8
    10 $ $ $ 9
    

    等等。

    所以其实一定有解。

    #include <iostream>
    
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        int n;
        cin >> n;
    
        // 对于 i = 0 .. n-1
        // 第 i 行填入两个数字:
        //   2i+1 放在第 i 列 (0‑index)
        //   2i+2 放在第 (i+1) % n 列
        // 其余格子填 '$'
        for (int i = 0; i < n; ++i) {
            int a = 2 * i + 1;          // 第一个数
            int b = 2 * i + 2;          // 第二个数
            int col1 = i;               // 第一个数所在的列
            int col2 = (i + 1) % n;     // 第二个数所在的列
    
            for (int j = 0; j < n; ++j) {
                if (j == col1)
                    cout << a;
                else if (j == col2)
                    cout << b;
                else
                    cout << '$';
    
                if (j + 1 < n)          // 除最后一个格子外,后面加空格
                    cout << ' ';
            }
            cout << '\n';
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    13
    时间
    2000ms
    内存
    512MiB
    难度
    4
    标签
    递交数
    55
    已通过
    22
    上传者