QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#104265#6399. Classic: Classical ProblemMelacau#WA 15ms53840kbC++174.1kb2023-05-09 21:13:502023-05-09 21:13:55

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-09 21:13:55]
  • 评测
  • 测评结果:WA
  • 用时:15ms
  • 内存:53840kb
  • [2023-05-09 21:13:50]
  • 提交

answer

#include<bits/stdc++.h>
#define MN 800005
typedef long long ll;
typedef long double LD;
const LD PI = acos(-1);
struct C {
    LD r, i;
    C(LD r = 0, LD i = 0) : r(r), i(i) {}
    friend C operator+(const C &A, const C &B) {
        return C(A.r + B.r, A.i + B.i);
    }
    friend C operator-(const C &A, const C &B) {
        return C(A.r - B.r, A.i - B.i);
    }
    friend C operator*(const C &A, const C &B) {
        return C(A.r * B.r - A.i * B.i, A.r * B.i + A.i * B.r);
    }
};
int n, f[MN], g[MN], pn;
C a[MN], b[MN];
std::vector<int> aa;
int quipow(int a, int b, int p) {
    int ret = 1;
    for(; b; b >>= 1, a = (ll) a * a % p) if(b & 1) ret = (ll) ret * a % p;
    return ret;
}
std::vector<int> get_factor(int n) {
    std::vector<int> tmp;
    for(int i = 2; i * i <= n; i++) {
        if(n % i == 0) {
            tmp.push_back(i);
            while(n % i != 0) n /= i;
        }
    }
    if(n > 1) tmp.push_back(n);
    return tmp;
}
int find(int p) {
    auto factor = get_factor(p - 1);
    for(int i = 2; i <= p; i++) {
        bool flag = 0;
        for(int x : factor) {
            if(quipow(i, (p - 1) / x, p) == 1) {
                flag = 1;
                break;
            }
        }
        if(!flag) return i;
    }
    assert(0);
    return -1;
}
void FFT(C x[], int n, int p) {
    for(int i = 0, t = 0; i < n; ++i) {
        if(i > t) std::swap(x[i], x[t]);
        for(int j = n >> 1; (t ^= j) < j; j >>= 1);
    }
    for(int h = 2; h <= n; h <<= 1) {
        C wn(cos(p * 2 * PI / h), sin(p * 2 * PI / h));
        for(int i = 0; i < n; i += h) {
            C w(1, 0), u;
            for(int j = i, k = h >> 1; j < i + k; ++j) {
                u = x[j + k] * w;
                x[j + k] = x[j] - u;
                x[j] = x[j] + u;
                w = w * wn;
            }
        }
    }
    if(p == -1) {
        for(int i = 0; i < n; i++)
            x[i].r /= n;
    }
}
void conv(C a[], C b[], int n) {
    FFT(a, n, 1);
    FFT(b, n, 1);
    for(int i = 0; i < n; i++)
        a[i] = a[i] * b[i];
    FFT(a, n, -1);
}
int p;
bool check(int x) {
    int nn = p + p, N;
    for(N = 1; N <= nn; N <<= 1);
    // std::cerr << N << " " << x << "\n";
    for(int i = 0; i < N; i++) a[i] = b[i] = C(0, 0);
    for(int x : aa) if(x) a[p - 1 - f[x]] = C(1, 0);
    for(int i = 1; i < x; i++) b[f[i]] = C(1, 0);
    // for(int i = 0; i < N; i++) {
    //     std::cerr << a[i].r << " ";
    // }
    // std::cerr << "\n";
    // for(int i = 0; i < N; i++) {
    //     std::cerr << b[i].r << " ";
    // }
    // std::cerr << "\n";
    conv(a, b, N);
    // for(int i = 0; i < N; i++) {
    //     std::cerr << a[i].r << " ";
    // }
    // std::cerr << "\n";
    for(int i = 0; i < N; i++) {
        // if(i == 3) {
        //     std::cerr << (int)(a[i].r + 0.5) << " " << x - 1 << "\n";
        // }
        if((int)(a[i].r + 0.5) >= x - 1) return 1;
    }
    return 0;
}
void init() {
    pn = find(p);
    // std::cerr << pn << "\n";
    int u = pn;
    for(int i = 1; i < p; i++) {
        f[u] = i;
        g[i] = u;
        // std::cerr << u << " " << i << "\n";
        u = (ll)u * pn % p;
    }
}
void solve() {
    scanf("%d%d", &n, &p);
    aa.resize(n);
    bool flag = 0;
    for(int &x : aa) {
        scanf("%d", &x);
        if(!x) flag = 1;
    }
    if(!flag) {
        puts("1 1\n0");
        return;
    }
    init();
    int l = 1, r = p, ans = 0;
    while(l <= r) {
        int mid = l + r >> 1;
        if(check(mid)) {
            l = mid + 1;
            ans = mid;
        } else r = mid - 1;
    }
    check(ans);
    std::vector<int> tmp;
    for(int i = p; i < p - 1 + p; i++) {
        if(int(a[i].r + 0.5)  >= ans - 1) tmp.push_back(g[i - (p - 1)]);

    }
    std::sort(tmp.begin(), tmp.end());
    printf("%d %d\n", tmp.size(), ans);
    for (int i = 0; i < tmp.size(); ++i)
        printf("%d%c", tmp[i], " \n"[i + 1 == tmp.size()]);
}
int main() {
    int TT = 1;
    scanf("%d", &TT);
    while(TT--) {
        solve();
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 15ms
memory: 53840kb

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: 4ms
memory: 53828kb

input:

3
1 2
0
1 2
1
2 2
1 0

output:

1 1
2
1 1
0
0 2

result:

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