Jzoj P6311 Mobitel___优化转移状态dp

题目大意:

给定一个 r 行 s 列的矩阵,每个格子里都有一个正整数。
问如果从左上角走到右下角,且每次只能向右或向下走到相邻格子,那么使得路径上所有数的乘积不小于 n 的路径有多少条?
由于答案可能很大,所以请输出答案对 10^9+7 取模的结果。
1<=r,s<=300,1<=n<=10^6,矩阵中的数不超过 1 0 6 10^6 106

分析:

f i , j , k f_{i,j,k} fi,j,k表示走到 ( i , j ) (i,j) (i,j),状态为k时的方案数。
k为 ∗ b [ k ] *b[k] b[k]后当前位置的乘积会>=n
b[k]的状态时可以发现最多只有 2 n 2\sqrt{n} 2n
就可以过了

代码:

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>

#define rep(i, st, ed) for (int i = st; i <= ed; i++)
#define rwp(i, ed, st) for (int i = ed; i >= st; i--)

#define M 1000000
#define N 305

using namespace std;

typedef long long ll;

const int mo = 1000000007;

int dp[2][N][2005], a[N][N], orz[M+5], id[M+5], tai[2005];
int r, s, n, cnt, Q, ans;

void read(int &x) {
	int f = 1; x = 0; char s = getchar();
	while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
	while (s >= '0' && s <= '9') { x = x * 10 + (s - '0'); s = getchar(); }
	x = x * f;	
}

int ged(int x, int y) {
	return x / y + ((x % y) ? 1 : 0);
}

int main() {
	//freopen("data.in", "r", stdin);
	freopen("mobitel.in", "r", stdin);
	freopen("mobitel.out", "w", stdout);
	read(r); read(s); read(n);
	rep(i, 1, r) 
		rep(j, 1, s) read(a[i][j]);
	rep(i, 1, n) orz[i] = ged(n, i);
	rep(i, 1, n) 
	    if (orz[i] != orz[i - 1]) tai[++cnt] = orz[i], id[orz[i]] = cnt; 
	dp[1][1][id[ged(n, a[1][1])]] = 1;
	int now = 1;
	rep(i, 1, r) { 
        rep(j, 1, s)
            rep(k, 1, cnt) {
                if (i != r) dp[now ^ 1][j][id[ged(tai[k], a[i + 1][j])]] = (dp[now ^ 1][j][id[ged(tai[k], a[i + 1][j])]] + dp[now][j][k]) % mo;
                if (j != s) dp[now][j + 1][id[ged(tai[k], a[i][j + 1])]] = (dp[now][j + 1][id[ged(tai[k], a[i][j + 1])]] + dp[now][j][k]) % mo;
			    if (i != r) dp[now][j][k] = 0;
			}
		now ^= 1;
    } 
	printf("%d\n", dp[r & 1][s][cnt]);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值