結果

問題 No.2349 Power!! (Hard)
ユーザー maksim
提出日時 2025-09-19 22:19:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 4,422 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 167,228 KB
実行使用メモリ 11,044 KB
最終ジャッジ日時 2025-09-19 22:20:11
合計ジャッジ時間 29,426 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 4 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define int long long

template<int32_t MOD>
struct ModInt {
    int32_t value;

    ModInt() : value(0) {}

    ModInt(long long v) : value(v % MOD) { if (value < 0) value += MOD; }

    ModInt(int32_t v): value(v % MOD) { if (value < 0) value += MOD; }

    ModInt operator+=(ModInt m) {
        value += m.value;
        if (value >= MOD) value -= MOD;
        return value;
    }

    ModInt operator-=(ModInt m) {
        value -= m.value;
        if (value < 0) value += MOD;
        return value;
    }

    ModInt operator*=(ModInt m) {
        value = (value * 1LL * m.value) % MOD;
        return value;
    }

    ModInt power(long long exp) const {
        if (exp == 0) return 1;
        ModInt res = (exp & 1 ? value : 1);
        ModInt half = power(exp >> 1);
        return res * half * half;
    }

    ModInt operator/=(ModInt m) { return *this *= m.power(MOD - 2); }

    friend std::istream &operator>>(std::istream &is, ModInt &m) {
        is >> m.value;
        return is;
    }

    friend std::ostream &operator<<(std::ostream &os, const ModInt &m) {
        os << m.value;
        return os;
    }

    explicit operator int32_t() const { return value; }

    explicit operator long long() const { return value; }

    static int32_t mod() { return MOD; }
};

template<int32_t MOD>ModInt<MOD> operator+(ModInt<MOD> a, ModInt<MOD> b) { return a += b; }
template<int32_t MOD, typename L>ModInt<MOD> operator+(L a, ModInt<MOD> b) { return ModInt<MOD>(a) += b; }
template<int32_t MOD, typename R>ModInt<MOD> operator+(ModInt<MOD> a, R b) { return a += b; }

template<int32_t MOD>ModInt<MOD> operator-(ModInt<MOD> a, ModInt<MOD> b) { return a -= b; }
template<int32_t MOD, typename L>ModInt<MOD> operator-(L a, ModInt<MOD> b) { return ModInt<MOD>(a) -= b; }
template<int32_t MOD, typename R>ModInt<MOD> operator-(ModInt<MOD> a, R b) { return a -= b; }

template<int32_t MOD>ModInt<MOD> operator*(ModInt<MOD> a, ModInt<MOD> b) { return a *= b; }
template<int32_t MOD, typename L>ModInt<MOD> operator*(L a, ModInt<MOD> b) { return ModInt<MOD>(a) *= b; }
template<int32_t MOD, typename R>ModInt<MOD> operator*(ModInt<MOD> a, R b) { return a *= b; }

template<int32_t MOD>ModInt<MOD> operator/(ModInt<MOD> a, ModInt<MOD> b) { return a /= b; }
template<int32_t MOD, typename L>ModInt<MOD> operator/(L a, ModInt<MOD> b) { return ModInt<MOD>(a) /= b; }
template<int32_t MOD, typename R>ModInt<MOD> operator/(ModInt<MOD> a, R b) { return a /= b; }

template<int32_t MOD>bool operator==(ModInt<MOD> a, ModInt<MOD> b) { return a.value == b.value; }
template<int32_t MOD, typename L>bool operator==(L a, ModInt<MOD> b) { return a == b.value; }
template<int32_t MOD, typename R>bool operator==(ModInt<MOD> a, R b) { return a.value == b; }

template<int32_t MOD>bool operator!=(ModInt<MOD> a, ModInt<MOD> b) { return a.value != b.value; }
template<int32_t MOD, typename L>bool operator!=(L a, ModInt<MOD> b) { return a != b.value; }
template<int32_t MOD, typename R>bool operator!=(ModInt<MOD> a, R b) { return a.value != b; }

using mint = ModInt<998244353>;
//using mint = ModInt<(int32_t)(1e9 + 7)>;
//237c16
const int h=(1<<12)*7*17;
const int sz=((998244353-1)/h);
mint g[1024][2];
mint sum[1024][2];
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t;cin>>t;while(t--)
    {
    mint a;int n;cin>>a>>n;mint u2=a.power(h);mint cur=1;
    mint pos[sz];for(int i=0;i<sz;++i) {pos[i]=cur;cur*=u2;}
    mint invs[sz];for(int i=0;i<sz;++i) invs[i]=1/(pos[i]-1);
    for(int i=0;i<1024;++i)
    {
        for(int j=0;j<2;++j)
        {
            sum[i][j]=0;
            int o=(j==0 ? (n-1)/h : (n-h)/h);++o;
            mint u=pos[(2*i)%sz];
            mint t=a.power(o*i*2*h);
            if(u!=1) {mint inva=invs[(2*i)%sz];mint res=(t-1)*inva;g[i][j]=res;}
            else {g[i][j]=o;}
        }
    }
    mint cur1=1;mint cur2=a;mint t=1;mint u1=1;mint u=1;mint a2=(a*a);
    for(int i=0;i<h;++i)
    {
        if(i>=n) continue;
        int o=(n-1-i)/h+1;
        if(o==(n-1)/h+1)
        {
            sum[i%1024][0]+=cur1;
        }
        else
        {
            sum[i%1024][1]+=cur1;
        }
        cur1*=cur2;cur2*=a2;
    }
    mint res=0;
    for(int i=0;i<1024;++i)
    {
        for(int j=0;j<2;++j)
        {
            res+=g[i][j]*sum[i][j];
        }
    }
    cout<<res<<'\n';
    }
    return 0;
}
0