結果

問題 No.1648 Sum of Powers
ユーザー kotatsugame
提出日時 2021-08-13 22:59:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 2,489 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 89,432 KB
実行使用メモリ 5,632 KB
最終ジャッジ日時 2024-10-03 22:10:16
合計ジャッジ時間 3,603 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 56
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:88:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   88 | main()
      | ^~~~

ソースコード

diff #
プレゼンテーションモードにする

#include<iostream>
#include<atcoder/modint>
using namespace std;
using mint=atcoder::modint998244353;
#include<vector>
#include<algorithm>
int BSGS(long long X,long long Y,const int P0)
//k s.t. X^k=Y mod P of -1
//0^0=1
{
X%=P0;
Y%=P0;
if(P0==1)return 0;
if(X==0)return Y==1?0:Y==0?1:-1;
int g=1;
for(int i=P0;i>1;i>>=1)g=g*X%P0;
//gcd
for(int p=P0;p;)
{
int t=g%p;
g=p;
p=t;
}
int t=1,fst=0;
while(t%g)
{
if(t==Y)return fst;
t=t*X%P0;
fst++;
}
if(Y%g)return -1;
t/=g;
Y/=g;
const int P=P0/g;
int sqP=0,Xm=1;
while(sqP*sqP<P)
{
Xm=Xm*X%P;
sqP++;
}
vector<pair<int,int> >tb(sqP);
for(int i=0,e=Y;i<sqP;i++)
{
e=e*X%P;
tb[i]=make_pair(e,-i-1);
}
sort(tb.begin(),tb.end());
for(int s=0,e=t;s<P;)
{
e=(long)e*Xm%P;
s+=sqP;
vector<pair<int,int> >::iterator it=lower_bound(tb.begin(),tb.end(),make_pair(e,-sqP));
if(it!=tb.end()&&it->first==e)return fst+s+it->second;
}
return -1;
}
#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;
if(B==0)
{
int k=BSGS(A,P,998244353);
if(k<2)k+=998244353-1;
cout<<k<<endl;
return 0;
}
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;
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0