結果
| 問題 |
No.2349 Power!! (Hard)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-06-24 19:47:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 4,524 ms / 7,000 ms |
| コード長 | 2,077 bytes |
| コンパイル時間 | 1,985 ms |
| コンパイル使用メモリ | 195,336 KB |
| 最終ジャッジ日時 | 2025-02-15 02:09:47 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 12 |
ソースコード
#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();
}