結果

問題 No.2506 Sum of Weighted Powers
ユーザー 👑 emthrmemthrm
提出日時 2023-03-29 01:31:03
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,589 bytes
コンパイル時間 3,309 ms
コンパイル使用メモリ 171,952 KB
実行使用メモリ 12,860 KB
最終ジャッジ日時 2024-04-10 18:16:06
合計ジャッジ時間 10,458 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 4 ms
6,940 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 3 ms
6,940 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 RE -
testcase_15 AC 2 ms
6,940 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 3 ms
6,944 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 386 ms
12,016 KB
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 454 ms
12,860 KB
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 AC 164 ms
6,944 KB
testcase_36 AC 192 ms
6,948 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <vector>

#include <atcoder/convolution>
#include <atcoder/modint>

// https://github.com/yosupo06/library-checker-problems/blob/ac1ddf5011509c3d3960c33389291eed23a9ecc7/math/kth_root_mod/sol/correct.cpp
namespace library_checker_problems {

long long pow(long long a, long long n, long long p) {
    long long r=1;
    for (;n>0;n>>=1,a=a*a%p)if(n%2==1)r=r*a%p;
    return r;
}

int cnt(long long a,long long base,long long p) {
    int ret=0;
    while (a!=1) {
        a=pow(a,base,p);
        ++ret;
    }
    return ret;
}

long long inv(long long a, long long p) {
    a%=p;
    long long u=1, v=0;
    long long b=p;
    while (b>0) {
        long long q=a/b;
        a%=b;
        u-=v*q%p;
        u=(u%p+p)%p;
        {
            u^=v;v^=u;u^=v;
            a^=b;b^=a;a^=b;
        }
    }
    return u<0?u+p:u;
}

long long gcd(long long a,long long b) {
    return a==0?b:gcd(b%a,a);
}

long long peth_root(long long a,long long p,int e,long long mod) {
    long long q=mod-1;
    int s=0;
    while (q%p==0) {
        q/=p;
        ++s;
    }
    long long pe=pow(p,e,mod);
    long long ans=pow(a,((pe-1)*inv(q,pe)%pe*q+1)/pe,mod);
    long long c=2;
    while (pow(c,(mod-1)/p,mod)==1)
        ++c;
    c=pow(c,q,mod);
    std::map<long long,int> map;
    long long add=1;
    int v=(int)std::sqrt((double)(s-e)*p)+1;
    long long mul=pow(c,v*pow(p,s-1,mod-1)%(mod-1),mod);
    for (int i=0;i<=v;++i) {
        map[add]=i;
        add=add*mul%mod;
    }
    mul=inv(pow(c,pow(p,s-1,mod-1),mod),mod);
    for (int i=e;i<s;++i) {
        long long err=inv(pow(ans,pe,mod),mod)*a%mod;
        long long target=pow(err,pow(p,s-1-i,mod-1),mod);
        for (int j=0; j<=v; ++j) {
            if (map.find(target)!=map.end()) {
                int x=map[target];
                ans=ans*pow(c,(j+v*x)*pow(p,i-e,mod-1)%(mod-1),mod)%mod;
                break;
            }
            target=target*mul%mod;
            assert(j!=v);
        }
    }
    return ans;
}

long long kth_root(long long a,long long k,long long p) {
    if(k>0&&a%p==0)return 0;
    k%=p-1;
    long long g=gcd(k,p-1);
    if (pow(a,(p-1)/g,p)!=1)
        return -1;
    a=pow(a,inv(k/g,(p-1)/g),p);
    for (long long div=2;div*div<=g;++div) {
        int sz=0;
        while (g%div==0) {
            g/=div;
            ++sz;
        }
        if (sz>0) {
            long long b=peth_root(a,div,sz,p);
            a=b;
        }
    }
    if (g>1)
        a=peth_root(a,g,1,p);
    return a;
}

}  // namespace library_checker_problems

using mint = atcoder::static_modint<943718401>;

// <嘘解法 (WA)>
// 3 乗根を求めようとする。
mint S(const int n, mint x,
       std::vector<mint> a, std::vector<mint> b, std::vector<mint> c) {
  assert(n == std::ssize(a) - 1 &&
         n == std::ssize(b) - 1 &&
         n == std::ssize(c) - 1);

  if (x == 0) {
    mint s = 0;
    s += std::inner_product(a.begin(), a.end(), b.begin(), mint::raw(0)) * c[0];
    s += std::inner_product(std::next(a.begin()), a.end(),
                            std::next(c.begin()), mint::raw(0)) * b[0];
    return s;
  }

  const int cubic_root =
      library_checker_problems::kth_root(x.val(), 3, mint::mod());
  assert(cubic_root != -1);
  x = mint::raw(cubic_root);

  const auto T = [](const int i) -> std::int64_t {
    return std::int64_t{i} * i * (i);
  };

  std::ranges::for_each(
      a, [&x, T, i = 0](mint& e) mutable -> void { e *= x.pow(T(i++)); });
  const mint inv = x.inv();
  std::ranges::for_each(
      b, [&inv, T, i = 0](mint& e) mutable -> void { e *= inv.pow(T(i++)); });
  std::ranges::for_each(
      c, [&inv, T, i = 0](mint& e) mutable -> void { e *= inv.pow(T(i++)); });

  const std::vector<mint> s = atcoder::convolution(b, c);
  return std::inner_product(a.begin(), a.end(), s.begin(), mint::raw(0));
}

int main() {
  constexpr int kMaxN = 200000;

  const auto GetMint = []() -> mint {
    int x;
    std::cin >> x;
    assert(0 <= x && x < mint::mod());
    return mint::raw(x);
  };

  int n;
  std::cin >> n;
  assert(0 <= n && n <= kMaxN);
  const mint x = GetMint();

  std::vector<mint> a(n + 1), b(n + 1), c(n + 1);
  std::ranges::for_each(a, [GetMint](mint& x) -> void { x = GetMint(); });
  std::ranges::for_each(b, [GetMint](mint& x) -> void { x = GetMint(); });
  std::ranges::for_each(c, [GetMint](mint& x) -> void { x = GetMint(); });

  std::cout << S(n, x, a, b, c).val() << '\n';
  return 0;
}
0