[Topic] DP - Los P1373 small valley and a great escape uim の

At first glance: the feeling can cut.

GO: Portal


 

Look at the data, that is O (nmk) of.

Try set f [i] [j] [k] [l] when expressed in (i, j), respectively, the total number of programs at this time k and l of the drug.

Here two people walking directly counted as one step.

f [i] [j] [k] [l] = f [i-2] [j] [ka [i-1] [j]] [la [i] [j]] + ... denote by right right, lower right, lower right point of the program number reaches the lower lower.

Click pretreatment, as a starting point to a certain point, then f [i] [j] [0] [0] = 1.

The time complexity of O (nmkk) is too large, no need to find two people walking step count (I'm so stupid)

Bluntly,

f [i] [j] [k] [0/1] represents (i, j) position, with the difference of the two liquids k, 0 represents a walking, 1 uim go.

Transfer nothing so hard to think.

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int read(){
 4     int x=0,f=1;
 5     char c=getchar();
 6     while(!isdigit(c)){
 7         if(c=='-') f=-1;
 8         c=getchar();
 9     }
10     while(isdigit(c)){
11         x=x*10+c-'0';
12         c=getchar();
13     }
14     return x*f;
15 }
16 typedef long long ll;
17 const int N=810;
18 const int p=1e9+7;
19 int n,m,k,mod;
20 int a[N][N];
21 int f[N][N][16][2];
22 int main(){
23     n=read();m=read();k=read();
24     mod=k+1;
25     for(int i=1;i<=n;i++){
26         for(int j=1;j<=m;j++){
27             f[i][j][a[i][j]=read()][0]=1;
28         }
29     }
30     for(int i=1;i<=n;i++){
31         for(int j=1;j<=m;j++){
32             for(int c=0;c<=k;c++){
33                 if(i-1>0){
34                     (f[i][j][c][0]+=f[i-1][j][(c-a[i][j]+mod)%mod][1])%=p;
35                     (f[i][j][c][1]+=f[i-1][j][(c+a[i][j])%mod][0])%=p;
36                 } 
37                 if(j-1>0){
38                     (f[i][j][c][0]+=f[i][j-1][(c-a[i][j]+mod)%mod][1])%=p;
39                     (f[i][j][c][1]+=f[i][j-1][(c+a[i][j])%mod][0])%=p;
40                 } 
41             }
42         }
43     }
44     ll ans=0;
45     for(int i=1;i<=n;i++){
46         for(int j=1;j<=m;j++){
47             ans+=f[i][j][0][1];
48             ans%=p;
49         }
50     }
51     printf("%lld",ans);
52     return 0;
53 }

Guess you like

Origin www.cnblogs.com/Nelson992770019/p/11574534.html