結果
問題 | No.1011 Infinite Stairs |
ユーザー | akiradeveloper |
提出日時 | 2020-03-20 23:38:11 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 1,011 ms / 2,000 ms |
コード長 | 10,973 bytes |
コンパイル時間 | 13,772 ms |
コンパイル使用メモリ | 404,984 KB |
実行使用メモリ | 16,456 KB |
最終ジャッジ日時 | 2024-07-17 11:55:38 |
合計ジャッジ時間 | 22,393 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 20 ms
6,940 KB |
testcase_03 | AC | 451 ms
9,756 KB |
testcase_04 | AC | 1,011 ms
16,220 KB |
testcase_05 | AC | 1,009 ms
16,272 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 118 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 4 ms
6,940 KB |
testcase_10 | AC | 91 ms
6,944 KB |
testcase_11 | AC | 436 ms
9,176 KB |
testcase_12 | AC | 43 ms
6,940 KB |
testcase_13 | AC | 438 ms
9,552 KB |
testcase_14 | AC | 27 ms
6,944 KB |
testcase_15 | AC | 121 ms
6,944 KB |
testcase_16 | AC | 991 ms
16,456 KB |
testcase_17 | AC | 557 ms
10,660 KB |
testcase_18 | AC | 265 ms
6,944 KB |
testcase_19 | AC | 12 ms
6,940 KB |
testcase_20 | AC | 98 ms
6,944 KB |
testcase_21 | AC | 201 ms
6,944 KB |
testcase_22 | AC | 605 ms
10,244 KB |
testcase_23 | AC | 269 ms
6,944 KB |
testcase_24 | AC | 207 ms
6,940 KB |
testcase_25 | AC | 223 ms
6,940 KB |
testcase_26 | AC | 124 ms
6,944 KB |
コンパイルメッセージ
warning: variable does not need to be mutable --> src/main.rs:120:9 | 120 | let mut poly = Polynomial::new(coeff, 1_000_000_007); | ----^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: unused `Result` that must be used --> src/main.rs:122:5 | 122 | writeln!(out,"{}",ans.coeff[k]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this `Result` may be an `Err` variant, which should be handled = note: `#[warn(unused_must_use)]` on by default = note: this warning originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info) warning: variable `L` should have a snake case name --> src/main.rs:275:9 | 275 | let L = x.len(); | ^ help: convert the identifier to snake case: `l` | = note: `#[warn(non_snake_case)]` on by default
ソースコード
#[doc = " https://github.com/akiradeveloper/rust-comp-snippets"] #[allow(unused_imports)] use std::cmp::{max, min, Ordering}; #[allow(unused_imports)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; #[allow(unused_imports)] use std::iter::FromIterator; #[macro_export] macro_rules ! chmax { ( $ x : expr , $ ( $ v : expr ) ,+ ) => { $ ( $ x = std :: cmp :: max ( $ x ,$ v ) ; ) + } ; } #[macro_export] macro_rules ! chmin { ( $ x : expr , $ ( $ v : expr ) ,+ ) => { $ ( $ x = std :: cmp :: min ( $ x ,$ v ) ; ) + } ; } #[macro_export] macro_rules ! max { ( $ x : expr ) => ( $ x ) ; ( $ x : expr , $ ( $ xs : expr ) ,+ ) => { std :: cmp :: max ( $ x , max ! ( $ ( $ xs ) ,+ ) ) } ; } #[macro_export] macro_rules ! min { ( $ x : expr ) => ( $ x ) ; ( $ x : expr , $ ( $ xs : expr ) ,+ ) => { std :: cmp :: min ( $ x , min ! ( $ ( $ xs ) ,+ ) ) } ; } #[macro_export] macro_rules ! dvec { ( $ t : expr ; $ len : expr ) => { vec ! [ $ t ; $ len ] } ; ( $ t : expr ; $ len : expr , $ ( $ rest : expr ) ,* ) => { vec ! [ dvec ! ( $ t ; $ ( $ rest ) ,* ) ; $ len ] } ; } #[macro_export] macro_rules ! cfor { ( ; $ ( $ rest : tt ) * ) => { cfor ! ( ( ) ; $ ( $ rest ) * ) } ; ( $ ( $ init : stmt ) ,+; ; $ ( $ rest : tt ) * ) => { cfor ! ( $ ( $ init ) ,+; ! false ; $ ( $ rest ) * ) } ; ( $ ( $ init : stmt ) ,+; $ cond : expr ; ; $ body : block ) => { cfor ! { $ ( $ init ) ,+; $ cond ; ( ) ; $ body } } ; ( $ ( $ init : stmt ) ,+; $ cond : expr ; $ ( $ step : expr ) ,+; $ body : block ) => { { $ ( $ init ; ) + while $ cond { let mut _first = true ; let mut _continue = false ; loop { if ! _first { _continue = true ; break } _first = false ; $ body } if ! _continue { break } $ ( $ step ; ) + } } } ; } #[doc = " main"] #[allow(unused_imports)] use std::io::{stdin, stdout, BufWriter, Write}; #[macro_export] macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut parser = Parser :: from_str ( $ s ) ; input_inner ! { parser , $ ( $ r ) * } } ; ( parser = $ parser : ident , $ ( $ r : tt ) * ) => { input_inner ! { $ parser , $ ( $ r ) * } } ; ( new_stdin_parser = $ parser : ident , $ ( $ r : tt ) * ) => { let stdin = std :: io :: stdin ( ) ; let reader = std :: io :: BufReader :: new ( stdin . lock ( ) ) ; let mut $ parser = Parser :: new ( reader ) ; input_inner ! { $ parser , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { input ! { new_stdin_parser = parser , $ ( $ r ) * } } ; } #[macro_export] macro_rules ! input_inner { ( $ parser : ident ) => { } ; ( $ parser : ident , ) => { } ; ( $ parser : ident , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ parser , $ t ) ; input_inner ! { $ parser $ ( $ r ) * } } ; } #[macro_export] macro_rules ! read_value { ( $ parser : ident , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ parser , $ t ) ) ,* ) } ; ( $ parser : ident , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ parser , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ parser : ident , chars ) => { read_value ! ( $ parser , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ parser : ident , usize1 ) => { read_value ! ( $ parser , usize ) - 1 } ; ( $ parser : ident , $ t : ty ) => { $ parser . next ::<$ t > ( ) . expect ( "Parse error" ) } ; } use std::io; use std::io::BufRead; use std::str; pub struct Parser<R> { reader: R, buf: Vec<u8>, pos: usize, } impl Parser<io::Empty> { pub fn from_str(s: &str) -> Parser<io::Empty> { Parser { reader: io::empty(), buf: s.as_bytes().to_vec(), pos: 0, } } } impl<R: BufRead> Parser<R> { pub fn new(reader: R) -> Parser<R> { Parser { reader: reader, buf: vec![], pos: 0, } } pub fn update_buf(&mut self) { self.buf.clear(); self.pos = 0; loop { let (len, complete) = { let buf2 = self.reader.fill_buf().unwrap(); self.buf.extend_from_slice(buf2); let len = buf2.len(); if len == 0 { break; } (len, buf2[len - 1] <= 0x20) }; self.reader.consume(len); if complete { break; } } } pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> { loop { let mut begin = self.pos; while begin < self.buf.len() && (self.buf[begin] <= 0x20) { begin += 1; } let mut end = begin; while end < self.buf.len() && (self.buf[end] > 0x20) { end += 1; } if begin != self.buf.len() { self.pos = end; return str::from_utf8(&self.buf[begin..end]).unwrap().parse::<T>(); } else { self.update_buf(); } } } } #[allow(unused_macros)] macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } } #[doc = " https://github.com/hatoo/competitive-rust-snippets"] const BIG_STACK_SIZE: bool = true; #[allow(dead_code)] fn main() { use std::thread; if BIG_STACK_SIZE { thread::Builder::new() .stack_size(32 * 1024 * 1024) .name("solve".into()) .spawn(solve) .unwrap() .join() .unwrap(); } else { solve(); } } fn solve() { let out = stdout(); let mut out = BufWriter::new(out.lock()); input!{ n:i64,d:usize,k:usize, } // (x^1+...+x^d)^n let mut coeff = vec![1;d+1]; coeff[0] = 0; let mut poly = Polynomial::new(coeff, 1_000_000_007); let ans = poly.pow(n); writeln!(out,"{}",ans.coeff[k]); } #[derive(Debug)] struct Polynomial { coeff: Vec<i64>, mo: i64, } impl Polynomial { pub fn new(coeff: Vec<i64>, mo: i64) -> Polynomial { Polynomial { coeff: coeff, mo: mo, } } pub fn multiply(&self, other: &Self) -> Self { assert!(self.mo == other.mo); Polynomial { coeff: ntt_multiply(&self.coeff, &other.coeff, self.mo), mo: self.mo, } } pub fn pow(&self, k: i64) -> Self { let mut res = Polynomial { coeff: vec![1], mo: self.mo, }; let mut x = Polynomial { coeff: self.coeff.clone(), mo: self.mo, }; let mut k = k; while k > 0 { if k & 1 == 1 { res = res.multiply(&x); } x = x.multiply(&x); k >>= 1; } res } } struct NTT { pub mo: i64, } impl NTT { pub fn new(mo: i64) -> NTT { NTT { mo: mo } } fn _ntt(&self, a: &mut [i64], n: usize, inverse: bool) { let g = 3; let mut h = modpow(g, (self.mo - 1) / n as i64, self.mo); if inverse { h = modinv(h, self.mo); } let mut i = 0; for j in 1..n - 1 { let mut k = n >> 1; loop { i ^= k; if k > i { k >>= 1; } else { break; } } if j < i { let tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } let mut m = 1; while m < n { let m2 = m * 2; let base = modpow(h, (n / m2) as i64, self.mo); let mut w = 1; for x in 0..m { let mut s = x; while s < n { let u = a[s]; let d = (a[s + m] * w) % self.mo; a[s] = u + d; if a[s] >= self.mo { a[s] -= self.mo; } a[s + m] = u - d; if a[s + m] < 0 { a[s + m] += self.mo; } s += m2; } w = (w * base) % self.mo; } m *= 2; } for i in 0..n { if a[i] < 0 { a[i] += self.mo; } } } fn ntt(&self, a: &mut [i64], n: usize) { self._ntt(a, n, false); } fn intt(&self, a: &mut [i64], n: usize) { self._ntt(a, n, true); let n_inv = modinv(a.len() as i64, self.mo); for i in 0..n { a[i] = (a[i] * n_inv) % self.mo; } } pub fn convolve(&self, a: &[i64], b: &[i64]) -> Vec<i64> { let mut a = a.to_vec(); let mut b = b.to_vec(); let mut n = 1; while n < a.len() + b.len() { n <<= 1; } a.resize(n, 0); b.resize(n, 0); self.ntt(&mut a, n); self.ntt(&mut b, n); let mut c = vec![0; n]; for i in 0..n { c[i] = (a[i] * b[i]) % self.mo; } self.intt(&mut c, n); c } } pub fn ntt_multiply(a: &[i64], b: &[i64], mo: i64) -> Vec<i64> { let mut a = a.to_vec(); let mut b = b.to_vec(); let n = a.len(); let m = b.len(); for i in 0..n { a[i] %= mo; } for i in 0..m { b[i] %= mo; } let ntt1 = NTT::new(167772161); let ntt2 = NTT::new(469762049); let ntt3 = NTT::new(1224736769); let x = ntt1.convolve(&a, &b); let y = ntt2.convolve(&a, &b); let z = ntt3.convolve(&a, &b); let m1 = ntt1.mo; let m2 = ntt2.mo; let m3 = ntt3.mo; let m1_inv_m2 = modinv(m1, m2); let m12_inv_m3 = modinv(m1 * m2, m3); let m12_mod = (m1 * m2) % mo; let L = x.len(); let mut res = vec![0; L]; for i in 0..L { let mut v1 = (y[i] - x[i]) * m1_inv_m2; v1 %= m2; if v1 < 0 { v1 += m2; } let mut v2 = (z[i] - (x[i] + m1 * v1) % m3) * m12_inv_m3; v2 %= m3; if v2 < 0 { v2 += m3; } let mut const3 = (x[i] + m1 * v1 + m12_mod * v2) % mo; if const3 < 0 { const3 += mo; } res[i] = const3; } res.truncate(n + m - 1); res } #[doc = "ay=1 (mod m) -> y=a^{-1}"] pub fn modinv(a: i64, m: i64) -> i64 { let (_, x, _) = extgcd(a, m); (m + x % m) % m } #[allow(dead_code)] #[doc = " x ^ n % m"] pub fn modpow(x: i64, n: i64, m: i64) -> i64 { let mut res = 1; let mut x = x % m; let mut n = n; while n > 0 { if n & 1 == 1 { res = (res * x) % m; } x = (x * x) % m; n >>= 1; } res } #[doc = "ax+by=gcd(a,b) returns (gcd, x, y)"] #[allow(dead_code)] pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) { if b == 0 { (a, 1, 0) } else { let (gcd, x, y) = extgcd(b, a % b); (gcd, y, x - (a / b) * y) } }