QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#671048 | #9485. (mod N² + 1) | ucup-team4435 | RE | 0ms | 0kb | C++23 | 9.0kb | 2024-10-24 10:27:11 | 2024-10-24 10:27:15 |
answer
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
int Bit(int mask, int b) { return (mask >> b) & 1; }
template<class T>
bool ckmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool ckmax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}
// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
--l;
while (r - l > 1) {
T mid = l + (r - l) / 2;
if (predicat(mid)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
return FindFirstTrue(l, r, predicat) - 1;
}
const ll INF = 2e18;
const int INFi = 2e9;
const int N = 5e5 + 5;
const int LG = 20;
// MOD assumed to be prime
template<typename T>
int normalize(T value, int mod) {
if (value < -mod || value >= 2 * mod)
value %= mod;
if (value < 0)
value += mod;
if (value >= mod)
value -= mod;
return value;
}
template<typename T>
struct dynamic_modular_int {
using mint = dynamic_modular_int<T>;
int value;
dynamic_modular_int() : value(0) {}
dynamic_modular_int(const mint &x) : value(x.value) {}
template<typename U, typename V = std::enable_if_t<std::is_integral<U>::value>>
dynamic_modular_int(U value) : value(normalize(value, T::mod)) {}
template<typename U>
mint power(U degree) const {
mint prod = 1;
mint a = *this;
for (; degree > 0; degree >>= 1, a *= a)
if (degree & 1)
prod *= a;
return prod;
}
mint inv() const {
return power(T::mod - 2);
}
mint& operator=(const mint &x) {
value = x.value;
return *this;
}
mint& operator+=(const mint &x) {
value += x.value;
if (value >= T::mod)
value -= T::mod;
return *this;
}
mint& operator-=(const mint &x) {
value -= x.value;
if (value < 0)
value += T::mod;
return *this;
}
mint& operator*=(const mint &x) {
value = (long long) value * x.value % T::mod;
return *this;
}
mint& operator/=(const mint &x) {
return *this *= x.inv();
}
friend mint operator+(const mint &x, const mint &y) {
return mint(x) += y;
}
friend mint operator-(const mint &x, const mint &y) {
return mint(x) -= y;
}
friend mint operator*(const mint &x, const mint &y) {
return mint(x) *= y;
}
friend mint operator/(const mint &x, const mint &y) {
return mint(x) /= y;
}
mint& operator++() {
++value;
if (value == T::mod)
value = 0;
return *this;
}
mint& operator--() {
--value;
if (value == -1)
value = T::mod - 1;
return *this;
}
mint operator++(int) {
mint prev = *this;
value++;
if (value == T::mod)
value = 0;
return prev;
}
mint operator--(int) {
mint prev = *this;
value--;
if (value == -1)
value = T::mod - 1;
return prev;
}
mint operator-() const {
return mint(0) - *this;
}
bool operator==(const mint &x) const {
return value == x.value;
}
bool operator!=(const mint &x) const {
return value != x.value;
}
template<typename U>
explicit operator U() {
return value;
}
friend std::istream& operator>>(std::istream &in, mint &x) {
std::string s;
in >> s;
x = 0;
for (const auto c : s)
x = x * 10 + (c - '0');
return in;
}
friend std::ostream& operator<<(std::ostream &out, const mint &x) {
return out << x.value;
}
static int primitive_root() {
static int root = -1;
if (root != -1)
return root;
std::vector<int> primes;
int value = T::mod - 1;
for (int i = 2; i * i <= value; i++)
if (value % i == 0) {
primes.push_back(i);
while (value % i == 0)
value /= i;
}
if (value != 1)
primes.push_back(value);
for (int r = 2;; r++) {
bool ok = true;
for (auto p : primes)
if ((mint(r).power((T::mod - 1) / p)).value == 1) {
ok = false;
break;
}
if (ok)
return root = r;
}
}
};
struct dynamic_modular_int_mod {
static int mod;
};
int dynamic_modular_int_mod::mod = 998244353;
int &MOD = dynamic_modular_int_mod::mod;
using mint = dynamic_modular_int<dynamic_modular_int_mod>;
void solve(int n, int r) {
if (n == 3 && r == 0) {
cout << "Yes\n";
cout << "3 6 1\n";
cout << "2 5 4\n";
cout << "7 8 9\n";
return;
}
int who = n * n + 1;
for(int x = 2; x * x <= who; ++x) {
if (who % x == 0) {
cout << "No\n";
return;
}
}
if (!r) {
cout << "No\n";
return;
}
MOD = who;
vector<int> a(who, -1);
int md2 = n * n;
{
int root = mint::primitive_root();
mint cur = 1;
for (int i = 0; i < md2; ++i) {
a[cur.value] = i;
cur *= root;
}
}
vector<int> b(md2, 0);
for(int i = 1; i < who; ++i) b[a[i]] = i;
vector<vi> ans(n, vi(n));
int start = md2 - 1;
for(int i = 0; i < n; i += 2) {
for(int j = 0; j < n; j += 2) {
ans[i][j] = start;
start--;
}
}
for(int i = n - 2; i >= 0; i -= 2) {
for(int j = n - 1; j >= 0; j -= 2) {
ans[i][j] = start;
start--;
}
}
for(int i = 1; i < n; i += 2) {
for(int j = n - 2; j >= 0; j -= 2) {
ans[i][j] = start;
start--;
}
}
for(int i = n - 1; i >= 0; i -= 2) {
for(int j = 1; j < n; j += 2) {
ans[i][j] = start;
start--;
}
}
assert(start == -1);
// rep(i, n ){
// rep(j, n) {
// int x; cin >> x;
// cout << a[x] << ' ';
// }
// cout << '\n';
// }
// r * (n^2/4) == (n^2) * (n^2-1) / 2
// 13 * 4 == 8 * 15
// vi kek;
// sort(all(kek));
// assert(kek[0] == kek.back());
bool ok = false;
int rr = a[r];
// cerr << a[2] << '\n';
rep(add, n * n) {
int cur = (ans[0][0] + ans[1][1] + ans[0][1] + ans[1][0] + add * 4) % md2;
// cerr << (rr * (n * n / 4)) % (n * n) << ' ' << (cur * (n * n / 4)) % (n * n) << endl;
if (cur != rr) continue;
ok = true;
rep(i, n) {
rep(j, n) {
ans[i][j] = (ans[i][j] + add) % md2;
}
}
}
if (!ok) {
cout << "No\n";
return;
}
vector<vector<mint>> res(n, vector<mint>(n));
rep(i, n) {
rep(j, n) {
res[i][j] = b[ans[i][j]];
}
}
assert(mint(r) == res[0][0] * res[0][1] * res[1][1] * res[1][0]);
cout << "Yes\n";
rep(i, n) {
rep(j, n) {
cout << res[i][j] << ' ';
}
cout << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(12) << fixed;
int t = 1;
// for(int q = 2; q <= 50; ++q) {
// int cnt = (q / 2) * (q / 2);
// int pr = 5;
// int need = q * q + 1;
// while (pr < need && need % pr != 0) pr++;
// if (pr < need && (need - 1) / pr >= cnt) {
// cerr << q << endl;
// }
// }
cin >> t;
rep(i, t) {
int n, r; cin >> n >> r;
solve(n, r);
}
// for(int n = 2; n <= 50; ++n) {
// solve(n, 1);
// }
return 0;
}
详细
Test #1:
score: 0
Runtime Error
input:
3 2 4 3 3 4 2