Monotonic Matrix

本文解析了一道计数矩阵问题,需要求出满足特定条件的 nxm 矩阵的数量。通过对01分界线和12分界线的路径分析,采用组合数学的方法给出了求解公式,并附带了C++代码实现。

链接:https://www.nowcoder.com/acm/contest/139/A
 

题目描述

Count the number of n x m matrices A satisfying the following condition modulo (109+7).
* Ai, j ∈ {0, 1, 2} for all 1 ≤ i ≤ n, 1 ≤ j ≤ m.
* Ai, j ≤ Ai + 1, j for all 1 ≤ i < n, 1 ≤ j ≤ m.
* Ai, j ≤ Ai, j + 1 for all 1 ≤ i ≤ n, 1 ≤ j < m.

输入描述:

The input consists of several test cases and is terminated by end-of-file.
Each test case contains two integers n and m.

输出描述:

For each test case, print an integer which denotes the result.

示例1

输入

复制

1 2
2 2
1000 1000

输出

复制

6
20
540949876

备注:

 

* 1 ≤ n, m ≤ 103
* The number of test cases does not exceed 105.

分析:

考虑n*m中01的分界线和12的分界线,一开始两条路径的起点都是(1,1)终点都是(n,m)

然后考虑他们相交的情况,相交包括重叠,所以将01的分界线往右上角移动,起点变成(2,0)终点变成(n+1,m-1)

在交点处交换他们后面的边使01的分界线起点变成(2,0)终点变成(n,m)。

12的分界线,起点变成(1,1),终点变成(n+1,m-1)

Cm+n,n(2)-Cm+n,n-1*Cm+n,m-1

代码:

#include <bits/stdc++.h>

const int MOD = 1e9 + 7;

const int N = 1005;

int dp[N][N];

void update(int& x, int a)
{
    x += a;
    if (x >= MOD) {
        x -= MOD;
    }
}

int sqr(int x)
{
    return 1LL * x * x % MOD;
}

int main()
{
    dp[0][0] = 1;
    for (int i = 0; i < N; ++ i) {
        for (int j = 0; j < N; ++ j) {
            if (i) {
                update(dp[i][j], dp[i - 1][j]);
            }
            if (j) {
                update(dp[i][j], dp[i][j - 1]);
            }
        }
    }
    int n, m;
    while (scanf("%d%d", &n, &m) == 2) {
        printf("%d\n", ((sqr(dp[n][m]) + MOD - 1LL * dp[n - 1][m + 1] * dp[n + 1][m - 1] % MOD) % MOD));
    }
}

 

注释:if (property == toInt(VehicleProperty::HUD_TRANSFER_WARP_MATRIX_DATA)) { // prop = getPropValue(toInt(VehicleProperty::HUD_WARPING_MATRIX), data, count * 8, VehiclePropertyStatus::AVAILABLE); uint8_t *p = (uint8_t*)data; for (int i = 0; i < count; i++) { reverse(p, p + 8); p = p + 8; } onPropChanged(toInt(VehicleProperty::HUD_WARPING_MATRIX), data, count * sizeof(int64_t)); return; } if (context.getReportWay() == REPORT_DIFFERENCE && !context.getForceReportFlag()) { ret = context.setData(data, count); if (ret != DATA_SET_OK) { return; } } else { ret = context.setData(data, count); } if (toInt(VehicleProperty::MCU_IG_DATA) == property && ret == DATA_SET_OK) { for (auto manager : mManagerVec) { manager->onVehiclePowerStatus(*((int32_t*)data)); } } if (toInt(VehicleProperty::MCU_SYNC_INFO) == property && ret == DATA_SET_OK) { for (auto manager : mManagerVec) { manager->onCfcInfoChanged(data, count); } } // only dispatch the property has subscribe. if (context.getSubscribe() && ret != DATA_SET_NO_PERMISSION) { VehiclePropValuePtr prop = getPropValue(property, data, count, VehiclePropertyStatus::AVAILABLE); t1 = systemTime(CLOCK_MONOTONIC); //we report to up layer DATA_SET_OK or DATA_SET_EQUAL, but we logProp only DATA_SET_OK. if (!useSharedMemory) { doHalEvent(std::move(prop)); } else { int buffer[toInt(VehicleConstant::SHARED_MEMORY_META_DATA_SIZE) / sizeof(int)]; buffer[0] = 0; buffer[1] = count; buffer[2] = crc32(0, reinterpret_cast<const uint8_t *>(data), count); VehiclePropValuePtr prop = getPropValue(property, buffer, sizeof(buffer), VehiclePropertyStatus::AVAILABLE); doHalShmEvent(std::move(prop), data, count); } t2 = systemTime(CLOCK_MONOTONIC); if (t2 - t1 > ms2ns(100)) { SLOGW("callback cost too many time! prop = %X, diff = %ld", property, t2 - t1); } } else { t2 = systemTime(CLOCK_MONOTONIC); } level = context.getLogLevel(); if (context.getForceReportFlag()) { context.setForceReportFlag(false); forceprint = true; SLOGI("force log! (%d:%d:%s)", level, ret, context.propName); } if ((level == LOGCTRL_SHOW && ret == DATA_SET_OK) || forceprint) { logProp("rp", property, data, count, context.propName, dcount, context.getSubscribe(), t1, true); } else if (level == LOGCTRL_DOWNFREQ) { dcount = context.getDropCount(); if (context.isLogExpire(t2) || forceprint) { logProp("rp", property, data, count, context.propName, dcount, context.getSubscribe(), t1, true); context.setDropCount(0); } else { context.setDropCount(dcount + 1); } }
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值