以下はパスカルの三角形からnCk
を mod 1,000,000,000+7
にて求めるプログラムです。
MUsize は演算の際自動でmod
をとる構造体です。
演算子オーバーロードを実装した構造体をVectorに格納し実行したところ以下のようなワーニングが出力されてしまいます。Vectorの中身をMUsizeではなくusizeで同じように実装したところワーニングは出ませんでした。MUsizeとそのメソッドをどの様に改変すればワーニングが取れますでしょうか? よろしくお願いします。
この理由としてMUsizeにderiveするトレイトがusizeと比べ何か足りないのではないかと考えております。
/*
AtCoder abc 132 解説より
https://www.youtube.com/watch?v=mso8tE1yMl8
*/
/*
1
1 1
1 2 1
1 3 3 1
aCb はa段目のb番目
*/
static MOD: usize = 1_000_000_000 + 7;
use std::ops::{AddAssign, SubAssign, MulAssign};
use std::ops::{Add, Sub, Mul};
#[derive(Copy, Clone, Debug)]
struct MUsize {x: usize}
impl MUsize {
fn new(x: usize) -> MUsize {
MUsize{x: x%MOD}
}
}
impl AddAssign for MUsize {
fn add_assign(&mut self, other: MUsize) {
let tmp = self.x + other.x;
*self = MUsize {
x: if tmp >= MOD {tmp - MOD} else {tmp}
};
}
}
impl<'a> AddAssign<&'a MUsize> for MUsize {
fn add_assign(&mut self, other: &MUsize) {
let tmp = self.x + other.x;
*self = MUsize {
x: if tmp >= MOD {tmp - MOD} else {tmp}
};
}
}
impl SubAssign for MUsize {
fn sub_assign(&mut self, other: MUsize) {
let tmp = self.x + MOD - other.x;
*self = MUsize {
x: if tmp >= MOD {tmp - MOD} else {tmp}
};
}
}
impl MulAssign for MUsize {
fn mul_assign(&mut self, other: MUsize) {
*self = MUsize {
x: self.x * other.x % MOD
};
}
}
impl Add for MUsize {
type Output = MUsize;
fn add(self, other: MUsize) -> MUsize {
let mut res = MUsize::new(self.x);
res += other;
res
}
}
impl<'a> Add<&'a MUsize> for MUsize {
type Output = MUsize;
fn add(self, other: &MUsize) -> MUsize {
let mut res = MUsize::new(self.x);
res += other;
res
}
}
impl Sub for MUsize {
type Output = MUsize;
fn sub(self, other: MUsize) -> MUsize {
let mut res = MUsize::new(self.x);
res -= other;
res
}
}
impl Mul for MUsize {
type Output = MUsize;
fn mul(self, other: MUsize) -> MUsize {
let mut res = MUsize::new(self.x);
res *= other;
res
}
}
struct C {
c: Vec<Vec<MUsize>>
}
impl C {
fn new(max: usize) -> C {
let mut c = vec![vec![MUsize::new(0); max+2]; max+2];
c[0][0] = MUsize::new(1);
for i in 0..max+1 {
for j in 0..i+1 {
c[i+1][j] += c[i][j];
c[i+1][j+1] += c[i][j];
}
}
C {c}
}
fn c(&self, n: usize, k: usize) -> usize {
self.c[n][k].x
}
}
fn main() {
let c = C::new(40);
println!("{}", c.c(5, 2));
}
warning[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable
--> src/main.rs:83:30
|
83 | c[i+1][j] += c[i][j];
| -------------^------
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
| mutable borrow later used here
|
= warning: this error has been downgraded to a warning for backwards compatibility with previous releases
= warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future
warning[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable
--> src/main.rs:84:32
|
84 | c[i+1][j+1] += c[i][j];
| ---------------^------
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
| mutable borrow later used here
|
= warning: this error has been downgraded to a warning for backwards compatibility with previous releases
= warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future
Finished dev [unoptimized + debuginfo] target(s) in 0.47s