結果

問題 No.1648 Sum of Powers
ユーザー kotatsugamekotatsugame
提出日時 2021-08-13 23:02:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,592 bytes
コンパイル時間 998 ms
コンパイル使用メモリ 85,132 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 20:01:34
合計ジャッジ時間 4,524 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
6,816 KB
testcase_01 AC 9 ms
6,940 KB
testcase_02 AC 23 ms
6,940 KB
testcase_03 AC 23 ms
6,944 KB
testcase_04 AC 22 ms
6,940 KB
testcase_05 AC 21 ms
6,944 KB
testcase_06 AC 22 ms
6,944 KB
testcase_07 AC 21 ms
6,940 KB
testcase_08 AC 23 ms
6,940 KB
testcase_09 AC 22 ms
6,944 KB
testcase_10 AC 23 ms
6,944 KB
testcase_11 AC 22 ms
6,940 KB
testcase_12 AC 24 ms
6,940 KB
testcase_13 AC 22 ms
6,944 KB
testcase_14 AC 23 ms
6,944 KB
testcase_15 AC 22 ms
6,940 KB
testcase_16 AC 23 ms
6,944 KB
testcase_17 AC 23 ms
6,944 KB
testcase_18 AC 24 ms
6,940 KB
testcase_19 AC 24 ms
6,940 KB
testcase_20 AC 23 ms
6,940 KB
testcase_21 AC 23 ms
6,940 KB
testcase_22 AC 23 ms
6,944 KB
testcase_23 AC 23 ms
6,944 KB
testcase_24 AC 22 ms
6,940 KB
testcase_25 AC 23 ms
6,940 KB
testcase_26 AC 23 ms
6,940 KB
testcase_27 AC 22 ms
6,940 KB
testcase_28 AC 23 ms
6,940 KB
testcase_29 AC 23 ms
6,940 KB
testcase_30 AC 23 ms
6,940 KB
testcase_31 AC 25 ms
6,940 KB
testcase_32 AC 23 ms
6,944 KB
testcase_33 AC 23 ms
6,940 KB
testcase_34 AC 23 ms
6,944 KB
testcase_35 AC 23 ms
6,940 KB
testcase_36 AC 23 ms
6,944 KB
testcase_37 AC 24 ms
6,944 KB
testcase_38 AC 24 ms
6,944 KB
testcase_39 AC 26 ms
6,940 KB
testcase_40 AC 23 ms
6,940 KB
testcase_41 AC 25 ms
6,944 KB
testcase_42 AC 23 ms
6,940 KB
testcase_43 AC 24 ms
6,940 KB
testcase_44 AC 23 ms
6,940 KB
testcase_45 AC 23 ms
6,944 KB
testcase_46 AC 24 ms
6,944 KB
testcase_47 AC 23 ms
6,940 KB
testcase_48 AC 10 ms
6,940 KB
testcase_49 AC 23 ms
6,940 KB
testcase_50 AC 22 ms
6,940 KB
testcase_51 AC 9 ms
6,940 KB
testcase_52 AC 22 ms
6,944 KB
testcase_53 AC 22 ms
6,944 KB
testcase_54 AC 23 ms
6,940 KB
testcase_55 AC 23 ms
6,948 KB
testcase_56 AC 23 ms
6,940 KB
testcase_57 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:38:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   38 | main()
      | ^~~~

ソースコード

diff #

#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;
		}
	}
}
0