結果

問題 No.634 硬貨の枚数1
ユーザー まいん-
提出日時 2018-02-21 22:57:16
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 641 bytes
コンパイル時間 517 ms
コンパイル使用メモリ 62,324 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-21 07:58:28
合計ジャッジ時間 2,906 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 75
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <set>
using namespace std;

int main() {
    int N;
    cin >> N;

    set<int> triangularNumbers;
    for (int k = 1; k * (k + 1) / 2 <= N; k++) {
        triangularNumbers.insert(k * (k + 1) / 2);
    }
    // 三角数定理よりNはたかだか3つの整数の和で表すことができる
    int answer = 3;
    if (triangularNumbers.count(N) > 0) {
        answer = 1;
    } else {
        for (int n : triangularNumbers) {
            if (triangularNumbers.count(N - n) > 0) {
                answer = 2;
                break;
            }
        }
    }
    cout << answer << endl;

    return 0;
}
0