結果

問題 No.1501 酔歩
ユーザー SSRS
提出日時 2021-05-07 22:13:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,457 bytes
コンパイル時間 5,859 ms
コンパイル使用メモリ 391,356 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-09-15 10:14:47
合計ジャッジ時間 11,736 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

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

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
boost::multiprecision::cpp_int gcd(boost::multiprecision::cpp_int a, boost::multiprecision::cpp_int b){
if (b == 0){
return a;
} else {
return gcd(b, a % b);
}
}
struct fraction{
boost::multiprecision::cpp_int num, den;
fraction(){
}
fraction(boost::multiprecision::cpp_int a, boost::multiprecision::cpp_int b){
boost::multiprecision::cpp_int g = gcd(a, b);
num = a / g;
den = b / g;
}
fraction operator +(fraction x){
return fraction(num * x.den + x.num * den, den * x.den);
}
fraction operator -(fraction x){
return fraction(num * x.den - x.num * den, den * x.den);
}
fraction operator *(fraction x){
return fraction(num * x.num, den * x.den);
}
fraction operator /(fraction x){
return fraction(num * x.den, den * x.num);
}
};
int main(){
int N, K;
cin >> N >> K;
vector<int> A(N);
for (int i = 0; i < N; i++){
cin >> A[i];
}
K--;
if (K == N - 1){
cout << "1/1" << endl;
} else if (K == 0){
cout << 0 << endl;
} else {
vector<fraction> dp(N);
dp[0] = fraction(0, 1);
dp[1] = fraction(1, 1);
for (int i = 2; i < N; i++){
dp[i] = fraction(A[i - 2] + A[i], A[i]) * dp[i - 1] - fraction(A[i - 2], A[i]) * dp[i - 2];
}
fraction x = fraction(1, 1) / dp[N - 1];
fraction ans = x * dp[K];
cout << ans.num << "/" << ans.den << endl;
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0