Untitled
unknown
plain_text
a year ago
3.9 kB
6
Indexable
use std::io; use std::str; pub struct Scanner<R> { reader: R, buffer: Vec<String>, } impl<R: io::BufRead> Scanner<R> { pub fn new(reader: R) -> Self { Self { reader, buffer: vec![], } } pub fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buffer.pop() { return token.parse().ok().expect("Failed parse"); } let mut input = String::new(); self.reader.read_line(&mut input).expect("Failed read"); self.buffer = input.split_whitespace().rev().map(String::from).collect(); } } } /// Same API as Scanner but nearly twice as fast, using horribly unsafe dark arts pub struct UnsafeScanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: str::SplitAsciiWhitespace<'static>, } impl<R: io::BufRead> UnsafeScanner<R> { pub fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_ascii_whitespace(), } } /// This function should be marked unsafe, but noone has time for that in a /// programming contest. Use at your own risk! pub fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_ascii_whitespace()) } } } } //fn unsafe_solve<R: io::BufRead, W: io::Write>(scan: &mut UnsafeScanner<R>, out: &mut W) { // let x = scan.token::<i32>(); // let y = scan.token::<i32>(); // writeln!(out, "{} - {} = {}", x, y, x - y).ok(); //} struct Combinatorics { array: Vec<i64>, modulus: i64, } //TODO:Precompute inverse factorials to for optimizations impl Combinatorics { fn new(size: usize, modulus: i64) -> Combinatorics { let mut array: Vec<i64> = vec![1; size + 1]; for i in 2..=size { array[i] = (array[i - 1] * i as i64) % modulus; } Combinatorics { array, modulus } } fn get_factorial(&self, num: usize) -> i64 { self.array[num] } fn inverse(&self, a: i64) -> i64 { self.power(a, self.modulus - 2) } fn add(&self, a: i64, b: i64) -> i64 { (a % self.modulus + b % self.modulus) % self.modulus } fn multiply(&self, a: i64, b: i64) -> i64 { (a % self.modulus * b % self.modulus) % self.modulus } fn subtract(&self, a: i64, b: i64) -> i64 { (a % self.modulus - b % self.modulus + self.modulus) % self.modulus } fn divide(&self, a: i64, b: i64) -> i64 { (a % self.modulus * self.inverse(b) % self.modulus) % self.modulus } fn power(&self, mut a: i64, mut p: i64) -> i64 { let mut res = 1; while p > 0 { if p & 1 == 1 { res = self.multiply(res, a); } a = self.multiply(a, a); p >>= 1; } res } fn nCr(&self, n: usize, r: usize) -> i64 { self.multiply( self.multiply(self.get_factorial(n), self.inverse(self.get_factorial(r))), self.inverse(self.get_factorial(n - r)), ) } } fn solve<R: io::BufRead, W: io::Write>(scan: &mut Scanner<R>, out: &mut W) { let t: i32 = scan.token(); let c=Combinatorics::new(300005,1_000_000_007); for _ in 0..t { let n: i64 = scan.token(); let k: i64 = scan.token(); } } fn main() { let mut scan = Scanner::new(io::stdin().lock()); let mut out = io::BufWriter::new(io::stdout().lock()); solve(&mut scan, &mut out); }
Editor is loading...
Leave a Comment