結果
問題 | No.1043 直列大学 |
ユーザー |
|
提出日時 | 2024-09-10 11:24:55 |
言語 | Rust (1.83.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,994 bytes |
コンパイル時間 | 13,363 ms |
コンパイル使用メモリ | 403,500 KB |
実行使用メモリ | 77,568 KB |
最終ジャッジ日時 | 2024-09-10 11:25:11 |
合計ジャッジ時間 | 16,067 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 10 WA * 18 |
ソースコード
use proconio::input;fn main() {input! {n:usize,m:usize,v:[usize;n],r:[usize;m],a:usize,b:usize,}let sum_v = v.iter().sum::<usize>();let sum_r = r.iter().sum::<usize>();let dp_v = calc_dp(&v, n);let dp_r = calc_dp(&r, m);let cum_dp_v = dp_v.cum(|cum, dpi| cum + dpi, ModInt::new(0));let mut ans = ModInt::<MOD>::new(0);for i in 1..=sum_r {ans += dp_r[i] * (cum_dp_v[(b * i + 1).min(sum_v + 1)] - cum_dp_v[(a * i).min(sum_v)]);}println!("{}", ans);}fn calc_dp(a: &Vec<usize>, n: usize) -> Vec<ModInt<MOD>> {let sum_a = a.iter().sum::<usize>();let mut dp = vec![vec![ModInt::<MOD>::new(0); sum_a + 1]; n + 1];dp[0][0] = ModInt::new(1);for i in 0..n {for j in 0..=sum_a {dp[i + 1][j] = dp[i + 1][j] + dp[i][j];if j + a[i] <= sum_a {dp[i + 1][j + a[i]] = dp[i + 1][j + a[i]] + dp[i][j];}}}dp[n].clone()}use cumulative::*;mod cumulative {pub trait Cumulative<T, F>whereT: Copy,F: Fn(T, T) -> T,{fn cum(&self, op: F, e: T) -> Vec<T>;fn cum_rev(&self, op: F, e: T) -> Vec<T>;}impl<T, F> Cumulative<T, F> for [T]whereT: Copy,F: Fn(T, T) -> T,{//cum[i]:=op[0,i)fn cum(&self, op: F, e: T) -> Vec<T> {let n = self.len();let mut res = vec![e; n + 1];for i in 0..n {res[i + 1] = op(res[i], self[i]);}res}//cum_rev[i]:=op[i,n)fn cum_rev(&self, op: F, e: T) -> Vec<T> {let n = self.len();let mut res = vec![e; n + 1];for i in (0..n).rev() {res[i] = op(res[i + 1], self[i]);}res}}}const MOD: usize = 1000000007;use modint::*;mod modint {use std::fmt;use std::ops;#[derive(Copy, Clone, PartialEq, Eq)]pub struct ModInt<const MOD: usize> {pub val: usize,}impl<const MOD: usize> ModInt<MOD> {pub fn new(val: usize) -> Self {Self { val: val % MOD }}pub fn pow(mut self, mut e: usize) -> Self {let mut res = Self::new(1);while 0 < e {if e & 1 != 0 {res *= self;}self *= self;e >>= 1;}res}}impl<const MOD: usize> From<usize> for ModInt<MOD> {fn from(value: usize) -> Self {Self { val: value % MOD }}}impl<const MOD: usize> fmt::Display for ModInt<MOD> {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {write!(f, "{}", self.val)}}impl<const MOD: usize> fmt::Debug for ModInt<MOD> {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {write!(f, "{}", self.val)}}impl<const MOD: usize> ops::Neg for ModInt<MOD> {type Output = Self;fn neg(self) -> Self::Output {Self {val: (MOD - self.val) % MOD,}}}impl<const MOD: usize> ops::Add for ModInt<MOD> {type Output = Self;fn add(self, rhs: Self) -> Self::Output {Self {val: (self.val + rhs.val) % MOD,}}}impl<const MOD: usize> ops::AddAssign for ModInt<MOD> {fn add_assign(&mut self, rhs: Self) {*self = *self + rhs;}}impl<const MOD: usize> ops::Mul for ModInt<MOD> {type Output = Self;fn mul(self, rhs: Self) -> Self::Output {Self {val: self.val * rhs.val % MOD,}}}impl<const MOD: usize> ops::MulAssign for ModInt<MOD> {fn mul_assign(&mut self, rhs: Self) {*self = *self * rhs;}}impl<const MOD: usize> ops::Sub for ModInt<MOD> {type Output = Self;fn sub(mut self, rhs: Self) -> Self::Output {if self.val < rhs.val {self.val += MOD;}Self {val: (self.val - rhs.val) % MOD,}}}impl<const MOD: usize> ops::SubAssign for ModInt<MOD> {fn sub_assign(&mut self, rhs: Self) {if self.val < rhs.val {self.val += MOD;}*self = *self - rhs;}}impl<const MOD: usize> ops::Div for ModInt<MOD> {type Output = Self;fn div(self, rhs: Self) -> Self {assert!(rhs.val != 0);self * rhs.pow(MOD - 2)}}impl<const MOD: usize> ops::DivAssign for ModInt<MOD> {fn div_assign(&mut self, rhs: Self) {*self = *self / rhs}}}