P1373 小a和uim之大逃离: https://www.luogu.org/problemnew/show/P1373
题意:
在一个矩阵中,小A和小B轮流取数,小A可以从任意点先取,小B后取,最后一步必须是小B取的。每次都是只能向下走一格或者向右走一个格子。问小B拿好后,两人拿的数值和是一样的情况数。
思路:
DP,递推,感觉这道题比较难想的就是每个点要开一维记录两人不同差值的情况数。dp[i][j][h][l] 表示在点 (i,j),差值为h,小A还是uim取液体的方案数(0-->小A 1-->小B)
dp[i][j][h][1]+=(dp[i-1][j][(h+a[i][j])%k][0]) // uim取,差值就变小了,即从高的差值状态拉下来/ dp[i][j][h][1]+=(dp[i][j-1][(h+a[i][j])%k][0] dp[i][j][h][0]+=(dp[i-1][j][((h-a[i][j])%k + k )%k][1]) // 小A取, dp[i][j][h][0]+=(dp[i][j-1][((h-a[i][j])%k + k )%k][1]) 初始化:dp[i][j][a[i][j]][0]=1; // 一开始小A可以从任意点开始
//#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 802; int dp[maxn][maxn][20][2],mp[maxn][maxn]; int n,m,k; int main(){ scanf("%d%d%d", &n, &m, &k); k++; for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ scanf("%d", &mp[i][j]); dp[i][j][ mp[i][j]%k ][0] = 1; } } int ans = 0; for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ // cout<<dp[i][j][mp[i][j]%k][0]<<endl; for(int p = 0; p <k; p++){ if(i > 1) dp[i][j][p][1] = (dp[i][j][p][1] + dp[i-1][j][ ((p + mp[i][j])% k + k)%k][0])%mod; if(j > 1) dp[i][j][p][1] = (dp[i][j][p][1] + dp[i][j-1][ ((p + mp[i][j])% k + k)%k][0])%mod; if(i > 1) dp[i][j][p][0] = (dp[i][j][p][0] + dp[i-1][j][ ((p - mp[i][j])% k + k)%k][1])%mod; if(j > 1) dp[i][j][p][0] = (dp[i][j][p][0] + dp[i][j-1][ ((p - mp[i][j])% k + k)%k][1])%mod; } ans = (ans + dp[i][j][0][1] )% mod; } } printf("%d ", ans); return 0; }