結果
問題 | No.1648 Sum of Powers |
ユーザー | 蜜蜂 |
提出日時 | 2021-07-28 23:38:36 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 670 ms / 2,000 ms |
コード長 | 3,159 bytes |
コンパイル時間 | 4,482 ms |
コンパイル使用メモリ | 253,968 KB |
実行使用メモリ | 88,252 KB |
最終ジャッジ日時 | 2024-10-03 16:01:13 |
合計ジャッジ時間 | 26,778 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 56 |
ソースコード
//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; } } }