Problem:
Score : 400 points

Problem Statement

There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.

When the knight is at the square (i, j), it can be moved to either (i+1,j+2) or (i+2, j+1).

In how many ways can the knight reach the square (X, Y)?

Find the number of ways modulo 10^9 + 7.

Constraints

1 \leq X \leq 10^6

1 \leq Y \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the number of ways for the knight to reach (X, Y) from (0, 0), modulo 10^9 + 7.

Sample Input 1

3 3

Sample Output 1

2

There are two ways: (0,0) \to (1,2) \to (3,3) and (0,0) \to (2,1) \to (3,3).

Sample Input 2

2 2

Sample Output 2

0

The knight cannot reach (2,2).

Sample Input 3

999999 999999

Sample Output 3

151840682

Print the number of ways modulo 10^9 + 7.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[test]
fn test_solve() {
    assert_eq!(solve("3 3\n"), "2\n");
    assert_eq!(solve("2 2\n"), "0\n");
    assert_eq!(solve("999999 999999\n"), "151840682\n");
    dbg!(solve("1000000 100002\n"));
}

fn solve(src: &str) -> String {
    let mut sc = Scanner::from_str(src);

    let mut pat = Finite(1);
    let x: u64 = sc.read();
    let y: u64 = sc.read();
    let n = (x+y)/3;
    if (x+y)%3 != 0 || x < n || y < n {
        return "0\n".to_string()
    }
    let x = x - n;
    let y = y - n;

    for i in 1..n+1 {
        pat = pat * Finite(i);
    }
    for i in 1..x+1 {
        pat = pat / Finite(i);
    }
    for i in 1..y+1 {
        pat = pat / Finite(i);
    }

    format!("{}\n", pat)
}

const MOD: u64 = 1_000_000_007;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
struct Finite(u64);
impl Finite {
    pub fn pow(self, mut exp: u32) -> Self {
        let mut base = self.0;
        let mut acc = 1;
        while exp > 1 {
            if (exp & 1) == 1 {
                acc = acc * base % MOD;
            }
            exp >>= 1;
            base = base * base % MOD;
        }
        if exp == 1 {
            acc = acc * base % MOD;
        }
        Finite(acc)
    }
}
impl std::fmt::Display for Finite {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl std::ops::Mul for Finite {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self {
        Finite(self.0 * rhs.0 % MOD)
    }
}
impl std::ops::Div for Finite {
    type Output = Self;
    fn div(self, rhs: Self) -> Self {
        self * rhs.pow((MOD - 2) as u32)
    }
}

fn main() {
    let mut buf = String::new();
    use std::io;
    use std::io::{Read, Write};
    io::stdin().read_to_string(&mut buf).unwrap();
    io::stdout().write_all(solve(&buf).as_bytes()).unwrap();
}

struct Scanner<'a>(std::str::SplitWhitespace<'a>);
impl<'a> Scanner<'a> {
    fn from_str(src: &str) -> Scanner {
        Scanner(src.split_whitespace())
    }
    fn get(&mut self) -> &str {
        self.0.next().unwrap()
    }
    fn read<T: std::str::FromStr>(&mut self) -> T {
        self.get().parse().ok().unwrap()
    }
}