結果
問題 | No.1648 Sum of Powers |
ユーザー |
|
提出日時 | 2021-08-13 23:02:55 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 25 ms / 2,000 ms |
コード長 | 1,592 bytes |
コンパイル時間 | 797 ms |
コンパイル使用メモリ | 84,412 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-03 22:19:48 |
合計ジャッジ時間 | 3,938 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 56 |
コンパイルメッセージ
main.cpp:38:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] 38 | main() | ^~~~
ソースコード
#include<iostream> #include<algorithm> #include<vector> #include<atcoder/modint> using namespace std; using mint=atcoder::modint998244353; #include<array> template<typename T,unsigned int N> struct Matrix{ array<array<T,N>,N>dat; array<T,N>&operator[](int i){return dat[i];} const array<T,N>&operator[](int i)const{return dat[i];} static Matrix eye(){ Matrix res; for(int i=0;i<N;i++)res[i][i]=1; return res; } Matrix operator+(const Matrix&A)const{ Matrix res; for(int i=0;i<N;i++)for(int j=0;j<N;j++) res[i][j]=dat[i][j]+A[i][j]; return res; } Matrix operator*(const Matrix&A)const{ Matrix res; for(int i=0;i<N;i++)for(int k=0;k<N;k++)for(int j=0;j<N;j++) res[i][j]+=dat[i][k]*A[k][j]; return res; } Matrix pow(long long n)const{ Matrix a=*this,res=eye(); for(;n;a=a*a,n>>=1)if(n&1)res=res*a; return res; } }; using mat=Matrix<mint,2>; int A,B,P,Q; main() { cin>>A>>B>>P>>Q; int sqP=2e5; mat X; X[0][0]=A;X[0][1]=-B;X[1][0]=1;X[1][1]=0; mat E=X.pow(sqP); vector<pair<pair<int,int>,int> >tb(sqP); pair<int,int>e=make_pair(P,Q); for(int i=0;i<sqP;i++) { e=make_pair((X[0][0]*e.first+X[0][1]*e.second).val(),(X[1][0]*e.first+X[1][1]*e.second).val()); tb[i]=make_pair(e,-i-1); } sort(tb.begin(),tb.end()); e=make_pair(((mint)A*A-2*B).val(),A); for(long s=0;;) { e=make_pair((E[0][0]*e.first+E[0][1]*e.second).val(),(E[1][0]*e.first+E[1][1]*e.second).val()); s+=sqP; auto it=lower_bound(tb.begin(),tb.end(),make_pair(e,-sqP)); if(it!=tb.end()&&it->first==e) { long N=s+it->second+2; cout<<N<<endl; return 0; } } }