結果

問題 No.1648 Sum of Powers
ユーザー kotatsugamekotatsugame
提出日時 2021-08-13 22:59:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 2,489 bytes
コンパイル時間 1,164 ms
コンパイル使用メモリ 88,916 KB
実行使用メモリ 5,472 KB
最終ジャッジ日時 2023-07-27 06:50:04
合計ジャッジ時間 5,233 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
5,340 KB
testcase_01 AC 10 ms
5,352 KB
testcase_02 AC 6 ms
4,380 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 9 ms
4,380 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 8 ms
4,380 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 6 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 25 ms
5,352 KB
testcase_23 AC 23 ms
5,404 KB
testcase_24 AC 25 ms
5,364 KB
testcase_25 AC 24 ms
5,472 KB
testcase_26 AC 25 ms
5,424 KB
testcase_27 AC 24 ms
5,280 KB
testcase_28 AC 26 ms
5,320 KB
testcase_29 AC 25 ms
5,328 KB
testcase_30 AC 25 ms
5,468 KB
testcase_31 AC 27 ms
5,264 KB
testcase_32 AC 25 ms
5,336 KB
testcase_33 AC 25 ms
5,328 KB
testcase_34 AC 25 ms
5,344 KB
testcase_35 AC 24 ms
5,320 KB
testcase_36 AC 25 ms
5,340 KB
testcase_37 AC 27 ms
5,432 KB
testcase_38 AC 25 ms
5,332 KB
testcase_39 AC 28 ms
5,388 KB
testcase_40 AC 25 ms
5,320 KB
testcase_41 AC 29 ms
5,348 KB
testcase_42 AC 26 ms
5,324 KB
testcase_43 AC 24 ms
5,336 KB
testcase_44 AC 25 ms
5,348 KB
testcase_45 AC 25 ms
5,344 KB
testcase_46 AC 25 ms
5,348 KB
testcase_47 AC 6 ms
4,376 KB
testcase_48 AC 11 ms
5,368 KB
testcase_49 AC 7 ms
4,380 KB
testcase_50 AC 8 ms
4,376 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 24 ms
5,428 KB
testcase_53 AC 24 ms
5,288 KB
testcase_54 AC 24 ms
5,364 KB
testcase_55 AC 26 ms
5,348 KB
testcase_56 AC 25 ms
5,468 KB
testcase_57 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:88:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-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;
		}
	}
}
0