結果

問題 No.890 移調の限られた旋法
ユーザー cotton_fn_cotton_fn_
提出日時 2020-02-09 12:01:01
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,865 bytes
コンパイル時間 1,124 ms
コンパイル使用メモリ 188,252 KB
実行使用メモリ 17,552 KB
最終ジャッジ日時 2024-04-08 12:05:36
合計ジャッジ時間 3,496 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 61 ms
17,552 KB
testcase_14 AC 21 ms
9,728 KB
testcase_15 WA -
testcase_16 AC 37 ms
13,576 KB
testcase_17 AC 23 ms
9,616 KB
testcase_18 AC 28 ms
11,200 KB
testcase_19 AC 20 ms
8,068 KB
testcase_20 AC 10 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 17 ms
7,896 KB
testcase_23 AC 19 ms
9,252 KB
testcase_24 AC 13 ms
6,676 KB
testcase_25 AC 4 ms
6,676 KB
testcase_26 AC 20 ms
9,404 KB
testcase_27 AC 20 ms
9,532 KB
testcase_28 AC 14 ms
6,804 KB
testcase_29 AC 10 ms
6,676 KB
testcase_30 AC 19 ms
9,168 KB
testcase_31 AC 20 ms
7,848 KB
testcase_32 WA -
testcase_33 AC 57 ms
15,676 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused)]

use std::io::{self, BufRead, Write};

pub struct Input<R> {
    src: R,
    buf: Vec<u8>,
    pos: usize,
}

impl<R: BufRead> Input<R> {
    pub fn new(src: R) -> Self {
        Self {
            src,
            buf: Vec::new(),
            pos: 0,
        }
    }

    pub fn input_bytes(&mut self) -> &[u8] {
        loop {
            self.advance_until(|b| b.is_ascii_whitespace());
            if self.pos == self.buf.len() {
                self.buf.clear();
                self.src
                    .read_until(b'\n', &mut self.buf)
                    .expect("input error");
                self.pos = 0;
            } else {
                break;
            }
        }
        let l = self.pos;
        self.advance_until(|b| !b.is_ascii_whitespace());
        &self.buf[l..self.pos]
    }

    fn advance_until(&mut self, f: impl Fn(u8) -> bool) {
        while let Some(b) = self.buf.get(self.pos) {
            if !f(*b) {
                break;
            }
            self.pos += 1;
        }
    }

    pub fn input<T: std::str::FromStr>(&mut self) -> T
    where
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        String::from_utf8_lossy(self.input_bytes())
            .parse()
            .expect("parse error")
    }
}

pub fn mod_pow(a: i64, b: i64, m: i64) -> i64 {
    let mut b = b;
    let mut x = a;
    let mut y = 1;
    while b > 0 {
        if b & 1 == 1 {
            y = y * x % m;
        }
        x = x * x % m;
        b >>= 1;
    }
    y
}

pub fn mod_inv(x: i64, m: i64) -> i64 {
    mod_pow(x, m - 2, m)
}

struct Combination {
    m: i64,
    fact: Vec<i64>,
}

impl Combination {
    pub fn new(size: usize, m: i64) -> Self {
        let mut comb = Self {
            m,
            fact: Vec::new(),
        };
        comb.resize(size);
        comb
    }

    pub fn resize(&mut self, size: usize) {
        let old_len = self.fact.len();
        self.fact.resize(size + 1, 0);
        for i in old_len..size + 1 {
            self.fact[i] = if i == 0 {
                1
            } else {
                i as i64 * self.fact[i - 1] % self.m
            };
        }
    }

    pub fn fact(&self, x: i64) -> i64 {
        self.fact[x as usize]
    }

    pub fn perm(&self, a: i64, b: i64) -> i64 {
        if a >= b {
            self.fact(a) * mod_inv(self.fact(a - b), self.m) % self.m
        } else {
            0
        }
    }

    pub fn comb(&self, a: i64, b: i64) -> i64 {
        if a >= b {
            self.fact(a) * mod_inv(self.fact(b) * self.fact(a - b) % self.m, self.m) % self.m
        } else {
            0
        }
    }
}

pub fn gcd(x: i64, y: i64) -> i64 {
    if y > 0 {
        gcd(y, x % y)
    } else {
        x
    }
}

fn main() {
    let stdin = io::stdin();
    let mut input = Input::new(stdin.lock());
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    let stderr = io::stderr();
    let mut err = io::BufWriter::new(stderr.lock());

    const MOD: i64 = 1e9 as i64 + 7;

    let n = input.input();
    let k = input.input();
    let g = gcd(n, k);

    let mut mebious = vec![-2; g as usize + 1];
    mebious[0] = 0;
    mebious[1] = 0;
    for x in 2..=g as usize {
        if mebious[x] == -2 {
            mebious[x] = -1;
            for d in (2 * x..=g as usize).step_by(x) {
                mebious[d] = if d % (x * x) == 0 {
                    0
                } else if mebious[d] == -2 {
                    -1
                } else {
                    -mebious[d]
                }
            }
        }
    }
    
    let mut ans = 0;
    let comb = Combination::new(n as usize, MOD);
    for d in 2..=g {
        if g % d == 0 {
            ans += -mebious[d as usize] * comb.comb(n / d, k / d) % MOD;
        }
    }
    ans %= MOD;
    writeln!(&mut out, "{}", ans);
}
0