AT1761 square1001 of school route (square1001's School Road)

AT1761 square1001 of school route (square1001's School Road)

ATcoder
Luogu

Problem statement
square1001 are through to the junior high school on foot every day.

He has lived in the lower left corner of the intersection (1, 1), go to school in the upper-right corner of the intersection (W, H).

However, there are the following conditions.

He does not proceed only on right or because of the time shortened.
He is that day, there was a errands into K trout.
In the lower left is (Xi, Yi), is a mass in the upper right corner is (Xi + 1, Yi + 1 ).
Therefore, for each of errands mass, at least one of the one intersection 4 around its had to pass.

Whether those conditions are met Directions a number of ways. Find in mod 1,000,000,007.
Input Example 1

4 4 1
2 2

Output Example 1

18


And directions of (1,1) → (1,2) → (1,3) → (1,4) → (2,4) → (3,4) → (4,4),

(1,1) → (2,1) → (3,1) → (4,1) → (4,2) → (4,3) → (4,4) that directions can not be.

Thus, the output of the 20-2 = 18.

※ Sorry for missing the upper left corner the number of the figure.

Translation of the meaning of problems

square1001 walk to school every day.

He lives at the crossroads lower left (1,1), go to schools located in the upper right corner (W, H) of.

However, there are some conditions.

He only right to shorten the time or on the go. That day, he found that there are K grid, the lower left corner coordinates are (Xi1, Yi1), (Xi2, Yi2), (Xi3, Yi3) ...... (XiK, YiK)

Furthermore, with this lattice, the four lattice surrounding (four refers to a surrounding grid coordinates of four vertices) in square1001 require at least one.

Path to meet these conditions how many?

After 1,000,000,007 output answer mod.

How to do it?

Such issues considered directly counted DP like.
There is aCompare similarThe title is recommended to do it.
CF559C Gerald and Giant Chess
This question of transfer: For each point, can be directly transferred from the lower left plus its lower right. As for how to get restrictions, we found that for every square abscissa ordinate plus two points imperative is certain, it is possible to maintain an array, if and only if it corresponds to the information recorded on the map points transferred.

//记录点的信息
++mp[x + 1][y];++mp[x][y + 1];
//对每个横纵坐标的合维护出现次数
++cnt[x + y + 1];
//转移时进行判断
cnt[i + j] == mp[i][j]

Take a sample is described, for example in the (1,4) point (not illegal state transition), since cnt [5] = 1, mp [1] [4] = 0, can not be transferred. This ensures that the path will be through a certain point passes.
This is what I saw snow39 code found good skills.

Code

#include <bits/stdc++.h>
namespace fdata
{
inline char nextchar()
{
    static const int BS = 1 << 21;
    static char buf[BS], *st, *ed;
    if (st == ed)
        ed = buf + fread(st = buf, 1, BS, stdin);
    return st == ed ? -1 : *st++;
}
#ifdef lky233
#define nextchar getchar
#endif
template <typename Y>
inline void poread(Y &ret)
{
    ret = 0;
    char ch;
    while (!isdigit(ch = nextchar()))
        ;
    do
        ret = ret * 10 + ch - '0';
    while (isdigit(ch = nextchar()));
}
#undef nextcar
} // namespace fdata
using fdata::poread;
using namespace std;
const int MAXN = 1005;
const int MOD = 1e9 + 7;
int f[MAXN][MAXN];
int mp[MAXN][MAXN];
int cnt[MAXN];
int h, w, k;
int main()
{
    poread(h), poread(w), poread(k);
    for (register int i = 1, x, y; i <= k; ++i)
    {
        poread(x), poread(y);
        ++mp[x + 1][y];
        ++mp[x][y + 1];
        ++cnt[x + y + 1];
    }
    f[1][1] = 1;
    for (register int i = 1; i <= h; ++i)
    {
        for (register int j = 1; j <= w; ++j)
        {
            if (i != 1 && cnt[i + j] == mp[i][j])
                f[i][j] = (f[i][j] + f[i - 1][j]) % MOD;
            if (j != 1 && cnt[i + j] == mp[i][j])
                f[i][j] = (f[i][j] + f[i][j - 1]) % MOD;
        }
    }
    cout << f[h][w] << endl;
}

I saw a python code at the time, then moved here.
Source commit record

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime,random
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
 
H,W,K = inpl()
XYs = [inpl() for _ in range(K)]
XYs.sort()
 
MAX = H+W
fac = [1]*(MAX+1)
for i in range(1,MAX+1):
    fac[i] = (fac[i-1]*i)%mod
 
gyakugen = [1]*(MAX+1)
gyakugen[MAX] = pow(fac[MAX],mod-2,mod)
for i in range(MAX,0,-1):
    gyakugen[i-1] = (gyakugen[i]*i)%mod
 
def Comb (n,k):#nCk
    return (fac[n]*gyakugen[k]*gyakugen[n-k])%mod
 
def calc(xys,xyt):
    dx = xyt[0] - xys[0]
    dy = xyt[1] - xys[1]
    return Comb(dx+dy,dx)
 
def LU(xy):
    x,y = xy
    return([x,y+1])
 
def RD(xy):
    x,y = xy
    return([x+1,y])
 
tmp0 = calc([1,1],LU(XYs[0]))
tmp1 = calc([1,1],RD(XYs[0]))
for i in range(1,K):
    tmp0ed,tmp1ed = tmp0,tmp1
    tmp0 = tmp0ed * calc(LU(XYs[i-1]),LU(XYs[i])) + tmp1ed * calc(RD(XYs[i-1]),LU(XYs[i]))
    tmp1 = tmp0ed * calc(LU(XYs[i-1]),RD(XYs[i])) + tmp1ed * calc(RD(XYs[i-1]),RD(XYs[i]))
    tmp0 %= mod
    tmp1 %= mod
 
ans = tmp0 * calc(LU(XYs[K-1]),[W,H]) + tmp1 * calc(RD(XYs[K-1]),[W,H])
print(ans%mod)

Guess you like

Origin www.cnblogs.com/Shiina-Rikka/p/11593336.html