Jzoj P6311 Mobitel___優化轉移狀態dp

題目大意:

給定一個 r 行 s 列的矩陣,每個格子裏都有一個正整數。
問如果從左上角走到右下角,且每次只能向右或向下走到相鄰格子,那麼使得路徑上所有數的乘積不小於 n 的路徑有多少條?
由於答案可能很大,所以請輸出答案對 10^9+7 取模的結果。
1<=r,s<=300,1<=n<=10^6,矩陣中的數不超過10610^6

分析:

fi,j,kf_{i,j,k}表示走到(i,j)(i,j),狀態爲k時的方案數。
k爲b[k]*b[k]後當前位置的乘積會>=n
b[k]的狀態時可以發現最多隻有2n2\sqrt{n}
就可以過了

代碼:

#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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章