結果

問題 No.648  お や す み 
ユーザー denderaKawazu
提出日時 2018-02-09 23:49:05
言語 Java
(openjdk 23)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,503 bytes
コンパイル時間 2,070 ms
コンパイル使用メモリ 76,996 KB
実行使用メモリ 42,060 KB
最終ジャッジ日時 2024-09-13 21:05:15
合計ジャッジ時間 15,415 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 84
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            long n = in.nextLong();
            long maxN = n;
            long minN = 0;
            long cn = -1;

            if (n == 1) {
                out.println("YES");
                out.println(1);
                return;
            }

            while ((maxN + minN) / 2 != cn) {
                cn = (maxN + minN) / 2;
                double sum = (double) cn * ((double) cn + 1) / 2;
                if (sum == n) {
                    break;
                } else if (sum > n) {
                    maxN = cn;
                } else if (sum < n) {
                    minN = cn;
                }
            }

            if (cn * (cn + 1) / 2 == n) {
                out.println("YES");
                out.println(cn);
            } else {
                out.println("NO");
            }
        }

    }
}

0