結果

問題 No.1648 Sum of Powers
ユーザー 蜜蜂蜜蜂
提出日時 2021-07-28 23:38:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 678 ms / 2,000 ms
コード長 3,159 bytes
コンパイル時間 4,735 ms
コンパイル使用メモリ 247,608 KB
実行使用メモリ 88,632 KB
最終ジャッジ日時 2024-04-14 16:03:12
合計ジャッジ時間 27,866 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 571 ms
80,620 KB
testcase_01 AC 147 ms
36,024 KB
testcase_02 AC 60 ms
12,416 KB
testcase_03 AC 57 ms
12,160 KB
testcase_04 AC 66 ms
12,672 KB
testcase_05 AC 61 ms
12,416 KB
testcase_06 AC 64 ms
12,288 KB
testcase_07 AC 62 ms
12,288 KB
testcase_08 AC 68 ms
12,672 KB
testcase_09 AC 61 ms
12,288 KB
testcase_10 AC 60 ms
12,288 KB
testcase_11 AC 60 ms
12,032 KB
testcase_12 AC 72 ms
12,672 KB
testcase_13 AC 61 ms
12,160 KB
testcase_14 AC 58 ms
12,032 KB
testcase_15 AC 62 ms
12,288 KB
testcase_16 AC 61 ms
12,160 KB
testcase_17 AC 65 ms
12,416 KB
testcase_18 AC 60 ms
12,032 KB
testcase_19 AC 60 ms
12,288 KB
testcase_20 AC 59 ms
12,032 KB
testcase_21 AC 58 ms
12,288 KB
testcase_22 AC 594 ms
82,028 KB
testcase_23 AC 576 ms
81,080 KB
testcase_24 AC 568 ms
80,892 KB
testcase_25 AC 571 ms
81,652 KB
testcase_26 AC 569 ms
80,952 KB
testcase_27 AC 563 ms
80,824 KB
testcase_28 AC 589 ms
82,232 KB
testcase_29 AC 579 ms
81,556 KB
testcase_30 AC 578 ms
82,212 KB
testcase_31 AC 646 ms
85,096 KB
testcase_32 AC 582 ms
81,968 KB
testcase_33 AC 571 ms
81,072 KB
testcase_34 AC 568 ms
80,952 KB
testcase_35 AC 580 ms
81,468 KB
testcase_36 AC 567 ms
81,460 KB
testcase_37 AC 646 ms
85,680 KB
testcase_38 AC 603 ms
83,700 KB
testcase_39 AC 668 ms
87,472 KB
testcase_40 AC 597 ms
83,128 KB
testcase_41 AC 678 ms
88,632 KB
testcase_42 AC 582 ms
82,620 KB
testcase_43 AC 588 ms
82,588 KB
testcase_44 AC 596 ms
82,744 KB
testcase_45 AC 581 ms
82,736 KB
testcase_46 AC 570 ms
82,588 KB
testcase_47 AC 59 ms
12,416 KB
testcase_48 AC 147 ms
36,028 KB
testcase_49 AC 60 ms
12,416 KB
testcase_50 AC 64 ms
12,544 KB
testcase_51 AC 4 ms
6,944 KB
testcase_52 AC 551 ms
80,572 KB
testcase_53 AC 556 ms
81,116 KB
testcase_54 AC 555 ms
80,612 KB
testcase_55 AC 577 ms
82,768 KB
testcase_56 AC 584 ms
82,864 KB
testcase_57 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//g++ 1.cpp -std=c++14 -O2 -I .
#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;

#define fi first
#define se second
#define pb push_back
#define pq_big(T) priority_queue<T,vector<T>,less<T>>
#define pq_small(T) priority_queue<T,vector<T>,greater<T>>
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)

constexpr ll mod = 998244353;

//p*q行列aとr*s行列bの積(mod)
template <class X> vector<vector<X>> matmulti(const vector<vector<X>> &a,const vector<vector<X>> &b,X mod){
  int p=a.size(),q=a[0].size(),r=b.size(),s=b[0].size();
  if(q!=r){
    cout<<"WARNING : SIZE ERROR"<<endl;
  }
  vector<vector<X>> res(p,vector<X>(s,0));
  
  for(int i=0;i<p;i++){
    for(int j=0;j<s;j++){
      for(int k=0;k<q;k++){
        res[i][j]+=(a[i][k]%mod*b[k][j]%mod)%mod;
      }
      res[i][j]%=mod;
    }
  }
  return res;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll a,b,p,q;
  cin>>a>>b>>p>>q;

  if(b==0){
    if(a==0){
      cout<<2<<"\n";
      return 0;
    }

    //pow1[i] := A^i
    //pow2[i] := A^{10^5 i}
    ll pow1[110000],pow2[110000];
    pow1[0]=1,pow2[0]=1;
    map<ll,int> m;
    m[1]=0;

    rep(i,1,110000){
      pow1[i]=pow1[i-1]*a;
      pow1[i]%=mod;
      m[pow1[i]]=i;
    }
    rep(i,1,110000){
      pow2[i]=pow2[i-1]*pow1[100000];
      pow2[i]%=mod;
    }

    rep(i,0,110000){
      ll inv=inv_mod(pow2[i],mod);
      ll z=(p*inv)%mod;
      ll ans=-1;
      if(z==1){
        ans=100000*i;
      }
      else if(m[z]!=0){
        ans=100000*i+m[z];
      }

      if(ans>=2){
        cout<<ans<<"\n";
        return 0;
      }
    }
    
    cout<<"Failed"<<endl;
    return 0;
  }

  vvll m(2,vll(2)),e(2,vll(2)),start(2,vll(1)),end(2,vll(1));
  m[0][0]=a,m[0][1]=(mod-b)%mod,m[1][0]=1,m[1][1]=0;
  e[0][0]=1,e[0][1]=0,e[1][0]=0,e[1][1]=1;
  start[0][0]=2,start[1][0]=(a*inv_mod(b,mod))%mod;
  end[0][0]=p,end[1][0]=q;

  //pow1[i] := m^i
  //pow2[i] := m^{10^5 i}
  vector<vvll> pow1,pow2;
  pow1.emplace_back(e),pow2.emplace_back(e);

  rep(i,0,110000){
    pow1.emplace_back(matmulti(pow1[i],m,mod));
  }
  rep(i,0,110000){
    pow2.emplace_back(matmulti(pow2[i],pow1[100000],mod));
  }

  map<vvll,int> seen;
  map<vvll,int> val;
  rep(i,0,110000){
    auto x=matmulti(pow1[i],start,mod);
    seen[x]=1;
    val[x]=i;
  }

  rep(i,0,110000){
    //x=m^{10^5 i}
    //inv_x = x^-1
    vvll x=pow2[i];
    ll det=x[0][0]*x[1][1]-x[0][1]*x[1][0];
    det=((det%mod)+mod)%mod;
    ll inv_det=inv_mod(det,mod);

    vvll inv_x(2,vll(2));
    inv_x[0][0]=(inv_det*x[1][1])%mod,inv_x[0][1]=(inv_det*(mod-x[0][1]))%mod,inv_x[1][0]=(inv_det*(mod-x[1][0]))%mod,inv_x[1][1]=(inv_det*x[0][0])%mod;

    auto z=matmulti(inv_x,end,mod);

    ll ans=-1;
    if(seen[z]==1){
      ans=100000*i+val[z];
    }

    if(ans>=2){
      cout<<ans<<"\n";
      return 0;
    }
  }
}
0