結果

問題 No.1559 Next Rational
ユーザー kotatsugamekotatsugame
提出日時 2021-06-26 01:17:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,142 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 69,876 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-07 15:13:53
合計ジャッジ時間 1,608 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,388 KB
testcase_17 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:7:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<atcoder/modint>
using namespace std;
#line 1 "/home/kotatsugame/library/math/squarematrix.cpp"
#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;
	}
};
#line 5 "a.cpp"
using mint=atcoder::modint1000000007;
using mat=Matrix<mint,2>;
main()
{
	long N,A,B,K;
	cin>>N>>A>>B>>K;
	if(N==1)
	{
		cout<<A<<endl;
		return 0;
	}
	mat X;
	X[0][0]=mint(A*A+B*B+K)/A/B;
	X[0][1]=-1;
	X[1][0]=1;
	X=X.pow(N-2);
	cout<<(X[0][0]*B+X[0][1]*A).val()<<endl;
}
0