結果

問題 No.8111 April Fool 2024 in Pi
ユーザー greentea011
提出日時 2025-04-03 16:04:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,619 bytes
コンパイル時間 974 ms
コンパイル使用メモリ 71,536 KB
実行使用メモリ 19,520 KB
最終ジャッジ日時 2025-04-03 16:04:31
合計ジャッジ時間 7,514 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cmath>
#include <sstream>

// BBP公式を使用して円周率を計算する
std::string calculatePi(int precision) {
    long double pi = 0.0;

    // BBP公式で円周率を計算
    for (int k = 0; k < precision; ++k) {
        long double term = (4.0 / (8 * k + 1))
                         - (2.0 / (8 * k + 4))
                         - (1.0 / (8 * k + 5))
                         - (1.0 / (8 * k + 6));
        term /= std::pow(16, k);
        pi += term;
    }

    // 小数部分を返す (整数部分を除外)
    std::ostringstream oss;
    oss.precision(precision + 1);
    oss << std::fixed << pi;

    std::string piStr = oss.str();
    size_t dotIndex = piStr.find('.');
    if (dotIndex != std::string::npos) {
        return piStr.substr(dotIndex + 1); // 小数点以下部分
    }
    return "";
}

int main() {
    const std::string TARGET = "20240401"; // 探す文字列
    const int PRECISION = 43322750;        // 円周率の計算精度(小数点以下桁数)

    std::cout << "計算中です。しばらくお待ちください...\n";

    // 円周率を計算して文字列として取得
    std::string piDecimal = calculatePi(PRECISION);

    // ターゲット文字列を検索
    size_t position = piDecimal.find(TARGET);

    if (position != std::string::npos) {
        // 見つかった場合、位置を1加算して出力(1始まり)
        std::cout << position + 1 << std::endl;
    } else {
        // 見つからない場合は -1 を出力
        std::cout << -1 << std::endl;
    }

    return 0;
}
0