The 2024 ICPC Asia Shenyang Regional Contest B. Magical Palette
Problem - B - Codeforces
打表可以发现gcd(n,m)必须为1才有方案
打表代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
#define pii pair<int, int>
#define lowbit(x) (x & (-x))
int n, m;
int a[N], b[N];
bool st1[N], st2[N];
bool fd;
void dfs(int t1, int t2)
{if (fd)return;if (t2 == m + 1){map<int, int> mp;bool ok = 1;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){mp[a[i] * b[j] % (n * m)]++;if (mp[a[i] * b[j] % (n * m)] == 2)ok = 0;}}if (ok){fd = 1;for (int i = 1; i <= n; i++){cout << a[i] << ' ';}cout << endl;for (int i = 1; i <= m; i++){cout << b[i] << ' ';}cout << endl;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cout << a[i] * b[j] % (n * m) << ' ';}cout << endl;}cout << endl;}return;}if (t1 != n + 1){for (int i = 1; i <= n * m; i++){if (st1[i])continue;st1[i] = 1;a[t1] = i;dfs(t1 + 1, t2);st1[i] = 0;a[t1] = 0;}}else{for (int i = 1; i <= n * m; i++){if (st2[i])continue;st2[i] = 1;b[t2] = i;dfs(t1, t2 + 1);st2[i] = 0;b[t2] = 0;}}
}void solve()
{cin >> n >> m;dfs(1, 1);
}signed main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int t = 1;cin >> t;while (t--)solve();
}
然后写一个check函数套一下就能套出来这么一个正确的构造:
a[i] = (i * m + 1) % (n * m);
b[i] = (i * n + 1) % (n * m);
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
#define pii pair<int, int>
#define lowbit(x) (x & (-x))
int a[N];
int b[N];
int n, m;
bool check()
{map<int, int> mp;bool ok = 1;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){mp[a[i] * b[j] % (n * m)]++;if (mp[a[i] * b[j] % (n * m)] == 2)ok = 0;}}for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cout << a[i] * b[j] % (n * m) << ' ';}cout << endl;}return ok;
}
void solve()
{cin >> n >> m;if (gcd(n, m) != 1){cout << "No" << endl;return;}for (int i = 1; i <= n; i++)a[i] = (i * m + 1) % (n * m);for (int i = 1; i <= m; i++)b[i] = (i * n + 1) % (n * m);// cout << check() << endl;cout << "Yes" << endl;for (int i = 1; i <= n; i++)cout << a[i] << ' ';cout << endl;for (int i = 1; i <= m; i++)cout << b[i] << ' ';cout << endl;
}signed main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int t = 1;cin >> t;while (t--)solve();
}