[ABC265E] Warp

该博客讨论了一个二维平面上的路径计数问题,其中一个人物Takahashi可以在三种预设移动方式下进行最多N次传送。平面上存在M个障碍点,Takahashi不能传送到这些点。博客介绍了如何使用动态规划和哈希表来计算在不碰到障碍的情况下可能的路径数量,并给出了样例输入和输出,以及对应的解决方案代码。
摘要由CSDN通过智能技术生成

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
有三种步伐,发现我们在坐标系上很难 dp。因为只有三种移动方式,所以我们考虑 dp 每种操作了多少次。

为了 \(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;	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值