結果

問題 No.2348 Power!! (Easy)
ユーザー maksim
提出日時 2023-06-09 22:57:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 420 ms / 5,000 ms
コード長 1,360 bytes
コンパイル時間 1,530 ms
コンパイル使用メモリ 161,992 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-01-02 04:49:14
合計ジャッジ時間 6,328 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int p=998244353;
int po(int a,int b){
  int x = a, res = 1;
  int h = b;
  while (h > 0) {
    if (h & 1) {res *= x;res%=p;}
    x *= x;x%=p;
    h >>= 1;
  }
  return res;
}
int inverse(int a,int m) {
  int u = 0, v = 1;
  while (a != 0) {
    int t = m / a;
    m -= t * a; swap(a, m);
    u -= t * v; swap(u, v);
  }
  return u;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int h=(1<<12)*7*17;int sz=((p-1)/h);
    int t;cin>>t;while(t--)
    {
    int a,n;cin>>a>>n;
    int pos[sz];for(int i=0;i<sz;++i) pos[i]=po(a,i*h);
    int invs[sz];for(int i=0;i<sz;++i) invs[i]=inverse(pos[i]-1,p);
    int res=0;
    int cur1=1;int cur2=1;int t=1;int u1=1;int u=1;
    for(int i=0;i<h;++i)
    {
        if(i>=n) continue;
        int o=(n-1-i)/h+1;
        int l=i*i;int r=i*i+o*i*2*h;
        int step=2*i*h;int u=pos[(2*i)%sz];
        if(i==0 || (n-i-1)/h!=(n-i)/h)
        {
            t=po(a,o*i*2*h);u1=po(a,o*2*h);
        }
        else
        {
            t*=u1;t%=p;
        }
        if(u!=1) {int inva=invs[(2*i)%sz];int v=(cur1*t)%p;res+=(v-cur1)*inva;res%=p;}
        else {res+=cur1*o;res%=p;}
        cur1*=cur2;cur1%=p;cur1*=cur2;cur1%=p;cur1*=a;cur1%=p;cur2*=a;cur2%=p;
    }
    cout<<(res%p+p)%p<<'\n';
    }
    return 0;
}
0