#[doc = " https://github.com/hatoo/competitive-rust-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 ] } ; } #[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 { reader: R, buf: Vec, pos: usize, } impl Parser { pub fn from_str(s: &str) -> Parser { Parser { reader: io::empty(), buf: s.as_bytes().to_vec(), pos: 0, } } } impl Parser { pub fn new(reader: R) -> Parser { 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(&mut self) -> Result { 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::(); } 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!{ p:i64, Q:usize, q:[usize;Q] } let NMAX = 100000; let mo = 1_000_000_007; let mut a = vec![0;NMAX+1]; a[1] = 0; a[2] = 1; for i in 3..=NMAX { a[i] = p * a[i-1]; a[i] %= mo; a[i] += a[i-2]; a[i] %= mo; } let p1 = Polynomial::new(a, mo); let p2 = p1.multiply(&p1); for qi in q { writeln!(out,"{}",p2.coeff[qi]); } } #[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) } } #[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 } #[derive(Debug)] struct Polynomial { coeff: Vec, mo: i64, } impl Polynomial { pub fn new(coeff: Vec, 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; loop { if 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; loop { if 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; } else { break; } } w = (w * base) % self.mo; } m *= 2; } else { break; } } 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 { 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 } } #[doc = "compute minimum x from a list of x % m[i] = r[i]"] pub fn garner(mr: Vec<(i64, i64)>, mo: i64) -> i64 { let mut mr = mr; mr.push((mo, 0)); let mut coef = vec![1; mr.len()]; let mut constants = vec![0; mr.len()]; for i in 0..mr.len() - 1 { let v = (mr[i].1 + mr[i].0 - constants[i]) * modinv(coef[i], mr[i].0) % mr[i].0; for j in i + 1..mr.len() { constants[j] += coef[j] * v; constants[j] %= mr[j].0; coef[j] *= mr[i].0; coef[j] %= mr[j].0; } } constants[mr.len() - 1] } pub fn ntt_multiply(a: &[i64], b: &[i64], mo: i64) -> Vec { 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 }