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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace ABC145D
{
    class Program
    {
        public static void Solve(Input input)
        {
            var x = input.NextInt();
            var y = input.NextInt();

            if ((x + y) % 3 != 0)
            {
                Console.WriteLine(0);
                return;
            }

            var m = (2 * x - y) / 3;
            var n = x - 2 * m;

            if (m < 0 || n < 0)
            {
                Console.WriteLine(0);
                return;
            }

            var mc = new ModCom();

            var count = mc.Ncr(n + m, n);

            Console.WriteLine(count);
        }

        /// <summary>
        /// Mを法とする数値によるCombinationなど
        /// Mは素数
        /// </summary>
        public class ModCom
        {
            public readonly int M;
            // 階乗テーブル
            private int[] factorials;
            // 逆元テーブル
            private int[] invs;
            // 階乗の逆元テーブル
            private int[] finvs;

            private const int maxTableSize = (int)1e5;

            public ModCom(int mod = (int)1e9 + 7)
            {
                M = mod;

                factorials = Enumerable.Repeat(1, 2).ToArray();
                invs = Enumerable.Repeat(1, 2).ToArray();
                finvs = Enumerable.Repeat(1, 2).ToArray();
            }

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            int Mul(int a, int b)
                => (int)(((long)(a % M) * (b % M)) % M);

            /// <summary> a/b </summary>
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            int Div(int a, int b)
                => Mul(a, Inverse(b));

            /// <summary>
            /// aの逆元（a^-1）
            /// （厳密には違うが）Mは素数とする
            /// </summary>
            int Inverse(int a)
            {
                int b = M;
                int u = 1, v = 0;

                while (b > 0)
                {
                    int t = a / b;
                    a -= t * b;
                    Swap(ref a, ref b);
                    u -= t * v;
                    Swap(ref u, ref v);
                }

                u %= M;
                if (u < 0)
                    u += M;

                return u;
            }

            static void Swap<T>(ref T a, ref T b)
            {
                T t = a;
                a = b;
                b = t;
            }

            /// <summary>
            /// n以下の各テーブルを作成する
            /// </summary>
            void MakeTable(int n)
            {
                int begin = factorials.Length;
                if (n < begin)
                    return;

                if (n > maxTableSize)
                {
                    throw new ArgumentOutOfRangeException();
                }

                Array.Resize(ref factorials, n + 1);
                Array.Resize(ref invs, n + 1);
                Array.Resize(ref finvs, n + 1);

                for (int i = begin; i <= n; i++)
                {
                    factorials[i] = Mul(factorials[i - 1], i);
                    invs[i] = M - Mul(invs[M % i], M / i);
                    finvs[i] = Mul(finvs[i - 1], invs[i]);
                }
            }

            /// <summary>
            /// 順列(nPr)
            /// </summary>
            public int Npr(int n, int r)
            {
                if (n < r)
                    return 0;
                if (n < 0 || r < 0)
                    return 0;

                if (n <= maxTableSize)
                {
                    MakeTable(n);

                    return Mul(factorials[n], finvs[n - r]);
                }
                else
                {
                    int npr = 1;
                    for (int i = n; i > n - r; i--)
                    {
                        npr *= n;
                        npr %= M;
                    }

                    return npr;
                }
            }

            /// <summary>
            /// 組み合わせ(nCr)
            /// </summary>
            public int Ncr(int n, int r)
            {
                r = Math.Min(r, n - r);

                if (n <= maxTableSize)
                {
                    return Mul(Npr(n, r), finvs[r]);
                }
                else
                {
                    int npr = 1, fact = 1;
                    for (int i = 0; i < r; i++)
                    {
                        npr = Mul(npr, n - i);
                        fact = Mul(fact, i + 1);
                    }

                    return Div(npr, fact);
                }
            }

            /// <summary>
            /// 重複組み合わせ(nHr)
            /// n個から重複を許してr個取り出す
            /// </summary>
            public int Nhr(int n, int r)
                => Ncr(n + r - 1, r);
        }

        #region Main
        public static void Main(string[] args)
        {
            // 出力が少ないときはこれをセットする方が時間かかるけれど
            // そのときにTLEするのはアルゴリズムが悪いせいだし、まあ良しとする
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 通常は引数無しで、ファイルから読み出したい場合はpath指定する
            var input = new Input();

            // 仮想的に標準入力をセットする
            // テスト入力したまま提出することがままあるので……
#if DEBUG
            input.SetText("");
#endif

            Solve(input);

            Console.Out.Flush();
        }
        #endregion

        #region Competitive Template
#pragma warning disable CS0414
        static readonly int MOD = (int)1e9 + 7;
        static readonly int[] dx = { 1, 0, -1, 0 };
        static readonly int[] dy = { 0, 1, 0, -1 };
        /// <summary> 左上が原点 </summary>
        static readonly char[] dir = { 'R', 'U', 'L', 'D' };
#pragma warning restore CS0414

        public class Input
        {
            // 変な改行コードがたまに混じっているので、一応セパレート指定する
            // スペース単独指定の方がもちろん早い
            static readonly char[] separator = { ' ', '\r', '\n' };
            readonly StreamReader sr;
            readonly Queue<string> queue;

            /// <summary>
            /// 特定のファイルから読み出したい場合はpath指定する
            /// </summary>
            public Input(string path = "")
            {
                queue = new Queue<string>();

                if (string.IsNullOrEmpty(path))
                { sr = new StreamReader(Console.OpenStandardInput()); }
                else
                { sr = new StreamReader(path); }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                    SetText(item);
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public bool SetText(string s)
            {
                if (string.IsNullOrEmpty(s))
                    return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                    queue.Enqueue(elem);

                return true;
            }

            /// <summary>
            /// 要素が存在するか
            /// </summary>
            public bool Any() => queue.Any() || Read();

            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            bool Read()
            {
                if (!SetText(sr.ReadLine()))
                    return false;

                if (!queue.Any())
                    return Read();

                return queue.Any();
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            public string Next()
            {
                if (!queue.Any() && !Read())
                    return "";

                return queue.Dequeue();
            }

            /// <summary>
            /// 指定個数queueにたまるまでenqueueし続ける
            /// </summary>
            bool Accumulate(int n)
            {
                while (queue.Count() < n)
                    if (!Read())
                        return false;

                return true;
            }

            public int NextInt() => int.Parse(Next());
            public long NextLong() => long.Parse(Next());
            public double NextDouble() => double.Parse(Next());

            /// <summary>
            /// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
            /// </summary>
            T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add)
            {
                if (!Accumulate(n))
                    return null;

                var a = new T[n];

                for (int i = 0; i < n; i++)
                    a[i] = add(parse(queue.Dequeue()), offset);

                return a;
            }

            public string[] Next(int n) => NextT(n, "", x => x, (x, y) => x);
            public int[] NextInt(int n, int offset = 0) => NextT(n, offset, int.Parse, (x, y) => x + y);
            public long[] NextLong(int n, long offset = 0) => NextT(n, offset, long.Parse, (x, y) => x + y);
            public double[] NextDouble(int n, double offset = 0.0) => NextT(n, offset, double.Parse, (x, y) => x + y);
        }

        static class Utils
        {
            public static T Max<T>(params T[] objs) => objs.Max();
            public static T Min<T>(params T[] objs) => objs.Min();

            /// <summary>
            /// vでfillされたT[d1][d2]配列を作成する
            /// </summary>
            public static T[][] Create2DArray<T>(int d1, int d2, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(v, d2).ToArray()).ToArray();
            }

            /// <summary>
            /// vでfillされたT[d1][d2][d3]配列を作成する
            /// </summary>
            public static T[][][] Create3DArray<T>(int d1, int d2, int d3, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(0, d2).Select(__ =>
                    Enumerable.Repeat(v, d3).ToArray()).ToArray()).ToArray();
            }
        }
        #endregion
    }
}