QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621175#618. 多项式乘法Ycfhnnd0 525ms100336kbC++202.6kb2024-10-08 10:36:032024-10-08 10:36:04

Judging History

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

  • [2024-10-08 10:36:04]
  • 评测
  • 测评结果:0
  • 用时:525ms
  • 内存:100336kb
  • [2024-10-08 10:36:03]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;
namespace FFT{
    const long double PI = acosl(-1.L);
    struct C{
        double x, y;
        C(double x = 0, double y = 0) : x(x), y(y) {}
        C operator!() const { return C(x, -y); }
    };
    inline C operator*(C a, C b) { return C(a.x * b.x - a.y * b.y, a.y * b.x + b.y * a.x); }
    inline C operator+(C a, C b) { return C(a.x + b.x, a.y + b.y); }
    inline C operator-(C a, C b) { return C(a.x - b.x, a.y - b.y); }
    constexpr int L(1 << 21);

    C w[L];
    void dft(C *a, int n){
        for (static int k = (w[1].x = 1, 2); k < n; k <<= 1){
            for (int i = 0; i < k; i++){
                w[i + k] = i & 1 ? C(cosl(PI * i / k), sinl(PI * i / k)) : w[i + k >> 1];
            }
        }
        for (int k = n >> 1; k; k >>= 1){
            for (int i = 0; i < n; i += k << 1){
                for (int j = 0; j < k; j++){
                    C &x = a[i + j], y = a[i + j + k];
                    a[i + j + k] = (x - y) * w[k + j], x = x + y;
                }
            }
        }
    }
    void idft(C *a, int n){
        for (int k = 1; k < n; k <<= 1){
            for (int i = 0; i < n; i += k << 1){
                for (int j = 0; j < k; j++){
                    C &x = a[i + j], y = a[i + j + k] * w[k + j];
                    a[i + j + k] = x - y, x = x + y;
                }
            }
        }
        for (int i = 0; i < n; i++)
            a[i].x /= n, a[i].y /= n;
        reverse(a + 1, a + n);
    }
}

using Poly = vector<i64>;
vector<i64> conv(const Poly &a, const Poly &b){
    using namespace FFT;
    int len = a.size() + b.size() - 1;
    int n = 1 << __lg(len * 2 - 1);
    vector<C> f(n);
    for (int i = 0; i < n; i++){
        f[i] = C(i < a.size() ? a[i] : 0,
                 i < b.size() ? b[i] : 0);
    }
    dft(f.data(), n);
    for (int i = 0; i < n; i++)
        f[i] = f[i] * f[i];
    idft(f.data(), n);
    vector<i64> r(len);
    for (int i = 0; i < len; i++){
        r[i] = f[i].y / 2 + 0.5;
    }
    return r;
}
void solve(){
    int n, m;
    cin >> n >> m;
    Poly f(n + 1), g(m + 1);
    for (int i = 0;i <= n;i ++){
        cin >> f[i];
    }
    for (int i = 0;i <= m;i ++){
        cin >> g[i];
    }
    auto ans = conv(f, g);
    for (int i = 0;i < ans.size();i ++){
        cout << ans[i] << " ";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    while (T --){
        solve();
    }

    return 0;
}

详细


Pretests


Final Tests

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 36648kb

input:

96 96
600395131 184265451 942971382 534262851 830366882 542271170 294355449 501371170 797809599 964826049 276651245 375755165 662619442 941920605 328216963 507795473 460271147 874920847 818231910 156789488 590591583 732194508 793983630 93566697 836155866 305319153 432040686 621119061 835023373 57138...

output:

293317256442966016 622165877277235200 1148384202335132672 1744330057081987072 1860706225583704064 2797555036758915072 2251669196605142016 2733962690963818496 2956161153011294208 3386689821974902784 4171909363448902656 3443615585976889344 3892200345118503936 4222713817313625088 4684195158345958400 46...

result:

wrong answer 1st numbers differ - expected: '683858396', found: '293317256442966016'

Test #2:

score: 0
Wrong Answer
time: 8ms
memory: 37092kb

input:

4992 4994
471194390 313639917 705341086 119536186 430124603 244978845 185588341 13731324 707132801 88167972 927324568 846658454 523684029 5133605 767200778 502476309 539772401 778154025 266136872 183516351 260704325 49303370 475056182 928574546 740424153 277920667 708439854 746983628 839491869 53579...

output:

134473066252140544 542666022105284608 904382502867894272 1309766133310029824 1350017506452176896 1550932542357897216 1748252215164010496 1784442257412587520 1696086726028722176 2047234800668114944 1943750560816988160 2719201956130914304 3192358180281057280 3385082672675454976 3552133728643219456 418...

result:

wrong answer 1st numbers differ - expected: '700935456', found: '134473066252140544'

Test #3:

score: 0
Wrong Answer
time: 16ms
memory: 38440kb

input:

29995 29992
417238081 53580806 733071257 224121793 786137422 127072245 129083351 988357079 246853229 150935424 596994106 975796660 838029970 619117898 328485797 948485083 574261409 79312345 596346086 489787404 929520168 515647000 211731260 50868568 811515357 428215135 498099163 644485329 802849075 3...

output:

333096846127267840 168127337663037440 959380886287548416 803298363025915904 1575256244477755392 1528350218920656896 1729129116289990656 2670982509184417792 2072069924366319616 2609407495264272384 3183494419512819712 3065007329336885248 4107487652429168640 4220986916462919680 5187908794045693952 5048...

result:

wrong answer 1st numbers differ - expected: '115270920', found: '333096846127267840'

Test #4:

score: 0
Wrong Answer
time: 57ms
memory: 43492kb

input:

100000 99993
812398607 947396010 797321381 883949986 56052416 586258761 193247973 611124334 773505112 142179482 565466227 140875825 79890768 893500101 553768089 648879319 480419657 915530184 799329430 494818755 793895824 851865180 459534006 259473419 610037701 472768430 868914058 887444584 588850309...

output:

462564215077470208 752354318985723904 817168602909114368 1159286459402813440 866250444163252224 1213131176422670336 1519025181810491392 1733602411941986304 2736704129090977792 2920908816963862528 3368153919533350912 3549256500532740096 2672722148545527808 3450543017832742912 3238543165170384896 3759...

result:

wrong answer 1st numbers differ - expected: '821875273', found: '462564215077470208'

Test #5:

score: 0
Wrong Answer
time: 525ms
memory: 100336kb

input:

999993 999994
388529697 811245378 165909114 295553883 667981275 78502012 400874009 139394758 249494489 4636487 997712665 259780805 431039016 716944209 709300152 356513646 823185021 699568300 650937921 859190797 899514799 785648601 933470757 627225124 349752104 471458923 456404256 48134357 315599086 ...

output:

208919996368158720 762534324133167104 1119084467969851392 1110166014349279232 996696216878186496 1209667789778845696 1638540798993629184 1498675824469999616 1313288483120349184 1597669149994123264 2394878301798662144 1999648512043646976 2701351539376652288 2471100347683176448 2674227558702120960 370...

result:

wrong answer 1st numbers differ - expected: '199012842', found: '208919996368158720'