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.42.0) (judge verdict: Accepted)

#[allow(unused_macros)]
macro_rules ! read_value {($ next : expr , ($ ($ t : tt ) ,* ) ) => {($ (read_value ! ($ next , $ t ) ) ,* ) } ; ($ next : expr , [$ t : tt ; $ len : expr ] ) => {(0 ..$ len ) . map (| _ | read_value ! ($ next , $ t ) ) . collect ::< Vec < _ >> () } ; ($ next : expr , chars ) => {read_value ! ($ next , String ) . chars () . collect ::< Vec < char >> () } ; ($ next : expr , usize1 ) => {read_value ! ($ next , usize ) - 1 } ; ($ next : expr , $ t : ty ) => {$ next () . parse ::<$ t > () . expect ("Parse error" ) } ; }
#[allow(unused_macros)]
macro_rules ! input_inner {($ next : expr ) => {} ; ($ next : expr , ) => {} ; ($ next : expr , $ var : ident : $ t : tt $ ($ r : tt ) * ) => {let $ var = read_value ! ($ next , $ t ) ; input_inner ! {$ next $ ($ r ) * } } ; }
#[allow(unused_macros)]
macro_rules ! input {(source = $ s : expr , $ ($ r : tt ) * ) => {let mut iter = $ s . split_whitespace () ; let mut next = || {iter . next () . unwrap () } ; input_inner ! {next , $ ($ r ) * } } ; ($ ($ r : tt ) * ) => {let stdin = std :: io :: stdin () ; let mut bytes = std :: io :: Read :: bytes (std :: io :: BufReader :: new (stdin . lock () ) ) ; let mut next = move || -> String {bytes . by_ref () . map (| r | r . unwrap () as char ) . skip_while (| c | c . is_whitespace () ) . take_while (| c |! c . is_whitespace () ) . collect () } ; input_inner ! {next , $ ($ r ) * } } ; }

#[allow(dead_code)]
fn ext_gcd(a: i64, b: i64) -> (i64, i64, i64) {
    if b == 0 {
        (a, 1, 0)
    } else {
        let (g, x, y) = ext_gcd(b, a % b);
        (g, y, x - (a / b) * y)
    }
}
#[allow(dead_code)]
fn mod_inverse(n: u64, modulo: u64) -> u64 {
    let (_, inv, _) = ext_gcd(n as i64, modulo as i64);
    ((inv + modulo as i64) as u64 % modulo) % modulo
}
#[allow(dead_code)]
fn mod_factor(n: u64, modulo: u64) -> u64 {
    let mut fact = 1;
    for i in 1..=n {
        fact = fact * i % modulo;
    }
    fact
}
#[allow(dead_code)]
fn mod_combination(n: u64, r: u64, modulo: u64) -> u64 {
    if n < r {
        return 0;
    }
    let a1 = mod_factor(n, modulo);
    dbg!(a1);
    let a2 = mod_factor(r, modulo);
    let a3 = mod_factor(n - r, modulo);
    a1 * mod_inverse(a2 * a3 % modulo, modulo) % modulo
}

fn main() {
    input! {
        x: i64,
        y: i64,
    }
    if (2 * x - y) % 3 != 0 {
        println!("0")
    } else {
        let p = (2 * x - y) / 3;
        let m = (2 * y - x) / 3;

        if (p < 0) || (m < 0) {
            println!("0")
        } else {
            let ret = mod_combination((p + m) as u64, m as u64, 1000000007);
            println!("{}", ret);
        }
    }
}