UPC-Knight (a combination of the foundations of mathematics, inverse)

Learning is like rowing upstream

Knight

Title Description

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 109+7.
Constraints
·1≤X≤106
·1≤Y≤106
·All values in input are integers.

Entry

Input is given from Standard Input in the following format:
X Y

Export

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

Sample Input 1

3 3

Sample Output 1

2

Sample Input 2

999999 999999

Sample Output 2

151840682

Sample Input 3

2 2

Sample Output 3

0

Topic effect
in chess, Chinese chess knight similar moves, but its moves is 2 + 1,
which means
to move forward two squares to go to the right one space
walk two squares forward one space to the left to go
back take a right to go two squares of a grid
go back two spaces go left one space
left to go two squares to move forward one space
left to go back to go two squares of a grid
right away two squares to move forward one space
to the right go forward two spaces after a grid
total of eight moves
as shown in
Here Insert Picture Description
the title want you to calculate from (0,0) to (x, y) the knight can come to that point yet, sadly, this can only be to the right or to ride go up
is as shown in Here Insert Picture Description
the calculated number of all programs in 10 . 9 +7 take I

Ideas analysis
Here Insert Picture Description
This question and similar methods, this man went from A to B, then how many ways it
is, he must take the right step 5, go up three steps. But take a right onto the uncertainty in the Shashi Hou
then we know that there are

C 5 8 or C 3 8 Kind can can which is 56 Kind How to case C_ {5} ^ {8} or C_ {3} ^ {8} possible, i.e., 56 kinds of solutions
then this question is similar, but to take the knight walked two to go up to the right or a right to take a walk up the two strategies, all of which say the whole good,This question becomes how many choose Option 1 and Option 2

Then we can solve the equation
2M + ① X = n-
m + 2N = Y ②
so that we can determine the values of X and Y
can be obtained by
m = (2X-Y) /. 3
n-= (2Y-X) /. 3
so that if m and n can not be certain that no divisible by 3 solution
followed by, Here Insert Picture Description
this must be impossible moves, i.e. n and m can not make direct access to a certain negative value

According to later
C m n = n ! ( m ! ) 1 ( ( n m ) ! ) 1 C_{m}^{n}=n!*(m!)^{-1}*((n-m)!)^{-1}
again may be in accordance with inverse
inverse element of ten. 9+7 I take
it can make X-1is equivalent to X1E9 +. 5
The congruence modulus principle, (a * b)% c
after the inverse, in addition to changes to multiply. Thus solvable

Time to AC

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
#include <stack>
#include<string>
#include<utility>
#include<math.h>
#include<stdio.h>
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
#define read read()
ll A = 1e9 + 7;
ll B = A - 2;
ll qpow(ll x, ll n, ll mod)
{
	ll res = 1;
	while (n > 0)
	{
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}
ll fac(ll n)
{
	ll res = 1;
	for (int i = 1; i <= n; i++)
	{
		res *= i;
		res %= A;
	}
	return res;
}
int main()
{
	ll X, Y;
	X = read, Y = read;
	ll m, n;
	m = X + X - Y;
	n = Y + Y - X;
	if (m % 3 || n % 3 || m < 0 || n < 0)
	{
		cout << 0 << endl;
		return 0;
	}
	else
	{
		m /= 3;
		n /= 3;
		ll temp_a = fac(m + n) % A;
		ll temp_b = fac(n) % A;
		ll temp_c = fac(m) % A;
		ll ans = temp_a;
		temp_b = qpow(temp_b, B, A);
		ans = (ans * temp_b) % A;
		temp_c = qpow(temp_c, B, A);
		ans = (ans * temp_c) % A;
		cout << ans % A << endl;
	}
}

In addition to change after the ride was not the inverse factorial not know how to deal with the data explosion was unable to start, forget the inverse. According inverse, combinatorics, congruence theorems mold, rapid power. This problem can be solved.

There is a road ground for the tracks, Boundless Learning bitter for the boat.
Published 32 original articles · won praise 12 · views 1188

Guess you like

Origin blog.csdn.net/qq_35339563/article/details/104524878