Combined number Modulus gym100947e | | Codeforces 559c

Source: Internet
Author: User
Tags modulus

E-qwerty78 TripTime limit:2000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeGym 100947E

Description

Standard Input/output
Announcement
  • Statements

    Qwerty78 is a well known programmer (He was a member of the ICPC WF winning team in, a TopCoder target and one of the code Forces Top 10).

    He wants to go-Dreamoon ' s house-to-apologize to him, after he ruined him plans in winning a Div2 contest (he Participat Ed using the handle "Sorry_dreamoon") so he came first and Dreamoon came second.

    Their houses is presented on a grid of N rows and M columns. Qwerty78 House was at the cell (1, 1) and Dreamoon's house wasat the cell (N, M).

    If Qwerty78 is standing in a cell (R, C) He can go to the cell (R + 1, C) or T o the cell (R, C + 1). Unfortunately Dreamoon expected Qwerty78 visit, so he puts exactly 1 obstacle in the this grid (neither in his house nor in Qw Erty78 ' s house) to challenge Qwerty78. Qwerty78 can ' t enter a cell which contains an obstacle.

    Dreamoon sent Qwerty78 a message "in what many ways can you reach my house?". Your task is to help Qwerty78 and count the number of ways he can reach Dreamoon's house. Since The answer is too large, and you were asked to calculate it modulo9 + 7.

Input

The first line containts a single integer T , the number of testcases.

Then T testcases is given as follows:

The first line of each testcase contains, space-separated N, m ( 2≤ n, m ≤105)

The second line of each testcase contains 2 space-separated integers oR, o c -The coordinates of the blocked cell ( 1≤ ORN) (1≤ o< /c21>CM).

Output

Output T Lines, the answer for each testcase which are the number of ways Qwerty78 can reach Dreamoon ' s house modulo 1 09 + 7.

Sample Input

Input
1
2 3
1 2
Output
1

Hint

Sample testcase Explanation:

The grid has the following form:

q*.

.. D

Only one valid path:

(2,1) to ( 2,2) to ( 2,3).

Test instructions, I give you a matrix, and there is only one obstacle lattice (x, y), which asks for the number of programs (N,M)

Idea: First the number of programs without barriers is C (n+m-2,m-1), and the number of scenarios with one barrier is total-the number of programs that have been disabled. The number of scenarios that pass through the barrier should be: number of methods that go to the barrier (the number of ways to go from the barrier to the end), then C (n+m-2,m-1)-C (x+y-2,y-1) XC (N-x+1+m-y+1-2, M-y+1-1), and the number of combinations can be modeled according to C (n,m) = n! /((N-M)! xm! Because it involves division, the inverse of all factorial is preprocessed when the remainder is taken. Can be based on fast dense to find the inverse, the complexity of nlog, you can also direct the introduction of all factorial inverse, the complexity of the

`

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intN = 2e5 +Ten;Const intMOD = 1e9 +7; typedefLong Longll;ll Fac[n], Afac[n];ll powm (ll A, ll b) {ll ans=1; A= a%MOD;  while(b) {if(B &1) ans = ans * a%MOD; A= A * A%MOD; b>>=1; }    returnans;}voidPre () {fac[0] =1;  for(inti =1; i < N; ++i) Fac[i] = fac[i-1] * (LL) I%MOD; Afac[n-1] = POWM (Fac[n-1], MOD-2);  for(inti = N-1; I >=1; -I.) Afac[i-1] = afac[i] * I%MOD;} llGet(intXinty) {ll ans=1; Ans= ((fac[x] * afac[x-y])% MOD * Afac[y])%MOD; returnans;}intMain () {int_; scanf"%d", &_);    Pre ();  while(_ --) {ll n, m, x, y; scanf ("%i64d%i64d%i64d%i64d", &n, &m, &x, &y); ll Res1=Get(n + M-2, M-1); ll Res2=Get(x + y-2, Y-1); ll Res3=Get(n + m-x-y, M-y); printf ("%i64d\n", (res1 + mod-res2 * res3% MOD)%MOD); }    return 0;}
View Code

Codeforces 559c-gerald and Giant ChessTime limit:2000MS Memory Limit:262144KB 64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 559C

Description

Giant chess is quite common in geraldion. We won't delve into the rules of the game, we'll just say that the game takes place on An < Em>h x w  field, and it is painted in the colors, but not as in chess. Almost all cells of the field is white and only some of them is black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald have almost won, and the only thing he needs to win are to bring the pawn from the upper left corner of the board, WH Ere it was now standing, to the lower right corner. Gerald is so confident of victory the he became interested, in how many ways can he win?

The pawn, which Gerald have got left can go in both ways:one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There is no other pawns or pieces left on the field, so, according to the rules of giant chess Gerald moves he pawn Until the game is over, and Pollard are just watching this process.

Input

The first line of the input contains three integers: h, W, n -the sides of the board and T He number of black cells (1≤ H, w ≤105, 1≤ n ≤2000).

Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1≤ R ih, 1≤ ciw)-the number of the row and column of The i-th cell.

It is guaranteed, the upper left and lower, right cell was white, and all cells in the description were distinct.

Output

Print a single line-the remainder of the number of ways to move Gerald ' s pawn from the upper left to the lower right Cor NER modulo9 + 7.

Sample Input

Input
3 4 2
2 2
2 3
Output
2
Input
100 100 3
15 16
16 15
99 88
Output
545732279

Test instructions: This is the complex version above, there are n barriers, n = 2000
Idea: We set dp[i] to reach the first barrier, without passing through (Xi,yi) the number of other obstacles in the program, then there is dp[i] = C (xi+yi-2,yi-1)-Sigma (Dp[j] * C (XI-XJ+YI-YJ, Yi-yj))
Of these, XJ <= XI && yj <= Yi We regard the first (N,M) also as the first n+1 barrier, then dp[n+1] is the answer
#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intN =2005;Const intX = 1e5 +Ten;Const intM = 2e5 +Ten;Const intMOD = 1e9 +7;intH, W, N;ll Fac[m], afac[m], bc[n], Dp[n];ll pow_mod (ll A, ll b) {ll ans=1; A%=MOD;  while(b) {if(B &1) ans = (ans * a)%MOD; A= (A * a)%MOD; b>>=1; }    returnans;}voidPre () {fac[0] =1;  for(inti =1; i < M; ++i) Fac[i] = fac[i-1] * (LL) I%MOD; Afac[m-1] = Pow_mod (Fac[m-1], MOD-2);  for(inti = M-1; I >=1; -I.) Afac[i-1] = afac[i] * (LL) I%MOD;} ll C (intNintm) {if(M > N)return 0; return((fac[n] * afac[n-m]% MOD) * Afac[m])%MOD;}intMain () {pre (); CIN>> H >> W >>N;    ll x, y;  for(inti =1; I <= N; ++i) {cin>> x >>y; Bc[i]= (ll) x * x +y; } bc[++n] = (LL) H * X +W; Sort (BC+1, BC + N +1);  for(inti =1; I <= N; ++i) {intXI = bc[i]/X, Yi = bc[i]%X; Dp[i]= C (xi + yi-2, Yi-1);  for(intj =1; J < I; ++j) {intXJ = Bc[j]/X, YJ = bc[j]%X; if(Xj > XI | | yj > Yi)Continue; Dp[i]= (mod + dp[i]-(dp[j] * C (XI-XJ + yi-yj, yi-yj)% MOD))%MOD; }} cout<< Dp[n] <<Endl; return 0;}
View Code

Combined number Modulus gym100947e | | Codeforces 559c

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.