QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#182767#5743. Palindromic Polynomialucup-team1209#WA 1ms3484kbC++204.9kb2023-09-18 15:12:472023-09-18 15:12:47

Judging History

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

  • [2023-09-18 15:12:47]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3484kb
  • [2023-09-18 15:12:47]
  • 提交

answer

#include<bits/stdc++.h>
const int mod = 1e9 + 9;
using namespace std;
#define pb push_back
#define cs const
using std::cin;
using std::cout;
using u64 = unsigned long long;
using ll = long long;

void Add(int &x, int y) {
    if((x += y) >= mod) x -= mod; 
}
int add(int x, int y) {
    Add(x, y);
    return x;
}
void Dec(int &x, int y) {
    if((x -= y) < 0) x += mod; 
}
int dec(int x, int y) {
    Dec(x, y);
    return x;
}
int mul(int x, int y) {
    return 1ll * x * y % mod; 
}
void Mul(int &x, int y) {
    x = 1ll * x * y % mod; 
}
int ksm(int a, int b) {
    int c = 1; 
    for(; b; b >>= 1, Mul(a, a))
    if(b & 1) Mul(c, a);
    return c; 
}
const int N = 1e3 + 5;
int T, n; 
int a[N][N];

void fuckyou(int d, int mx, vector <int> x, vector <int> y) {
    for(int i = 0; i < n; i++) {
        for(int j = 1; j < d; j++) {
            int coef = j + j == mx ? ksm(x[i], j) : add(ksm(x[i], j), ksm(x[i], mx - j));
            a[i][j] = coef;
        }
        a[i][0] = 0; 
        a[i][d] = add(ksm(x[i], mx) + 1, y[i]);
    }

    // cout << d << ' ' << n <<  endl;

    for(int i = 0; i < d; i++) {
        int k = i; 
        while(a[k][i] == 0 && k < n) ++ k;
        if(k >= n) {
            continue; 
        }
        swap(a[i], a[k]);
        int iv = ksm(a[i][i], mod - 2);
        for(int j = 0; j < n; j++) 
        if(j != i && a[j][i]) {
            int c = mul(a[j][i], iv);
            for(int k = i; k <= d; k++)
                Dec(a[j][k], mul(a[i][k], c));
        }
    }
}
void Main() {
    cin >> n; 
    vector <int> x(n), y(n);
    for(int & a : x) cin >> a; 
    for(int & a : y) cin >> a; 

    vector <bool> ok(1e4 + 1, 1);
    for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++) 
    if(mul(x[i], x[j]) == 1) {
        // cout << "FF " << x[i] << ' ' << x[j] << endl;
        if(y[j] == 0) {
            if(y[i] != 0) {
                cout << -1 << '\n';
                return; 
            }
            continue; 
        }
        int c = mul(y[i], ksm(y[j], mod - 2));
        // x_i ^ d = c 
        // c != 0
        int mult = 1; 
        for(int t = 0; t <= 1e4; t++) {
            if(mult != c) ok[t] = 0; 
            Mul(mult, x[i]);
        }
    }
    int mx = -1;
    for(int i = 0; i <= 1e4; i++) {
        if(ok[i]) mx = max(mx, i);
    }
    if(mx > 2 * (n + 5)) {
        for(int i = 2 * (n + 5); i <= 1e4; i++)  
            if(ok[i]) { mx = i; break; }
    }
    // for(int i = 0;i <= 10; i++) cout << ok[i] << ' '; cout <<endl;
    // cout << mx << endl; 
    if(mx == -1) {
        cout << -1 << '\n';
        return;
    }
    int d = min(mx / 2 + 1, n);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            int coef = j + j == mx ? ksm(x[i], j) : add(ksm(x[i], j), ksm(x[i], mx - j));
            a[i][j] = coef; 
        }
        a[i][d] = y[i];
    }

    // cout << d << ' ' << n <<  endl;

    for(int i = 0; i < d; i++) {
        int k = i; 
        while(a[k][i] == 0 && k < n) ++ k;
        if(k >= n) {
            continue; 
        }
        swap(a[i], a[k]);
        int iv = ksm(a[i][i], mod - 2);
        for(int j = 0; j < n; j++) 
        if(j != i && a[j][i]) {
            int c = mul(a[j][i], iv);
            for(int k = i; k <= d; k++)
                Dec(a[j][k], mul(a[i][k], c));
        }
    }
    for(int i = d; i < n; i++) 
    if(a[i][d]) {
        cout << -1 << '\n';
        return;
    }
    vector <int> ans (mx + 1);
    for(int i = 0; i < d; i++){
        int c = 0; 
        if(a[i][i] == 0) {
            if(a[i][d]) {
                cout << -1 << '\n';
                return;
            }
        }
        else {
            c = mul(a[i][d], ksm(a[i][i], mod - 2));
        }
        // cout << i << ' ' << c << endl;
        ans[i] = ans[mx - i] = c; 
    }
    if(ans[0] == 0) {
        fuckyou(d, mx, x, y);
        ans[0] = ans.back() = 1; 
        for(int i = 1; i < d; i++){
            int c = 0; 
            if(a[i][i] == 0) {
                if(a[i][d]) {
                    cout << -1 << '\n';
                    return;
                }
            }
            else {
                c = mul(a[i][d], ksm(a[i][i], mod - 2));
            }
            // cout << i << ' ' << c << endl;
            ans[i] = ans[mx - i] = c; 
        }
    }
    for(int i = 0; i <n; i++) {
        int c = 1; 
        int f = 0; 
        for(int w : ans) {
            Add(f, mul(c, w));
            Mul(c, x[i]);
        }
        if(f != y[i]) {
            cout << -1 << '\n';
            return;
        }
    }
    cout << ans.size() << '\n';
    for(int x : ans) cout << x << ' '; cout << '\n';
}
int main() {
    #ifdef zqj
    freopen("1.in","r",stdin);
    #endif
	std::ios::sync_with_stdio(false), cin.tie(0);
    cin >> T; 
    while(T--) Main();
    return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3484kb

input:

8
2
0 1
2 4
3
0 1 2
2 10 36
4
0 1 2 3
1 4 9 16
5
0 1 2 3 4
1 25 961 14641 116281
2
2 500000005
5 375000004
2
2 500000005
5 375000004
2
2 500000005
1 2
3
2 500000005 3
5 375000004 10

output:

15
2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 
17
2 999999998 14 0 0 0 0 0 0 0 0 0 0 0 14 999999998 2 
19
1 939060511 787273552 273665956 0 0 0 0 0 0 0 0 0 0 0 273665956 787273552 939060511 1 
21
1 617839688 772195627 752150047 357814672 0 0 0 0 0 0 0 0 0 0 0 357814672 752150047 772195627 617839688 1 
4
44444444...

result:

wrong answer Test case 1: polynomial is not palindromic. (test case 1)