結果
| 問題 |
No.65 回数の期待値の練習
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-03-28 19:37:06 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,379 bytes |
| コンパイル時間 | 7,563 ms |
| コンパイル使用メモリ | 205,056 KB |
| 実行使用メモリ | 5,888 KB |
| 最終ジャッジ日時 | 2025-03-28 19:37:15 |
| 合計ジャッジ時間 | 6,542 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 3 WA * 13 |
コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`
ソースコード
module main;
import std;
// 有理数の例外クラス
class RationalException : Exception {
this(string s)
{
super("Rational:" ~ s);
}
}
// 有理数の構造体
struct Rational(I)
if (__traits(compiles, {I a = -1;})) {
/*
* メンバ変数
*/
I _n = 0; // 分子
I _d = 1; // 分母
@property
{
I numerator() { return _n; }
I numerator(I n) { return _n = n; }
I denominator() { return _d; }
I denominator(I d) { return _d = d; }
}
/*
* コンストラクタ
*/
this()(I n)
{
_n = n;
}
this()(I n, I d)
{
_n = n;
_d = d;
normalize();
}
this(U)(in ref U a)
if (is(Unqual!U == Rational))
{
this(a._n, a._d);
}
// 約分(正規化)する
void normalize()
{
if (_n == 0) {
_d = 1;
return;
}
if (_d < 0) {
_n = -_n;
_d = -_d;
}
try {
I g = gcd(_n, _d);
_n /= g;
_d /= g;
} catch (Exception e) {
writeln("_n = ", _n, ", _d = ", _d);
throw new RationalException("normalize failed");
}
}
/*
* 等号
*/
bool opEquals(U)(in ref U a) const
if (is(Unqual!U == Rational))
{
return _n == a._n && _d == a._d;
}
bool opEquals(U)(U a) const
if (isNumeric!U)
{
return _n == a * _d;
}
/*
* 代入演算子
*/
ref Rational opAssign(U)(U a)
if (is(U : I))
{
_n = a;
_d = 1;
return this;
}
ref Rational opAssign(U)(U a)
if (is(Unqual!U == Rational))
{
_n = a._n;
_d = a._d;
return this;
}
/*
* 演算代入演算子
*/
ref Rational opOpAssign(string op, U)(U r)
if (is(U : I))
{
static if (op == "+" || op == "-") {
mixin("_n " ~ op ~ "= r * _d;");
} else static if (op == "*") {
_n *= r;
} else static if (op == "/") {
if (r == 0) throw new RationalException("divided by zero");
_n *= std.math.sgn(r);
_d *= std.math.abs(r);
} else static if (op == "^^") {
if (r < 0) {
r = -r;
I n = _n;
_n = std.math.sgn(n) * _d;
_d = std.math.abs(n);
}
_n ^^= r;
_d ^^= r;
}
normalize();
return this;
}
ref Rational opOpAssign(string op, U)(U r)
if (is(Unqual!U == Rational))
{
static if (op == "+" || op == "-") {
mixin("_n = _n * r._d " ~ op ~ " _d * r._n;");
_d *= r._d;
} else static if (op == "*") {
_n *= r._n;
_d *= r._d;
} else static if (op == "/") {
if (r == 0) throw new RationalException("divided by zero");
_n *= r._d;
_d *= r._n;
}
normalize();
return this;
}
/*
* 二項演算子
*/
Rational opBinary(string op, U)(U r) const
if (op == "+" || op == "-" || op == "*" || op == "/" || op == "^^")
{
auto w = Rational(this);
return w.opOpAssign!op(r);
}
Rational opBinaryRight(string op, U)(U r) const
if (is(U : I))
{
static if (op == "+" || op == "*") {
return opBinary!op(r);
} else static if (op == "-") {
return -this + r;
} else static if (op == "/") {
if (this == 0) throw new RationalException("divided by zero");
auto r1 = Rational(r);
r1._n *= _d;
r1._d *= _n;
r1.normalize();
return r1;
} else {
assert(0);
}
}
/*
* キャスト演算子
*/
U opCast(U)() const
if (isNumeric!U)
{
U n = cast(U)_n, d = cast(U)_d;
return n / d;
}
}
void main()
{
// 入力
auto K = readln.chomp.to!int;
// 答えの計算と出力
alias R = Rational!long;
auto E = R(0L).repeat(21).array;
foreach_reverse (i; 0 .. K) {
E[i] = 1 + E[i + 1 .. K + 1].sum / 6;
}
writeln(cast(double)E[0]);
}