結果

問題 No.1632 Sorting Integers (GCD of M)
ユーザー akakimidoriakakimidori
提出日時 2021-07-30 20:52:56
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 3,218 bytes
コンパイル時間 16,941 ms
コンパイル使用メモリ 389,424 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-21 17:29:33
合計ジャッジ時間 16,823 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 1 ms
6,816 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 0 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 1 ms
6,944 KB
testcase_30 AC 1 ms
6,944 KB
testcase_31 AC 1 ms
6,940 KB
testcase_32 AC 1 ms
6,944 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 AC 1 ms
6,940 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 1 ms
6,944 KB
testcase_38 AC 1 ms
6,944 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 1 ms
6,940 KB
testcase_41 AC 1 ms
6,940 KB
testcase_42 AC 1 ms
6,944 KB
testcase_43 AC 1 ms
6,944 KB
testcase_44 AC 1 ms
6,940 KB
testcase_45 AC 1 ms
6,940 KB
testcase_46 AC 1 ms
6,940 KB
testcase_47 AC 1 ms
6,940 KB
testcase_48 AC 1 ms
6,940 KB
testcase_49 AC 1 ms
6,940 KB
testcase_50 AC 1 ms
6,940 KB
testcase_51 AC 1 ms
6,940 KB
testcase_52 AC 1 ms
6,940 KB
testcase_53 AC 1 ms
6,944 KB
testcase_54 AC 1 ms
6,940 KB
testcase_55 AC 1 ms
6,940 KB
testcase_56 AC 1 ms
6,944 KB
testcase_57 AC 1 ms
6,940 KB
testcase_58 AC 1 ms
6,940 KB
testcase_59 AC 1 ms
6,940 KB
testcase_60 AC 1 ms
6,940 KB
testcase_61 AC 1 ms
6,940 KB
testcase_62 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::io::Write`
  --> src/main.rs:70:5
   |
70 | use std::io::Write;
   |     ^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: unused variable: `n`
   --> src/main.rs:103:9
    |
103 |         n: u64,
    |         ^
    |
help: `n` is captured in macro and introduced a unused variable
   --> src/main.rs:43:13
    |
43  |           let $var = read_value!($iter, $t);
    |               ^^^^
...
102 | /     input! {
103 | |         n: u64,
104 | |         c: [u64; 9],
105 | |     }
    | |_____- in this macro invocation
    = note: `#[warn(unused_variables)]` on by default
    = note: this warning originates in the macro `input_inner` which comes from the expansion of the macro `input` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: type alias `Map` is never used
  --> src/main.rs:73:6
   |
73 | type Map<K, V> = BTreeMap<K, V>;
   |      ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
  --> src/main.rs:74:6
   |
74 | type Set<T> = BTreeSet<T>;
   |      ^^^

warning: type alias `Deque` is never used
  --> src/main.rs:75:6
   |
75 | type Deque<T> = VecDeque<T>;
   |      ^^^^^

ソースコード

diff #

// ---------- begin binary_gcd ----------
pub fn binary_gcd(a: u64, b: u64) -> u64 {
    if a == 0 || b == 0 {
        return a + b;
    }
    let x = a.trailing_zeros();
    let y = b.trailing_zeros();
    let mut a = a >> x;
    let mut b = b >> y;
    while a != b {
        let x = (a ^ b).trailing_zeros();
        if a < b {
            std::mem::swap(&mut a, &mut b);
        }
        a = (a - b) >> x;
    }
    a << x.min(y)
}
// ---------- end binary_gcd ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

const MOD: u64 = 1_000_000_007;

fn calc(r: u64, n: u64, m: u64) -> u64 {
    if n == 1 {
        1 % m
    } else if n % 2 == 0 {
        (1 + r) * calc(r * r % m, n / 2, m) % m
    } else {
        (1 + r * calc(r, n - 1, m)) % m
    }
}

fn pow_mod(mut r: u64, mut n: u64, m: u64) -> u64 {
    let mut t = 1 % m;
    while n > 0 {
        if n & 1 == 1 {
            t = t * r % m;
        }
        r = r * r % m;
        n >>= 1;
    }
    t
}

fn run() {
    input! {
        n: u64,
        c: [u64; 9],
    }
    let mut g = 0;
    for (i, a) in c.iter().enumerate() {
        for (j, b) in c.iter().enumerate().take(i) {
            if *a > 0 && *b > 0 {
                g = binary_gcd(g, 9 * (i - j) as u64);
            }
        }
    }
    let ans = if g == 0 {
        let x = c.iter().position(|c| *c > 0).unwrap();
        (x + 1) as u64 * calc(10, c[x], MOD) % MOD
    } else {
        let mut val = 0;
        for (i, c) in c.iter().enumerate() {
            if *c > 0 {
                val = (val * pow_mod(10, *c, g) + calc(10, *c, g) * (i + 1) as u64) % g;
            }
        }
        binary_gcd(val, g)
    };
    println!("{}", ans);
}

fn main() {
    run();
}
0