結果

問題 No.1409 Simple Math in yukicoder
ユーザー akakimidoriakakimidori
提出日時 2021-02-20 12:41:14
言語 Rust
(1.77.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 2,008 bytes
コンパイル時間 1,690 ms
コンパイル使用メモリ 182,304 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 23:23:18
合計ジャッジ時間 28,687 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 14 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 11 ms
4,348 KB
testcase_11 AC 14 ms
4,348 KB
testcase_12 AC 10 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 6 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 18 ms
4,348 KB
testcase_18 AC 9 ms
4,348 KB
testcase_19 AC 10 ms
4,348 KB
testcase_20 AC 8 ms
4,348 KB
testcase_21 AC 8 ms
4,348 KB
testcase_22 AC 12 ms
4,348 KB
testcase_23 AC 4 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 11 ms
4,348 KB
testcase_26 AC 12 ms
4,348 KB
testcase_27 AC 20 ms
4,348 KB
testcase_28 AC 19 ms
4,348 KB
testcase_29 AC 20 ms
4,348 KB
testcase_30 AC 20 ms
4,348 KB
testcase_31 AC 20 ms
4,348 KB
testcase_32 AC 20 ms
4,348 KB
testcase_33 AC 20 ms
4,348 KB
testcase_34 AC 19 ms
4,348 KB
testcase_35 AC 20 ms
4,348 KB
testcase_36 AC 20 ms
4,348 KB
testcase_37 AC 47 ms
4,348 KB
testcase_38 AC 46 ms
4,348 KB
testcase_39 AC 46 ms
4,348 KB
testcase_40 AC 47 ms
4,348 KB
testcase_41 AC 46 ms
4,348 KB
testcase_42 AC 46 ms
4,348 KB
testcase_43 AC 46 ms
4,348 KB
testcase_44 AC 46 ms
4,348 KB
testcase_45 AC 47 ms
4,348 KB
testcase_46 AC 47 ms
4,348 KB
testcase_47 AC 46 ms
4,348 KB
testcase_48 AC 46 ms
4,348 KB
testcase_49 AC 47 ms
4,348 KB
testcase_50 AC 46 ms
4,348 KB
testcase_51 AC 46 ms
4,348 KB
testcase_52 AC 46 ms
4,348 KB
testcase_53 AC 46 ms
4,348 KB
testcase_54 AC 47 ms
4,348 KB
testcase_55 AC 46 ms
4,348 KB
testcase_56 AC 46 ms
4,348 KB
testcase_57 AC 50 ms
4,348 KB
testcase_58 AC 51 ms
4,348 KB
testcase_59 AC 50 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn read() -> Vec<(u64, u64)> {
    let mut s = String::new();
    use std::io::Read;
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let t: usize = it.next().unwrap().parse().unwrap();
    assert!(t <= 1000);
    let mut ask = vec![(0u64, 0u64); t];
    for p in ask.iter_mut() {
        p.0 = it.next().unwrap().parse().unwrap();
        p.1 = it.next().unwrap().parse().unwrap();
        assert!(1 <= p.0 && p.0 <= 1000);
        assert!(1 <= p.1 && p.1 <= 1000);
        assert!(is_prime(p.0 * p.1 + 1));
    }
    ask
}

fn is_prime(n: u64) -> bool {
    (2..).take_while(|p| p * p <= n).all(|p| n % p != 0)
}

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

fn main() {
    let ask = read();
    for (v, x) in ask {
        let p = v * x + 1;
        if p == 2 {
            println!("1");
            continue;
        }
        let mut phi = p - 1;
        let mut f = vec![];
        for k in 2.. {
            if k * k > phi {
                break;
            }
            if phi % k == 0 {
                f.push(k);
                while phi % k == 0 {
                    phi /= k;
                }
            }
        }
        if phi > 1 {
            f.push(phi);
        }
        let phi = p - 1;
        for z in 2.. {
            let mut ok = true;
            for f in f.iter() {
                ok &= pow(z, phi / *f, p) > 1;
            }
            if ok {
                let mut ans = (0..x).map(|i| pow(z, i * v, p)).collect::<Vec<_>>();
                ans.sort();
                for (i, a) in ans.iter().enumerate() {
                    if i > 0 {
                        print!(" ");
                    }
                    print!("{}", *a);
                }
                println!();
                break;
            }
        }
    }
}
0