結果

問題 No.884 Eat and Add
ユーザー hirogahiroga
提出日時 2019-12-30 12:04:19
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 1,306 bytes
コンパイル時間 13,863 ms
コンパイル使用メモリ 407,780 KB
実行使用メモリ 65,052 KB
最終ジャッジ日時 2023-08-07 19:19:09
合計ジャッジ時間 18,596 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 332 ms
53,460 KB
testcase_01 AC 337 ms
53,516 KB
testcase_02 AC 332 ms
53,664 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://yukicoder.me/problems/no/884

package yukicoder

import java.math.BigInteger
import java.util.*

fun main() {
    val num = Scanner(System.`in`).nextBigInteger(2)
    println(no884(num))
}

fun no884(num: BigInteger): Int {
    return get2の累乗との差が0になるまで2の累乗を足すか引くかし続けた回数(num)
}

fun get2の累乗との差が0になるまで2の累乗を足すか引くかし続けた回数(num: BigInteger): Int {
    var 最寄りの2の累乗との差 = get最寄りの2の累乗との差(num)
    var count = 1

    while (最寄りの2の累乗との差 != BigInteger.valueOf(0)) {
        最寄りの2の累乗との差 = get最寄りの2の累乗との差(最寄りの2の累乗との差)
        count += 1
    }
    return count
}

fun get最寄りの2の累乗との差(num: BigInteger): BigInteger {
    val digit = num.bitLength()
    val 同桁の2の累乗数 = BigInteger("1").shiftLeft(digit - 1)
    val 同桁の2の累乗数からの差分 = 同桁の2の累乗数.xor(num)

    val i = 同桁の2の累乗数.shiftRight(1).compareTo(同桁の2の累乗数からの差分)
    return if (i > -1) {
        同桁の2の累乗数からの差分
    } else {
        同桁の2の累乗数 - 同桁の2の累乗数からの差分
    }
}

0