Cow Hopscotch
题目描述
The game is played on an R by C grid (2 <= R <= 750, 2 <= C <= 750), where each square is labeled with an integer in the range 1..K (1 <= K <= R*C). Cows start in the top-left square and move to the bottom-right square by a sequence of jumps, where a jump is valid if and only if
1) You are jumping to a square labeled with a different integer than your current square,
2) The square that you are jumping to is at least one row below the current square that you are on, and
3) The square that you are jumping to is at least one column to the right of the current square that you are on.
Please help the cows compute the number of different possible sequences of valid jumps that will take them from the top-left square to the bottom-right square.
输入
输出
样例输入
4 4 4
1 1 1 1
1 3 2 1
1 2 4 1
1 1 1 1
样例输出
5
分析:2个难点,状态转移和分治处理;
状态转移:ans[x][y]=∑ans[i][j](i<x,j<y)-Σans[i][j](i<x,j<y,a[i][j]=a[x][y]);
分治:由于处理左半部分,上半部分时和右半部分,下半部分没有半毛钱关系,所以分治处理;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e3+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,a[maxn][maxn],ans[maxn][maxn],check[maxn*maxn];
void gao(int l,int r)
{
if(l==r)return;
int mid=l+r>>;
gao(l,mid);
memset(check,,sizeof(check));
int all=;
for(int j=;j<=m;j++)
{
for(int i=mid+;i<=r;i++)
ans[i][j]=((ans[i][j]+all-check[a[i][j]])%mod+mod)%mod;
for(int i=l;i<=mid;i++)
all+=ans[i][j],all%=mod,check[a[i][j]]+=ans[i][j],check[a[i][j]]%=mod;
}
gao(mid+,r);
}
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&k);
rep(i,,n)rep(j,,m)scanf("%d",&a[i][j]);
ans[][]=;
gao(,n);
printf("%d\n",ans[n][m]);
//system("pause");
return ;
}