[ABC309Ex] Simple Path Counting Problem
Problem Statement
We have a grid with $N$ rows and $M$ columns. We denote by $(i,j)$ the cell in the $i$-th row from the top and $j$-th column from the left.
You are given integer sequences $A=(A_1,A_2,\dots,A_K)$ and $B=(B_1,B_2,\dots,B_L)$ of lengths $K$ and $L$, respectively.
Find the sum, modulo $998244353$, of the answers to the following question over all integer pairs $(i,j)$ such that $1 \le i \le K$ and $1 \le j \le L$.
- A piece is initially placed at $(1,A_i)$. How many paths are there to take it to $(N,B_j)$ by repeating the following move $(N-1)$ times?
- Let $(p,q)$ be the piece's current cell. Move it to $(p+1,q-1),(p+1,q)$, or $(p+1,q+1)$, without moving it outside the grid.
Constraints
- $1 \le N \le 10^9$
- $1 \le M,K,L \le 10^5$
- $1 \le A_i,B_j \le M$
先考虑如何计算某一个点到另一个点的方案数。
定义 \(dp_{i,j}\) 为到达 \((i,j)\) 的方案数,那么 \(dp_{i,j}=dp_{i-1,j-1}+dp_{i-1,j}+dp_{i-1,j+1}\)。
看似可以多项式快速幂优化,但是会发现有边界问题。
如何解决边界问题?对称一下,使 \(dp_{n+1-i}=-dp_i\),然后卷积的时候就可以把 \(j\le N\) 的限制给去掉。
\(x>0\) 的限制怎么去? 用 \(2n+2\) 的循环卷积即可。
然后循环卷积快速幂就可以了。
原题也一样,循环卷积跑出来 \((1+x+x^{-1})^N\) 的系数卷上 \(A\) 数组就可以了。
#include<bits/stdc++.h>
using namespace std;
const int N=524288,P=998244353;
int n,m,k,a,b,rr[N],ans,f[N],g[N],h[N],p,l;
int read()
{
int s=0;
char ch=getchar();
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9')
s=s*10+ch-48,ch=getchar();
return s;
}
int pown(int x,int y)
{
if(!y)
return 1;
int t=pown(x,y>>1);
if(y&1)
return 1LL*t*t%P*x%P;
return 1LL*t*t%P;
}
void ntt(int a[],int op)
{
for(int i=1;i<N;i++)
if(rr[i]<i)
swap(a[i],a[rr[i]]);
for(int md=1;md<N;md<<=1)
{
int g=pown(op? 3:332748118,(P-1)/(md<<1));
for(int i=0;i<N;i+=md<<1)
{
int pw=1;
for(int j=0;j<md;j++,pw=1LL*g*pw%P)
{
int x=a[i+j+md]*1LL*pw%P;
a[i+j+md]=(a[i+j]+P-x)%P;
(a[i+j]+=x)%=P;
}
}
}
if(!op)
{
int ivN=pown(N,P-2);
for(int i=0;i<N;i++)
a[i]=1LL*a[i]*ivN%P;
}
}
void mul(int a[],int b[])
{
ntt(a,1);
ntt(b,1);
for(int i=0;i<N;i++)
a[i]=1LL*a[i]*b[i]%P;
ntt(a,0);
for(int i=p;i<N;i++)
(a[i%p]+=a[i])%=P,a[i]=0;
}
void solve(int x)
{
if(x==1)
return;
if(x&1)
{
solve(x-1);
memcpy(h,f,sizeof(h));
for(int i=0;i<p;i++)
f[i]=(1LL*h[i]+h[(i+p-1)%p]+h[(i+1)%p])%P;
}
else
{
solve(x>>1);
memcpy(h,f,sizeof(h));
mul(f,h);
}
}
int main()
{
n=read(),m=read(),k=read(),l=read();
p=2*m+2;
for(int i=0;i<N;i++)
rr[i]=rr[i>>1]>>1|(i&1)*(N/2);
for(int i=1;i<=k;i++)
a=read(),g[a]++,(g[2*m-a+2]+=P-1)%=P;
if(n^1)
{
f[0]=f[p-1]=f[1]=1;
solve(n-1);
mul(g,f);
}
for(int i=1;i<=l;i++)
(ans+=g[read()])%=P;
printf("%d",ans);
}
[ABC309Ex] Simple Path Counting Problem的更多相关文章
- BNU 4356 ——A Simple But Difficult Problem——————【快速幂、模运算】
A Simple But Difficult Problem Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %l ...
- CF 954H Path Counting
H. Path Counting time limit per test 5 seconds memory limit per test 256 megabytes input standard in ...
- hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)
这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...
- HDU-4972 A simple dynamic programming problem
http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...
- hdu 4972 A simple dynamic programming problem(高效)
pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...
- A simple Gaussian elimination problem.(hdu4975)网络流+最大流
A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- Codeforces 954H Path Counting 【DP计数】*
Codeforces 954H Path Counting LINK 题目大意:给你一棵n层的树,第i层的每个节点有a[i]个儿子节点,然后问你树上的简单路径中长度在1~n*2-2之间的每个有多少条 ...
- UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。
/** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...
- Codeforces 954H Path Counting(DP)
题目链接 Path Counting 题意 给定一棵高度为$n$的树,给出每一层的每个点的儿子个数(某一层的所有点儿子个数相同). 令$f_{k}$为长度为$k$的路径条数,求$f_{1}, ...
- HDOJ 4975 A simple Gaussian elimination problem.
和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開 ...
随机推荐
- STM32中SWD下载不进去的解决方法
这是我第一次写自己的博客,希望以后写博客可以当做自己的个人习惯并坚持下去,作为技术分享,也欢迎各位大佬前来指正.本人本科学习的机械电子工程,了解机械制图.嵌入式编程.目前刚好学习了PCB制板,正在向着 ...
- Linux下后台运行Java程序
1.背景描述 用Java编写了一个程序(可执行的jar),需要在Linux中启动并持续运行 1.1.直接执行程序 直接执行程序后,在程序执行期间,无法在当前会话中再执行其他操作 1.2.直接执行程序后 ...
- papricice
2023-07-14 题目 题目传送门 题目大意 给定一个 \(n\) 个点的树,这 \(n\) 个点编号为 \(1\) 到 \(n\). 现在要选择断掉两条边,会形成三个连通块,假设这三个连通块内的 ...
- 在.NET Framework中使用RocketMQ(阿里云版)实战【第一章】
章节 第一章:https://www.cnblogs.com/kimiliucn/p/17662052.html 第二章: 作者:西瓜程序猿 主页传送门:https://www.cnblogs.com ...
- 《CTFshow-Web入门》01. Web 1~10
@ 目录 web1 题解 web2 题解 web3 题解 web4 题解 web5 题解 原理 web6 题解 原理 web7 题解 web8 题解 web9 题解 原理 web10 题解 ctf - ...
- PyAV 使用浅谈
背景: PyAV是一个用于音频和视频处理的Python库,它提供了一个简单而强大的接口,用于解码.编码.处理和分析各种音频和视频格式.PyAV基于FFmpeg多媒体框架,它本质上是FFmpeg 的Py ...
- iOS越狱后必装软件
iOS越狱后就跟ubuntu没两样了,很多ubuntu下常用的软件都要装一下 openssh 这个软件可以让我们能够登录iphone Apt-get 用这个软件可以安装很多软件,主要是一些工具调试类软 ...
- Vue3中的几个坑,你都见过吗?
Vue3 目前已经趋于稳定,不少代码库都已经开始使用它,很多项目未来也必然要迁移至Vue3.本文记录我在使用Vue3时遇到的一些问题,希望能为其他开发者提供帮助. 1. 使用reactive封装基础数 ...
- .NET Core 实现Excel的导入导出
目录 前言 NPOI简介 一.安装相对应的程序包 1.1.在 "管理NuGet程序包" 中的浏览搜索:"NPOI" 二.新建Excel帮助类 三.调用 3.1. ...
- ingress获取客户端真实IP
环境: k8s1.25.2.helm部署的ingress 1.进入ingress helm目录,修改values.yaml文件 2.卸载重装 helm uninstall ingress-test - ...