QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#165977 | #6399. Classic: Classical Problem | aesthetic | WA | 1ms | 5548kb | C++20 | 4.5kb | 2023-09-05 23:52:30 | 2023-09-05 23:52:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
typedef pair<int, int> pii;
const ll inf=LLONG_MAX;
const int maxn=1e6+10;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
// typedef long long ll;
// typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> C;
typedef vector<double> vd;
void fft(vector<C>& a) {
int n = sz(a), L = 31 - __builtin_clz(n);
static vector<complex<long double>> R(2, 1);
static vector<C> rt(2, 1); // (^ 10% faster if double)
for (static int k = 2; k < n; k *= 2) {
R.resize(n); rt.resize(n);
auto x = polar(1.0L, acos(-1.0L) / k);
rep(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2];
}
vi rev(n);
rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2;
rep(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int k = 1; k < n; k *= 2)
for (int i = 0; i < n; i += 2 * k) rep(j,0,k) {
// C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) /// include-line
auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k]; /// exclude-line
C z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]); /// exclude-line
a[i + j + k] = a[i + j] - z;
a[i + j] += z;
}
}
vd conv(const vd& a, const vd& b) {
if (a.empty() || b.empty()) return {};
vd res(sz(a) + sz(b) - 1);
int L = 32 - __builtin_clz(sz(res)), n = 1 << L;
vector<C> in(n), out(n);
copy(all(a), begin(in));
rep(i,0,sz(b)) in[i].imag(b[i]);
fft(in);
for (C& x : in) x *= x;
rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]);
fft(out);
rep(i,0,sz(res)) res[i] = imag(out[i]) / (4 * n);
return res;
}
ll pw(ll b, ll e, int md)
{
ll ans=1;
for(; e; b=b*b%md, e/=2)
if(e&1)
ans=ans*b%md;
return (ans+md)%md;
}
int primitive (int p) {
if(p==2)
return 1;
vector<int> fact;
int phi = p-1, n = phi;
for (int i=2; i*i<=n; ++i)
if (n % i == 0) {
fact.push_back (i);
while (n % i == 0)
n /= i;
}
if (n > 1)
fact.push_back (n);
for (int res=2; res<=p; ++res) {
bool ok = true;
for (size_t i=0; i<fact.size() && ok; ++i)
ok &= pw (res, phi / fact[i], p) != 1;
if (ok) return res;
}
return -1;
}
int n, p;
int e[maxn];
int a[maxn];
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios::sync_with_stdio(0);cin.tie(0);
int T; cin>> T;
while(T--)
{
cin>> n>> p;
int g=primitive(p);
int pot=1;
for(int i=0; i<p-1; i++)
{
a[pot]=i;
pot=pot*g%p;
}
bool zero=false;
int cur=0;
for(int i=1; i<=n; i++)
{
int si; cin>> si;
if(!si)
zero=true;
else
e[++cur]=a[si];
}
if(!zero)
{
cout<< "1 1\n0\n";
continue;
}
if(n==1)
{
cout<< p<< " "<< 1<< "\n";
for(int i=0; i<p; i++)
cout<< i<< " ";
cout<< "\n";
continue;
}
n=cur;
vd f2(p-1);
for(int i=1; i<=n; i++)
f2[(p-1-e[i])%(p-1)]=1;
auto check = [&](int m)
{
vd f(p-1);
for(int i=1; i<=m; i++)
f[a[i]]=1;
vd h=conv(f, f2);
bool exist=false;
for(int y=p-1; y<h.size(); y++)
{
h[y%(p-1)]+=h[y];
h[y]=0;
}
vector<int> ret;
for(int y=0; y<p-1; y++)
if(h[y]>=m)
ret.pb(y);
return ret;
};
int lo=1, hi=p-1;
while(hi>lo)
{
int mid=(lo+hi+1)/2;
if(check(mid).size())
lo=mid;
else
hi=mid-1;
}
vector<int> ans=check(lo);
cout<< ans.size()<< " "<< lo+1<< "\n";
sort(ans.begin(), ans.end());
for(int y: ans)
cout<< pw(g, y, p)<< " ";
cout<< "\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5476kb
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: 0ms
memory: 5488kb
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: 5548kb
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: -100
Wrong Answer
time: 1ms
memory: 5548kb
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 2 3 3 5 2 1 3 4 5 2 0 1 3 1 5 4 2 5 4 0 2 5 1 4 3 5 1 4 0 2 5 2 4 3 5 2 4 0 3 5 4 2 1 4 5 1 0 4 2 2 5 4 3 3 5 0 4 3 3 5 3 1 4 4 5 1 4 3 0 3 5 4 3 2 4 5 2 4 0 3 4 5 2 1 4 3 5 5 1 3 0 2 4
output:
5 1 0 1 2 3 4 1 1 0 1 2 1 1 1 0 1 2 3 1 1 0 1 3 1 1 1 0 1 2 2 1 1 0 1 3 2 1 1 0 2 2 2 3 1 1 0 1 4 1 1 1 0 1 2 4 1 1 0 2 2 1 4 1 1 0 1 3 3 1 1 0 1 4 3 1 1 0 1 3 4 1 1 0 1 4 2 1 1 0 1 4 4 1 1 0 4 5 1 2 4 3
result:
wrong answer 62nd lines differ - expected: '1 2 3 4', found: '1 2 4 3 '