Background
Mr. A and UIM came to the rainforest to explore. Suddenly a north wind blew, a dark clouds rushed from the northern side of the sky, accompanied by a road of lightning, a burst of thunder. In an instant, the wind was furious, and the dark clouds were filled with the sky. Then, the rain of douda fell down from the sky, and there was a dark monster in front of him. He said with a calm voice: "ha ha, now that you are here, you can only survive one! ". John and his friends are stunned!
Description
In an instant, a huge N * M matrix is displayed on the ground, and each grid of the matrix has a block of 0 ~ A magic liquid of different amounts in K. The monsters each gave Xiao A and UIM a magic bottle and said, You can start from any lattice in the matrix, each time taking a step to the right or down, and end from any lattice. At the beginning, Xiao A used the magic bottle to absorb the magic liquid on the ground, and the next step was absorbed by UIM. In this way, the last step was required to be absorbed by UIM. The magic bottle has only K capacity. That is to say, if k + 1 is installed, the magic bottle will be emptied to zero. If K + 2 is installed, only 1 is left, and so on. The monster also said, who has a lot of magic liquid in the bottle at last can survive. Little A and UIM have deep feelings. How can they leave their friends alone? Silence for a moment, Xiao A had the opportunity to survive if they had the same amount of magic liquid in the bottle? John and his friends are all smiling!
Now he wants to know how many ways they can survive.
Input/Output Format
Input Format:
The first line is an integer n, m, and K separated by three spaces.
The next n rows and M columns indicate the volume of magic liquid in each matrix. Numbers in the same row are separated by spaces.
Output Format:
An integer that represents the number of methods. The result of remainder of 1 000 000 007 is output because it may be large.
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 const int maxn=807; 8 const int mod=1000000007; 9 int n,m,K,ans;10 int dp[maxn][maxn][17][2],val[maxn][maxn];11 int main(){12 cin>>n>>m>>K;K++;13 for(int i=1;i<=n;i++)14 for(int j=1;j<=m;j++)15 cin>>val[i][j];16 for(int i=1;i<=n;i++){17 for(int j=1;j<=m;j++){18 dp[i][j][val[i][j]%K][0]=1;19 }20 }21 for(int i=1;i<=n;i++){22 for(int j=1;j<=m;j++){23 for(int k=0;k<K;k++){24 //dp[i+1][j][(-(k-val[i+1][j])%K)%K][1]+=dp[i][j][k][0];dp[i+1][j][(-(k-val[i+1][j])%K)%K][1]%=mod;25 //dp[i+1][j][(-(k-val[i+1][j])%K)%K][0]+=dp[i][j][k][1];dp[i+1][j][(-(k-val[i+1][j])%K)%K][0]%=mod;26 //dp[i][j+1][(-(k-val[i][j+1])%K)%K][1]+=dp[i][j][k][0];dp[i][j+1][(-(k-val[i][j+1])%K)%K][1]%=mod;27 //dp[i][j+1][(-(k-val[i][j+1])%K)%K][0]+=dp[i][j][k][1];dp[i][j+1][(-(k-val[i][j+1])%K)%K][0]%=mod;28 dp[i][j][k][0]+=dp[i-1][j][((k-val[i][j])%K+K)%K][1];dp[i][j][k][0]%=mod;29 dp[i][j][k][0]+=dp[i][j-1][((k-val[i][j])%K+K)%K][1];dp[i][j][k][0]%=mod;30 dp[i][j][k][1]+=dp[i-1][j][(k+val[i][j])%K][0];dp[i][j][k][1]%=mod;31 dp[i][j][k][1]+=dp[i][j-1][(k+val[i][j])%K][0];dp[i][j][k][1]%=mod; 32 }33 }34 35 for(int i=1;i<=n;i++)36 for(int j=1;j<=m;j++) 37 ans=(ans+dp[i][j][0][1])%mod;38 cout<<ans<<endl;39 return 0;40 }
Escape from a and UIM