QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#559149 | #6195. Linear Congruential Generator Problem | pandapythoner# | WA | 53ms | 18604kb | C++23 | 2.7kb | 2024-09-11 20:31:01 | 2024-09-11 20:31:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using lll = __int128_t;
using ll = long long;
using ld = long double;
#define len(a) ((int)(a).size())
#define rep(i, n) for(int i = 0; i < (n); i += 1)
mt19937 rnd(234);
int n;
ll a, b, p;
vector<int> perm;
ll inva, invb;
ll bin_pow(ll x, ll n) {
ll rs = 1;
for (ll i = 1; i <= n; i *= 2, x = lll(x) * x % p)
if (n & i) rs = lll(rs) * x % p;
return rs;
}
ll inv(ll x) {
return bin_pow(x, p - 2);
}
ll forward(ll x) {
return (lll(x) * a + b) % p;
}
ll backward(ll x) {
return (lll(x + invb) * inva) % p;
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n;
perm.resize(n + 1);
for (int i = 1; i <= n; i += 1) {
cin >> perm[i];
}
cin >> a >> b >> p;
inva = inv(a);
invb = (p - b) % p;
ll t = -1;
for (int i = 1; i <= n; i += 1) {
if (perm[i] == n) {
t = i - 1;
swap(perm[i], perm[n]);
break;
}
}
ll s = -1;
for (int i = 1; i <= n - 1; i += 1) {
if (perm[i] == n - 1) {
s = i - 1;
swap(perm[i], perm[n - 1]);
break;
}
}
assert(t != -1 and s != -1);
ll f = 1e6;
vector<vector<ll>> mp(n);
rep(h, f) {
ll val = (lll(a) * s + b) % p;
val = (val + lll(h) * a % p * f % p * (n - 1)) % p;
mp[val % n].push_back(h);
}
auto check = [&](ll x) -> ll {
if (x % (n - 1) != s) return -1;
if (forward(x) % n != t) return -1;
vector<pair<int, int>> swaps;
x = backward(x);
bool ok = true;
for (int i = n - 2; i >= 1; i -= 1) {
int t = x % i + 1;
if (perm[t] != i) {
ok = false;
break;
}
swap(perm[t], perm[i]);
swaps.push_back(make_pair(t, i));
x = backward(x);
}
reverse(swaps.begin(), swaps.end());
for (auto [i, j] : swaps) swap(perm[i], perm[j]);
if (!ok) {
return -1;
}
return x;
};
rep(l, f) {
ll val = lll(l) * a % p * (n - 1) % p;
val %= n;
ll first = (t + n - val) % n;
ll second = (t + n - (val + p) % n) % n;
for (auto z : { first, second }) {
for (auto h : mp[z]) {
ll x = (s + lll(l + f * h) * (n - 1)) % p;
if (check(x)) {
cout << x << "\n";
return 0;
}
}
}
}
assert(0);
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 53ms
memory: 18604kb
input:
100000 38898 35776 38192 80605 84959 84530 62023 71929 48220 44197 86087 74579 85822 73930 56932 8586 14771 97888 19725 83418 68537 51209 79063 941 18772 55012 95724 21509 54841 4758 53698 1878 15489 65235 63470 28437 47179 2603 9808 54485 34353 93975 75059 77384 24776 71739 15230 90871 29323 92012 ...
output:
690010765
result:
wrong answer 1st numbers differ - expected: '681779867', found: '690010765'