QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#165973#6399. Classic: Classical ProblemaestheticWA 1ms5500kbC++204.3kb2023-09-05 23:48:452023-09-05 23:48:46

Judging History

你现在查看的是最新测评结果

  • [2023-09-05 23:48:46]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5500kb
  • [2023-09-05 23:48:45]
  • 提交

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];
        }
        n=cur;
        if(!zero)
        {
            cout<< "1 1\n0\n";
            continue;
        }
        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";
        for(int y: ans)
            cout<< pw(g, y, p)<< " ";
        cout<< "\n";
    }
 
    return 0;
}   

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5500kb

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: -100
Wrong Answer
time: 1ms
memory: 5496kb

input:

3
1 2
0
1 2
1
2 2
1 0

output:

0 2

1 1
0
1 2
1 

result:

wrong answer 1st lines differ - expected: '2 1', found: '0 2'