Bnuoj 4275 Your Ways(数学题 + 动态规划)

53 篇文章 0 订阅
47 篇文章 1 订阅

Your Ways


 You live in a small well-planned rectangular town in Phuket. The size of the central area of the town is H kilometers x W kilometers. The central area is divided into HW unit blocks, each of size 1 x 1 km2. There are H + 1 streets going in the West to East direction, and there are W + 1 avenue going in the North-South direction. The central area can be seen as a rectangle on the plane, as shown below.

We can identify each intersection by its co-ordinate on the plane. For example, on the Figure above the bottom-left corner is intersection (0,0), and the top-right corner is intersection (6,3).
Your house is at the bottom-left corner (i.e., intersection (0,0)) and you want to go to the university at the top-right corner (i.e., intersection (W,H)). More over, you only want to go to the university with wasting any efforts; therefore, you only want to walk from West-to-East and South-to-North directions. Walking this way, in the example above there are 84 ways to reach the university.
You want to go to the university for K days. Things get more complicated when each morning, the city blocks parts of streets and avenues to do some cleaning. The blocking is done in such a way that it is not possible to reach parts of the streets or avenues which is blocked from some other part which is blocked as well through any paths containingonly West-to-East and South-to-North walks.
You still want to go to the university using the same West-to-East and South-to-North strategy. You want to find out for each day, how many ways you can reach the university by only walking West-to-East and South-to-North. Since the number can be very big, we only want the result modulo 2552.

Input

The first line contains an integer T, the number of test cases (1 <= T <= 5). Each test case is in the following format.

The first line of each test case contains 3 integers: W, H, and K (1 <= W <= 1,000; 1 <= H <= 1,000; 1 <= K <= 10,000). W and H specify the size of the central area. K denotes the number of days you want to go to the university.
The next K lines describe the information on broken parts of streets and avenues. More specifically, line 1 + i, for 1 <= i <= K, starts with an integer Qi(1 <= Qi <= 100) denoting the number of parts which are blocked. Then Qi sets of 4 integers describing the blocked parts follow. Each part is described with 4 integers, A, B, C, and D (0 <= A <= C <= W; 0 <= B <= D <= H) meaning that the parts connecting intersection (A,B) and (C,D) is blocked. It is guaranteed that that part is a valid part of the streets or avenues, also C - A <= 1, and D – B <= 1, i.e., the part is 1 km long.

Output

For each test case, for each day, your program must output the number of ways to go to the university modulo 2552 on a separate line. i.e., the output for each test case must contains K lines.

Sample Input

2
2 2 3
1 0 0 0 1
2 1 0 2 0 0 2 1 2
1 1 1 2 1
100 150 2
1 99 150 100 150
2 99 150 100 150 100 149 100 150

Sample Output

3
4
4
1562
0

Hint

The amount of I/O for this task is quite large. Therefore, when reading input, you should avoid using java.io.Scanner which is much slower than using java.io.BufferedReader. 

题意:给出一个w*h的方格,问除去不能走的路,从(0,0)到(w,h)共有多少种走法。

分析:最终的走法为总的走法减去不能走的走法。用dp[i][j]表示从(0, 0)到(i, j)的走法,则经过(x, y) —> (xx, yy)这条路走法为dp[x][y] * dp[w - xx][h - yy]。用总的走法减去所有不能走的走法就是最终的答案。
#include <cstdio>
#include <cmath>
using namespace std;

const int mod = 2552;
const int MaxN = 1005;

int dp[MaxN][MaxN];

void Init() {
    for(int i = 0; i < MaxN; i++)
        dp[i][0] = dp[0][i] = 1;
    for(int i = 1; i < MaxN; i++)
        for(int j = 1; j < MaxN; j++)
            dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % mod;
}

int main() {
    int T, w, h, k, q, x, y, xx, yy;
    Init();
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d%d", &w, &h, &k);
        while(k--) {
            scanf("%d", &q);
            int ans = dp[w][h];
            while(q--) {
                scanf("%d%d%d%d", &x, &y, &xx, &yy);
                ans = ((ans - dp[x][y] * dp[w - xx][h - yy]) % mod + mod) % mod;
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}



以下是三种处理数据预处理中缺失值的方法: 1. 删除缺失值:可以直接删除包含缺失值的行或列。这种方法适用于缺失值较少的情况,以确保数据的完整性。 ```python import pandas as pd # 创建包含缺失值的数据框 data = {'A': [1, 2, None, 4, 5], 'B': [None, 2, 3, 4, 5]} df = pd.DataFrame(data) # 删除包含缺失值的行 df.dropna(inplace=True) # 删除包含缺失值的列 df.dropna(axis=1, inplace=True) ``` 2. 填充缺失值:可以使用统计量(如均值、中位数、众数)来填充缺失值。这种方法适用于缺失值较少且缺失值与其他值之间没有明显关联的情况。 ```python import pandas as pd # 创建包含缺失值的数据框 data = {'A': [1, 2, None, 4, 5], 'B': [None, 2, 3, 4, 5]} df = pd.DataFrame(data) # 使用均值填充缺失值 df.fillna(df.mean(), inplace=True) # 使用中位数填充缺失值 df.fillna(df.median(), inplace=True) # 使用众数填充缺失值 df.fillna(df.mode().iloc[0], inplace=True) ``` 3. 插值法填充缺失值:可以使用插值方法(如线性插值、多项式插值)来填充缺失值。这种方法适用于缺失值较多且缺失值与其他值之间存在一定的关联性的情况。 ```python import pandas as pd # 创建包含缺失值的数据框 data = {'A': [1, 2, None, 4, 5], 'B': [None, 2, 3, 4, 5]} df = pd.DataFrame(data) # 使用线性插值填充缺失值 df.interpolate(method='linear', inplace=True) # 使用多项式插值填充缺失值 df.interpolate(method='polynomial', order=2, inplace=True) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值