結果

問題 No.2349 Power!! (Hard)
ユーザー i_am_noobi_am_noob
提出日時 2023-06-24 19:47:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,463 ms / 7,000 ms
コード長 2,077 bytes
コンパイル時間 1,994 ms
コンパイル使用メモリ 202,200 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 15:36:57
合計ジャッジ時間 53,517 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,376 KB
testcase_01 AC 4,409 ms
4,380 KB
testcase_02 AC 4,433 ms
4,380 KB
testcase_03 AC 4,422 ms
4,380 KB
testcase_04 AC 1,999 ms
4,380 KB
testcase_05 AC 4,421 ms
4,376 KB
testcase_06 AC 4,463 ms
4,380 KB
testcase_07 AC 4,399 ms
4,380 KB
testcase_08 AC 4,399 ms
4,376 KB
testcase_09 AC 4,408 ms
4,376 KB
testcase_10 AC 4,416 ms
4,380 KB
testcase_11 AC 4,413 ms
4,376 KB
testcase_12 AC 4,405 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;

#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())

const int mod=998244353,K=119<<8,Y=119<<11,cycle=(mod-1)/(Y*2);
int add(int x, int y){x+=y; if(x>=mod) x-=mod; return x;}
int sub(int x, int y){x-=y; if(x<0) x+=mod; return x;}
int mul(int x, int y){return ((ll)x)*y%mod;}
int Pow(int x, ll y=mod-2){int res=1; for(; y; x=mul(x,x),y>>=1) if(y&1) res=mul(res,x); return res;}

int a,n,pw[K+1],pw_K[(mod-1)/K+1],pw2[cycle+1],inv[2][cycle+1];

void ahcorz(){
    cin >> a >> n;
    pw[0]=1,pw_K[0]=1;
    for(int i=1; i<=K; ++i) pw[i]=mul(pw[i-1],a);
    for(int i=1; i<=(mod-1)/K; ++i) pw_K[i]=mul(pw_K[i-1],pw[K]);
    for(int i=0; i<cycle; ++i){
        //inv[0][i]=inv(1-a^(2Yi)),inv[1][i]=inv(1+a^(2Yi))
        pw2[i]=pw_K[2*(Y/K)*i];
        inv[0][i]=Pow(sub(1,pw2[i])),inv[1][i]=Pow(add(1,pw2[i]));
    }
    int de=Pow(a,1ll*Y*Y),m=n/Y,rem=n%Y;
    assert(de==1||de==mod-1);
    int res=0;
    for(int rr=0; rr<cycle; ++rr){
        int tot0=0,tot1=0;
        for(int r=rr; r<Y; r+=cycle){
            int tmp=1ll*r*r%(mod-1),pw_tmp=mul(pw_K[tmp/K],pw[tmp%K]);
            if(r<rem) tot1=add(tot1,pw_tmp);
            else tot0=add(tot0,pw_tmp);
        }
        int d=mul(de,pw2[rr]);
        if(d==1) res=add(res,add(mul(tot0,m),mul(tot1,m+1)));
        else if(de==1){
            res=add(res,mul(tot0,mul(sub(1,pw2[rr*m%cycle]),inv[0][rr])));
            res=add(res,mul(tot1,mul(sub(1,pw2[rr*(m+1)%cycle]),inv[0][rr])));
        }
        else if(m%2==0){
            res=add(res,mul(tot0,mul(sub(1,pw2[rr*m%cycle]),inv[1][rr])));
            res=add(res,mul(tot1,mul(add(1,pw2[rr*(m+1)%cycle]),inv[1][rr])));
        }
        else{
            res=add(res,mul(tot0,mul(add(1,pw2[rr*m%cycle]),inv[1][rr])));
            res=add(res,mul(tot1,mul(sub(1,pw2[rr*(m+1)%cycle]),inv[1][rr])));
        }
    }
    cout << res << "\n";
}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    int t; cin >> t;
    while(t--) ahcorz();
}
0