結果

問題 No.3118 Increment or Multiply
ユーザー umezo
提出日時 2025-04-19 23:51:58
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 811 bytes
コンパイル時間 2,822 ms
コンパイル使用メモリ 273,000 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-04-19 23:52:03
合計ジャッジ時間 4,080 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
template <class T> using V=vector<T>;
template <class T> using VV=V<V<T>>;

const int MOD=998244353;
const ll m2=499122177;
ll f(ll x){
  x%=MOD;
  return x*(x+1)%MOD*m2%MOD;
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int t;
  cin>>t;
  while(t--){
    ll n,a;
    cin>>n>>a;
    if(a==1){
      cout<<f(n-1)<<'\n';
      continue;
    }
    ll now=n,ans=0,sum=0,j=0;
    while(1){
      ll nex=now/a;
      ans=(ans+f(now-nex-1))%MOD;
      ll cnt=now-nex;
      ans=(ans+cnt*sum%MOD+cnt*j%MOD)%MOD;
      sum=(sum+now-nex*a)%MOD;
      if(nex==0) break;
      now=nex;
      j++;
    }
    cout<<ans<<'\n';
  }
    
  return 0;
}
0