結果

問題 No.980 Fibonacci Convolution Hard
ユーザー てりやきてりやき
提出日時 2020-12-24 01:47:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,495 bytes
コンパイル時間 2,471 ms
コンパイル使用メモリ 176,304 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-21 15:30:24
合計ジャッジ時間 45,865 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 AC 1,985 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(a)a.begin(),a.end()
#define rall(a)a.rbegin(),a.rend()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
const int mod2=998244353;
using P = pair<int,int>;
using Pl= pair<ll,ll>;
using ld=long double;
using V=vector<int>;
using Vl=vector<ll>;
using Vd=vector<ld>;
using VV=vector<vector<int>>;
using VVl=vector<vector<ll>>;
using VVd=vector<vector<ld>>;

// modint: mod 計算を int を扱うように扱える構造体
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
using mint = Fp<mod>;
using Vm=vector<mint>;

Vm even(Vm P){
    Vm R;
    int n=P.size();
    for (int i = 0; i <n;i+=2) {
        R.push_back(P[i]);
    }
    return R;
}
Vm odd(Vm P){
    Vm R;
    int n=P.size();
    for (int i =1; i < n;i+=2) {
        R.push_back(P[i]);
    }
    return R;
}
Vm mi(Vm P){
    Vm R=P;
    int n=R.size();
    for (int i = 1; i < n;i+=2) {
        R[i]*=-1;
    }
    return R;
}

Vm mul(Vm P,Vm Q){
    int n=P.size();
    int m=Q.size();
    Vm R(n+m,0);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            R[i+j]+=P[i]*Q[j];
        }
    }
    return R;
}

mint mori(int n,Vm P,Vm Q){
    while(n>0){
        Vm U=mul(P,mi(Q));
        if(n%2==0)P=even(U);
        else P=odd(U);
        Q=even(mul(Q,mi(Q)));
        n/=2;
    }
    return P[0]/Q[0];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll p;cin >>p;
    ll t;cin >>t;
    Vm P={0,0,1};
    Vm Q={1,-p,-1};
    Q=mul(Q,Q);
    while(t--){
        int q;cin >>q;
        cout<<mori(q-2,P,Q)<<"\n";
    }
    return 0;
}
0