QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#111533 | #6399. Classic: Classical Problem | Tobo | RE | 1193ms | 3892kb | C++20 | 5.7kb | 2023-06-07 15:17:35 | 2023-06-07 15:17:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using Poly = vector<int>;
struct Complex
{
double x = 0.0;
double y = 0.0;
Complex() {}
Complex(double x, double y) : x(x), y(y) {}
Complex operator+(const Complex &rhs) const { return Complex(x + rhs.x, y + rhs.y); }
Complex operator-(const Complex &rhs) const { return Complex(x - rhs.x, y - rhs.y); }
Complex operator*(const Complex &rhs) const { return Complex(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x); }
Complex conj() const { return Complex(x, -y); }
};
namespace FFT
{
constexpr double PI = 3.14159265358979323846;
int binary_upper_bound_in_bits(int w)
{
if (w <= 0)
return 1;
const int highbit = 31 - __builtin_clz(w);
return highbit + 1;
}
void fft(vector<Complex> &a, int L, int flag)
{
// flag == 1 : forward; flag == -1 : inverse.
// L should be a power of 2.
int k = binary_upper_bound_in_bits(L - 1);
vector<Complex> w(L);
vector<int> rev(L);
for (int i = 0; i < L; ++i)
{
w[i] = Complex(cos(2 * PI / L * i), flag * sin(2 * PI / L * i));
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (k - 1));
if (i < rev[i])
swap(a[i], a[rev[i]]);
}
for (int l = 2; l <= L; l <<= 1)
{
int m = l >> 1;
for (int i = 0; i < L; i += l)
{
for (int k = 0; k < m; ++k)
{
Complex t = w[L / l * k] * a[i + k + m];
a[i + k + m] = a[i + k] - t;
a[i + k] = a[i + k] + t;
}
}
}
if (flag == -1)
for (int i = 0; i < L; ++i)
a[i].x /= L;
}
}
namespace Polynomial
{
// basic operator
int norm(int n) { return 1 << (__lg(n - 1) + 1); }
// 任意模数多项式乘法
Poly multiply_p_any(Poly f, Poly g, int p)
{
int L = norm((int)f.size() - 1 + (int)g.size() - 1);
vector<Complex> a(L), b(L), c(L), d(L);
for (int i = 0; i < f.size(); ++i)
{
a[i].x = int(f[i]) >> 15;
b[i].x = int(f[i]) & 32767;
}
for (int i = 0; i < g.size(); ++i)
{
c[i].x = int(g[i]) >> 15;
d[i].x = int(g[i]) & 32767;
}
FFT::fft(a, L, 1);
FFT::fft(b, L, 1);
FFT::fft(c, L, 1);
FFT::fft(d, L, 1);
for (int i = 0; i < L; ++i)
{
Complex ta = a[i], tb = b[i], tc = c[i], td = d[i];
a[i] = ta * tc;
b[i] = ta * td + tb * tc;
c[i] = tb * td;
}
FFT::fft(a, L, -1);
FFT::fft(b, L, -1);
FFT::fft(c, L, -1);
Poly z((int)f.size() + (int)g.size() - 1);
for (int i = 0; i < z.size(); ++i)
{
z[i] = (((i64)(a[i].x + .5) % p << 30) +
((i64)(b[i].x + .5) % p << 15) +
(i64)(c[i].x + .5) % p) %
p;
}
return z;
}
}
using namespace Polynomial;
int qpow(int a, int n, int p)
{
int res = 1;
while (n)
{
if (n & 1)
res = (i64)res * a % p;
a = (i64)a * a % p;
n >>= 1;
}
return res;
}
vector<int> get_factors(int a)
{
vector<int> v;
for (int i = 1; i * i <= a; ++i)
if (a % i == 0)
{
v.push_back(i);
if (i * i != a)
v.push_back(a / i);
}
return v;
}
int get_minimum_primitive_root(int m)
{
int phiM = m - 1;
auto factors = get_factors(phiM);
for (int i = 1;; ++i)
{
if (gcd(i, m) != 1)
continue;
bool ok = true;
for (auto e : factors)
{
if (e != phiM && qpow(i, e, m) == 1)
{
ok = false;
break;
}
}
if (ok)
return i;
}
}
void solve()
{
int n, p;
cin >> n >> p;
vector<int> a(n);
for (int &i : a)
cin >> i;
sort(a.begin(), a.end());
if (a[0] != 0)
{
cout << 1 << ' ' << 1 << '\n'
<< 0 << '\n';
return;
}
int g = get_minimum_primitive_root(p);
vector<int> rev(p + 1);
for (int i = 0, c = 1; i < p - 1; i++)
{
rev[c] = i;
c = (i64)c * g % p;
}
a.erase(find(a.begin(), a.end(), 0));
Poly f(p - 1), ret(2 * p - 3);
for (auto &x : a)
f[rev[x]] = 1;
int l = 0, r = p - 1, ans = 0;
auto check = [&](int x)
{
Poly g(p - 1);
for (int i = 1; i <= x; i++)
g[rev[i]] = 1;
for (int i = 0; i < p - 1; i++)
g.emplace_back(g[i]);
reverse(g.begin(), g.end());
ret = multiply_p_any(f, g, 1e9);
return *max_element(ret.begin(), ret.end()) == x;
};
while (l <= r)
{
int mid = l + r >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
assert(check(ans));
vector<int> vec;
if (!ans)
vec.emplace_back(0);
for (int i = p - 2; i < 2 * p - 3; i++)
if (ret[i] == ans)
vec.emplace_back(qpow(g, 3 * p - 4 - i, p));
cout << vec.size() << ' ' << ans + 1 << '\n';
sort(vec.begin(), vec.end());
for (auto x : vec)
cout << x << ' ';
cout << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--)
solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3668kb
input:
3 2 3 0 2 3 5 2 3 4 3 5 0 2 3
output:
1 2 2 1 1 0 2 2 2 3
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 3708kb
input:
3 1 2 0 1 2 1 2 2 1 0
output:
2 1 0 1 1 1 0 1 2 1
result:
ok 6 lines
Test #3:
score: 0
Accepted
time: 1ms
memory: 3720kb
input:
7 1 3 0 1 3 1 2 3 1 0 1 3 2 2 3 2 0 2 3 1 2 3 3 0 1 2
output:
3 1 0 1 2 1 1 0 1 2 1 1 1 0 1 2 2 1 1 0 2 3 1 2
result:
ok 14 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
31 1 5 0 1 5 1 2 5 1 0 1 5 2 2 5 0 2 2 5 2 1 3 5 1 0 2 1 5 3 2 5 0 3 2 5 1 3 3 5 0 1 3 2 5 3 2 3 5 0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
result:
ok 254 lines
Test #6:
score: 0
Accepted
time: 45ms
memory: 3728kb
input:
2047 1 11 0 1 11 1 2 11 0 1 1 11 2 2 11 0 2 2 11 2 1 3 11 1 0 2 1 11 3 2 11 3 0 2 11 3 1 3 11 0 3 1 2 11 2 3 3 11 0 2 3 3 11 2 1 3 4 11 1 0 3 2 1 11 4 2 11 0 4 2 11 4 1 3 11 1 4 0 2 11 2 4 3 11 2 0 4 3 11 2 1 4 4 11 0 2 1 4 2 11 3 4 3 11 3 4 0 3 11 3 1 4 4 11 4 1 3 0 3 11 4 3 2 4 11 3 4 0 2 4 11 3 1...
output:
11 1 0 1 2 3 4 5 6 7 8 9 10 1 1 0 1 2 1 1 1 0 1 2 6 1 1 0 1 3 1 1 1 0 1 2 4 1 1 0 2 2 1 4 1 1 0 2 2 4 6 1 1 0 1 4 1 1 1 0 1 2 3 1 1 0 2 2 1 3 1 1 0 1 3 6 1 1 0 2 3 1 6 1 1 0 2 2 3 4 1 1 0 3 2 1 3 4 1 1 0 1 3 6 1 1 0 1 5 1 1 1 0 1 2 9 1 1 0 2 2 1 9 1 1 0 2 2 6 9 1 1 0 1 3 1 1 1 0 ...
result:
ok 4094 lines
Test #7:
score: 0
Accepted
time: 330ms
memory: 3712kb
input:
8191 1 13 0 1 13 1 2 13 0 1 1 13 2 2 13 2 0 2 13 2 1 3 13 2 1 0 1 13 3 2 13 0 3 2 13 1 3 3 13 1 0 3 2 13 2 3 3 13 2 0 3 3 13 3 1 2 4 13 1 3 2 0 1 13 4 2 13 4 0 2 13 4 1 3 13 0 1 4 2 13 2 4 3 13 0 2 4 3 13 2 4 1 4 13 0 1 4 2 2 13 3 4 3 13 3 0 4 3 13 4 1 3 4 13 4 1 0 3 3 13 4 2 3 4 13 3 2 0 4 4 13 3 4...
output:
13 1 0 1 2 3 4 5 6 7 8 9 10 11 12 1 1 0 1 2 1 1 1 0 1 2 7 1 1 0 1 3 1 1 1 0 1 2 9 1 1 0 2 2 1 9 1 1 0 2 2 7 9 1 1 0 1 4 1 1 1 0 1 2 10 1 1 0 2 2 1 10 1 1 0 1 3 7 1 1 0 2 3 1 7 1 1 0 2 2 9 10 1 1 0 3 2 1 9 10 1 1 0 1 3 7 1 1 0 1 5 1 1 1 0 1 2 8 1 1 0 2 2 1 8 1 1 0 2 2 7 8 1 1 0 1 3...
result:
ok 16382 lines
Test #8:
score: 0
Accepted
time: 501ms
memory: 3740kb
input:
11764 1 17 0 1 17 1 2 17 0 1 1 17 2 2 17 0 2 2 17 2 1 3 17 2 1 0 1 17 3 2 17 3 0 2 17 1 3 3 17 3 0 1 2 17 2 3 3 17 0 3 2 3 17 3 2 1 4 17 3 2 0 1 1 17 4 2 17 0 4 2 17 4 1 3 17 1 4 0 2 17 4 2 3 17 0 2 4 3 17 2 1 4 4 17 2 4 1 0 2 17 3 4 3 17 3 4 0 3 17 4 1 3 4 17 4 1 0 3 3 17 2 4 3 4 17 2 0 3 4 4 17 2 ...
output:
17 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 1 0 1 2 1 1 1 0 1 2 9 1 1 0 1 3 1 1 1 0 1 2 6 1 1 0 2 2 1 6 1 1 0 2 2 6 9 1 1 0 1 4 1 1 1 0 1 2 13 1 1 0 2 2 1 13 1 1 0 1 3 9 1 1 0 2 3 1 9 1 1 0 2 2 6 13 1 1 0 3 2 1 6 13 1 1 0 1 3 9 1 1 0 1 5 1 1 1 0 1 2 7 1 1 0 2 2 1 7 1 1 0 2 2 7 ...
result:
ok 23528 lines
Test #9:
score: 0
Accepted
time: 507ms
memory: 3728kb
input:
10526 1 19 0 1 19 1 2 19 0 1 1 19 2 2 19 2 0 2 19 2 1 3 19 0 2 1 1 19 3 2 19 0 3 2 19 3 1 3 19 1 0 3 2 19 3 2 3 19 2 0 3 3 19 1 3 2 4 19 1 2 0 3 1 19 4 2 19 0 4 2 19 4 1 3 19 0 1 4 2 19 4 2 3 19 4 0 2 3 19 2 4 1 4 19 4 2 0 1 2 19 4 3 3 19 0 3 4 3 19 1 3 4 4 19 3 4 0 1 3 19 4 3 2 4 19 0 4 3 2 4 19 1 ...
output:
19 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 1 0 1 2 1 1 1 0 1 2 10 1 1 0 1 3 1 1 1 0 1 2 13 1 1 0 2 2 1 13 1 1 0 2 2 10 13 1 1 0 1 4 1 1 1 0 1 2 5 1 1 0 2 2 1 5 1 1 0 1 3 10 1 1 0 2 3 1 10 1 1 0 2 2 5 13 1 1 0 3 2 1 5 13 1 1 0 1 3 10 1 1 0 1 5 1 1 1 0 1 2 4 1 1 0 2 2 1 4 ...
result:
ok 21052 lines
Test #10:
score: 0
Accepted
time: 1158ms
memory: 3892kb
input:
10000 9 83 60 35 63 59 58 81 0 13 71 1 5 0 1 7 0 2 61 39 0 2 7 0 4 1 7 0 2 19 0 14 1 2 0 3 23 14 10 0 3 11 0 5 2 1 5 0 2 7 0 4 2 3 0 2 2 3 0 1 1 13 0 5 47 10 2 34 15 0 1 2 0 1 17 0 1 11 0 2 7 1 0 1 7 0 2 23 0 17 2 13 10 0 2 7 1 0 6 31 19 13 6 29 0 24 4 23 0 5 18 17 2 19 0 5 1 7 0 2 13 7 0 3 17 0 6 1...
output:
2 3 38 76 5 1 0 1 2 3 4 7 1 0 1 2 3 4 5 6 1 2 36 1 2 2 7 1 0 1 2 3 4 5 6 1 2 15 2 1 0 1 2 2 5 7 2 2 6 9 5 1 0 1 2 3 4 1 2 2 1 2 2 1 2 1 13 1 0 1 2 3 4 5 6 7 8 9 10 11 12 4 2 18 22 24 33 2 1 0 1 17 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 11 1 0 1 2 3 4 5 6 7 8 9 10 1 2 1 7 1 0 ...
result:
ok 20000 lines
Test #11:
score: 0
Accepted
time: 1193ms
memory: 3764kb
input:
10000 10 11 6 8 0 1 7 3 2 9 4 5 21 23 21 19 10 17 11 20 6 3 2 18 9 16 13 14 4 12 8 7 1 0 15 7 7 6 2 4 0 1 5 3 17 19 4 6 5 11 17 15 0 10 3 8 12 18 13 7 9 2 14 15 17 11 15 8 2 12 3 1 13 16 6 7 0 9 10 5 2 2 0 1 2 2 0 1 33 37 11 20 9 16 19 32 33 31 3 29 36 10 8 25 22 17 5 6 15 28 14 0 4 27 18 12 34 21 3...
output:
1 10 1 1 19 4 6 7 1 2 3 4 5 6 1 14 14 1 14 12 1 2 1 1 2 1 2 21 21 24 1 8 8 6 7 1 2 3 4 5 6 10 11 1 2 3 4 5 6 7 8 9 10 1 22 22 1 6 1 1 10 11 1 26 22 2 3 1 2 1 14 16 22 23 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 4 2 6 1 18 3 6 7 1 2 3 4 5 6 1 2 1 1 8 7 1 28 15...
result:
ok 20000 lines
Test #12:
score: -100
Dangerous Syscalls
input:
10000 4 13 4 5 0 6 12 31 0 16 11 13 3 24 26 21 20 6 5 19 12 43 29 21 40 23 31 24 27 17 30 10 0 42 3 3 0 2 1 15 47 41 46 0 44 17 39 30 4 12 14 36 28 27 31 10 1 5 0 5 11 6 2 0 5 1 6 13 11 0 7 5 10 6 5 17 15 0 9 12 11 6 13 0 5 2 12 11 7 15 43 14 28 13 24 40 29 37 9 27 0 34 39 15 12 2 1 3 0 8 17 15 6 0 ...