結果
| 問題 |
No.1559 Next Rational
|
| コンテスト | |
| ユーザー |
momoyuu
|
| 提出日時 | 2024-08-07 21:39:40 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 2,000 ms |
| コード長 | 1,278 bytes |
| コンパイル時間 | 1,006 ms |
| コンパイル使用メモリ | 102,860 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-08-07 21:39:42 |
| 合計ジャッジ時間 | 2,051 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 15 |
ソースコード
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
#include<atcoder/modint>
using mint = atcoder::modint1000000007;
#include<array>
const int B = 2;
using dat = array<array<mint,B>,B>;
dat op(dat a,dat b){
dat c{};
for(int i = 0;i<B;i++) {
for(int j = 0;j<B;j++){
for(int k = 0;k<B;k++){
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
dat e(){
dat c{};
for(int i = 0;i<B;i++) c[i][i] = 1;
return c;
}
dat mpow(dat a,ll k){
dat b = e();
while(k){
if(k&1) b = op(a,b);
a = op(a,a);
k >>= 1;
}
return b;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n,a,b,k;
cin>>n>>a>>b>>k;
mint c = mint(b) * mint(b) + mint(k);
c *= mint(a).inv();
if(n==1) {
cout<<a<<endl;
return 0;
}else if(n==2){
cout<<b<<endl;
return 0;
}else if(n==3){
cout<<c.val()<<endl;
return 0;
}
mint x = c + mint(a);
x *= mint(b).inv();
dat now{};
now[0][0] = x;
now[0][1] = -1;
now[1][0] = 1;
now = mpow(now,n-2);
mint ans = now[0][0] * mint(b) + now[0][1] * mint(a);
cout<<ans.val()<<endl;
}
momoyuu