結果

問題 No.1339 循環小数
ユーザー StrorkisStrorkis
提出日時 2021-01-16 00:57:25
言語 Rust
(1.77.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,752 bytes
コンパイル時間 5,453 ms
コンパイル使用メモリ 157,112 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-17 19:28:09
合計ジャッジ時間 4,757 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 14 ms
4,376 KB
testcase_22 AC 15 ms
4,376 KB
testcase_23 AC 15 ms
4,376 KB
testcase_24 AC 13 ms
4,380 KB
testcase_25 AC 14 ms
4,380 KB
testcase_26 AC 14 ms
4,376 KB
testcase_27 AC 15 ms
4,376 KB
testcase_28 AC 14 ms
4,380 KB
testcase_29 AC 13 ms
4,380 KB
testcase_30 AC 13 ms
4,380 KB
testcase_31 AC 28 ms
4,380 KB
testcase_32 AC 28 ms
4,380 KB
testcase_33 AC 15 ms
4,380 KB
testcase_34 AC 7 ms
4,376 KB
testcase_35 AC 25 ms
4,380 KB
testcase_36 AC 14 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{Read, Write};

struct Scanner<R> { bytes: std::io::Bytes<R>, buf: Vec<u8> }
 
impl<R: Read> Scanner<R> {
    fn new(r: R) -> Scanner<R> {
        Scanner { bytes: r.bytes(), buf: Vec::new() }
    }
  
    fn next(&mut self) -> &str {
        self.buf = {
            self.bytes.by_ref().map(|b| b.unwrap())
                .skip_while(|b| b.is_ascii_whitespace())
                .take_while(|b| !b.is_ascii_whitespace())
                .collect::<Vec<_>>()
        };
        unsafe { std::str::from_utf8_unchecked(&self.buf) }
    }
}

macro_rules! scan {
    ($sc:expr, [$t:tt; $n:expr]) => (
        (0..$n).map(|_| scan!($sc, $t)).collect::<Vec<_>>()
    );
    ($sc:expr, ($($t:tt),*)) => (($(scan!($sc, $t)),*));
    ($sc:expr, Usize1) => (scan!($sc, usize) - 1);
    ($sc:expr, Bytes) => ($sc.next().bytes().collect::<Vec<_>>());
    ($sc:expr, Chars) => ($sc.next().chars().collect::<Vec<_>>());
    ($sc:expr, $t:ty) => ($sc.next().parse::<$t>().unwrap());
}

fn prime_factorize(mut n: usize) -> Vec<(usize, usize)> {
    let mut res = Vec::new();
    for i in 2.. {
        if i * i > n { break; }
        let mut cnt = 0;
        while n % i == 0 {
            cnt += 1;
            n /= i;
        }
        if cnt > 0 { res.push((i, cnt)); }
    }
    if n > 1 { res.push((n, 1)); }
    res
}

fn euler_f(n: usize) -> usize {
    prime_factorize(n).iter().fold(n, |acc, (i, _)| acc * (i - 1) / i)
}

fn get_divisors(n: usize) -> Vec<usize> {
    let (mut a, mut b) = (Vec::new(), Vec::new());
    for i in (1..).take_while(|i| i * i <= n) {
        if n % i > 0 { continue; }
        a.push(i);
        let j = n / i;
        if j == i { continue; }
        b.push(j);
    }
    a.into_iter().chain(b.into_iter().rev()).collect::<Vec<_>>()
}

fn run<R: Read, W: Write>(mut sc: Scanner<R>, mut wr: W) {
    let pow_mod = |mut a: usize, mut n: usize, m: usize| -> usize {
        let mut res = 1;
        while n > 0 {
            if n & 1 > 0 {
                res = res * a % m;
            }
            a = a * a % m;
            n >>= 1;
        }
        res
    };

    for _ in 0..scan!(sc, usize) {
        let mut n = scan!(sc, usize);
        while n % 2 == 0 { n /= 2; }
        while n % 5 == 0 { n /= 5; }
        if n == 1 {
            writeln!(wr, "1").ok();
            continue;
        }
        let m = euler_f(n);
        for i in get_divisors(m) {
            if pow_mod(10, i, n) == 1 {
                writeln!(wr, "{}", i).ok();
                break;
            }
        }
    }
}

fn main() {
    let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
    let sc = Scanner::new(std::io::BufReader::new(stdin.lock()));
    let wr = std::io::BufWriter::new(stdout.lock());
    run(sc, wr);
}
0