結果

問題 No.1419 Power Moves
ユーザー koba-e964koba-e964
提出日時 2021-12-17 00:06:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,750 bytes
コンパイル時間 1,212 ms
コンパイル使用メモリ 145,748 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-12 00:08:19
合計ジャッジ時間 5,937 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 1 ms
4,372 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 101 ms
4,368 KB
testcase_10 AC 3 ms
4,368 KB
testcase_11 AC 16 ms
4,372 KB
testcase_12 AC 106 ms
4,372 KB
testcase_13 AC 94 ms
4,372 KB
testcase_14 AC 126 ms
4,372 KB
testcase_15 AC 125 ms
4,368 KB
testcase_16 AC 125 ms
4,372 KB
testcase_17 AC 1 ms
4,372 KB
testcase_18 AC 1 ms
4,372 KB
testcase_19 AC 1 ms
4,368 KB
testcase_20 AC 128 ms
4,368 KB
testcase_21 AC 127 ms
4,368 KB
testcase_22 AC 128 ms
4,372 KB
testcase_23 AC 129 ms
4,368 KB
testcase_24 AC 129 ms
4,372 KB
testcase_25 AC 129 ms
4,372 KB
testcase_26 AC 128 ms
4,368 KB
testcase_27 AC 129 ms
4,368 KB
testcase_28 AC 129 ms
4,368 KB
testcase_29 AC 130 ms
4,368 KB
testcase_30 AC 129 ms
4,368 KB
testcase_31 AC 128 ms
4,368 KB
testcase_32 AC 128 ms
4,368 KB
testcase_33 AC 128 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

fn powmod(x: i64, mut e: i64, m: i64) -> i64 {
    let mut sum = 1;
    let mut cur = x % m;
    while e > 0 {
        if e % 2 != 0 {
            sum = sum * cur % m;
        }
        cur = cur * cur % m;
        e /= 2;
    }
    sum
}

const MOD: i64 = 1_000_000_007;

// https://yukicoder.me/problems/no/1419 (3)
// あり得る offset とその確率は、-2^K+1 + (0 から 2^{K+1} - 2 までの偶数の一様分布に従う変数) である。
fn main() {
    let n: usize = get();
    let k: i64 = get();
    let x = powmod(2, k, n as i64);
    let base = powmod(2, MOD - 1 - k, MOD);
    let x = (n + 1 - x as usize) % n;
    let m = if n % 2 == 0 {
        n / 2
    } else {
        n
    };
    let r = powmod(2, k, m as i64);
    let q = (powmod(2, k, MOD) + MOD - r) * powmod(m as i64, MOD - 2, MOD) % MOD;
    let mut ans = vec![0; n];
    for i in 0..m {
        if i < r as usize {
            ans[(x + 2 * i) % n] = (q + 1) * base % MOD;
        } else {
            ans[(x + 2 * i) % n] = q * base % MOD;
        }
    }
    for i in 0..n {
        println!("{}", ans[i]);
    }
}
0