[ABC265E] Warp
Problem Statement
Takahashi is at the origin of a two-dimensional plane.
Takahashi will repeat teleporting $N$ times. In each teleportation, he makes one of the following moves:
- Move from the current coordinates $(x,y)$ to $(x+A,y+B)$
- Move from the current coordinates $(x,y)$ to $(x+C,y+D)$
- Move from the current coordinates $(x,y)$ to $(x+E,y+F)$
There are obstacles on $M$ points $(X_1,Y_1),\ldots,(X_M,Y_M)$ on the plane; he cannot teleport to these coordinates.
How many paths are there resulting from the $N$ teleportations? Find the count modulo $998244353$.
Constraints
- $1 \leq N \leq 300$
- $0 \leq M \leq 10^5$
- $-10^9 \leq A,B,C,D,E,F \leq 10^9$
- $(A,B)$, $(C,D)$, and $(E,F)$ are distinct.
- $-10^9 \leq X_i,Y_i \leq 10^9$
- $(X_i,Y_i)\neq(0,0)$
- $(X_i,Y_i)$ are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $M$ $A$ $B$ $C$ $D$ $E$ $F$ $X_1$ $Y_1$ $X_2$ $Y_2$ $\vdots$ $X_M$ $Y_M$
Output
Print the answer.
Sample Input 1
2 2 1 1 1 2 1 3 1 2 2 2
Sample Output 1
5
The following $5$ paths are possible:
- $(0,0)\to(1,1)\to(2,3)$
- $(0,0)\to(1,1)\to(2,4)$
- $(0,0)\to(1,3)\to(2,4)$
- $(0,0)\to(1,3)\to(2,5)$
- $(0,0)\to(1,3)\to(2,6)$
Sample Input 2
10 3 -1000000000 -1000000000 1000000000 1000000000 -1000000000 1000000000 -1000000000 -1000000000 1000000000 1000000000 -1000000000 1000000000
Sample Output 2
0
Sample Input 3
300 0 0 0 1 0 0 1
Sample Output 3
292172978
为了 \(O(1)\) 判断,我们可以把所有不能走的坐标用哈希表存起来。
定义 \(dp_{i,j,k}\) 为第一中移动方式移动了 \(i\) 次,第二种移动方式移动了 \(j\) 次第三种移动方式移动了 \(k\) 次的情况下,有多少种方案。
首先在哈希表上查一下这样子到达的点是否可以走,如果可以走,\(dp_{i,j,k}=dp_{i-1,j,k}+dp_{i,j-1,k}+dp_{i,j,k-1}\)
#include<cstdio>
const int N=305,S=2e9+1,P=998244353,mod=1e7+3;
typedef long long LL;
int n,m,a,b,c,d,e,f,dp[N][N][N];
LL hs[mod],ans,x,y;
LL hsh(int x,int y)
{
return 1LL*(x+S-1)*S+y+S;
}
void insert(LL a)
{
for(int i=a%mod;;i++)
{
if(i==mod)
i=0;
if(!hs[i])
{
hs[i]=a;
break;
}
}
}
int find(LL a)
{
for(int i=a%mod;;i++)
{
if(i==mod)
i=0;
if(!hs[i])
return 0;
if(hs[i]==a)
return 1;
}
}
LL tx(int x,int y,int z)
{
return 1LL*x*a+1LL*y*c+1LL*z*e;
}
LL ty(int x,int y,int z)
{
return 1LL*x*b+1LL*y*d+1LL*z*f;
}
int main()
{
scanf("%d%d%d%d%d%d%d%d",&n,&m,&a,&b,&c,&d,&e,&f);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
insert(hsh(x,y));
}
dp[0][0][0]=1;
for(int i=0;i<=n;i++)
{
for(int j=0;j+i<=n;j++)
{
for(int k=0;k+j+i<=n;k++)
{
x=tx(i,j,k),y=ty(i,j,k);
if(x<-1e9||x>1e9||y<-1e9||y>1e9||!find(hsh(x,y)))
{
if(i)
dp[i][j][k]=dp[i-1][j][k];
if(j)
dp[i][j][k]=(dp[i][j][k]+dp[i][j-1][k])%P;
if(k)
dp[i][j][k]=(dp[i][j][k]+dp[i][j][k-1])%P;
}
if(i+j+k==n)
ans=(ans+dp[i][j][k])%P;
// printf("%lld %lld %d %d %d %d\n",x,y,i,j,k,dp[i][j][k]);
}
}
}
printf("%d",ans);
return 0;
}