結果

問題 No.642 Two Operations No.1
ユーザー @abcde@abcde
提出日時 2019-03-11 23:16:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 944 bytes
コンパイル時間 1,556 ms
コンパイル使用メモリ 142,668 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 20:17:55
合計ジャッジ時間 1,955 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main() {
    
    // 1. 入力情報取得.
    LL N;
    cin >> N;
    
    // 2. N から スタートして, 操作1, 操作2 の逆 を考えてみる.
    // -> N が 偶数: 1/2倍, N が 奇数: 1加算.
    LL ans = 0;
    while(N > 1){
        if(N % 2 == 0) N /= 2; // 操作2 の 逆.
        else           N++;    // 操作1 の 逆.
        ans++;
    }
    
    // 3. 出力 ~ 後処理.
    // N = 1000000000 だと, 39回?
    // 1 -> 2 -> 4 -> 8 -> 16 -> 15 -> 30 -> 60 -> 120 -> 240 -> 239
    // -> 478 -> 477 -> 954 -> 1908 -> 3816 -> 3815 -> 7630 -> 15260 -> 15259 -> 30518
    // -> 61036 -> 122072 -> 122071 -> 244142 -> 244141 -> 488282 -> 976564 -> 976563 -> 1953126 -> 1953125
    // -> 3906250 -> 7812500 -> 15625000 -> 31250000 -> 62500000 -> 125000000 -> 250000000 -> 500000000 -> 1000000000
    cout << ans << endl;
    return 0;
    
}
0