[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的方法从汇点開 ...
随机推荐
- GIT提交修改的项目到远程仓库
1.在项目目录下右键选择Git Bash. 2.执行提交命令三部曲 git add . //文件-暂存区,即将所有新增的文件添加到提交索引中,,add后面是"空格 点"就表示当前目 ...
- 如何平息WPS for linux启动后,笔记本风扇的怒吼
create:2022-09-06 20:02:45 WPS启动后,点击菜单栏右上角[未同步]按钮,不登录,直接关掉.几秒后,笔记本风扇嗷嗷叫.桌面conky显示wpscloudsvr进程CPU占用8 ...
- TOML格式简介
TOML(Tom's Obvious, Minimal Language)是一种用于配置文件的轻量级文本格式,旨在易于阅读和编写.它的设计目标是简单明了,同时也能表达复杂的数据结构.TOML文件通常用 ...
- numpy_tricks
Numpy Tricks 这篇文章不定期更新,主要是记录在使用numpy过程中一些有效的tricks(或者重要的API) import numpy as np numpy.where() numpy. ...
- Solution -「营业」「ABC 209F」Deforestation
Description Link. 有 \(n\) 棵树,每棵的高度为 \(a(i)\),看到一棵树对答案的贡献为 \(a(i-1)+a(i)+a(i+1)\)(未定义范围为 \(0\)),求 ...
- Solution -「营业」「CF 527C」Glass Carving
Description Link. 有一个块 \(n\times m\) 的矩形,有 \(q\) 次操作,每次把矩形横 / 竖着切一刀,问切完后的最大矩形面积. Solution 一个不同于大多数人. ...
- Solution -「洛谷 P2044」「NOI 2012」随机数生成器
Description Link. 给你一个递推式,让你求某一项的值模上 \(g\). Solution 这道题正解是矩阵.我这里给出一种分治的做法. 题目中说 $\ \ \ \ \ \ \ $ $\ ...
- PLC通过Modbus转Profinet网关连接变频器控制电机案例
在本案例中,通过使用Modbus转Profinet网关(XD-MDPN100),PLC可以通过Profinet协议与变频器进行通信和控制.这样,PLC可以实现对电机的转速调节.启停控制等功能. 同时, ...
- 如何选择适合你的HTAP数据库?
最近,在数据库行业对HTAP(混合事务/分析处理,Hybrid Transactional/Analytical Processing)这个概念宣传的非常火爆,也衍生出 Real-Time HTAP的 ...
- IO流知识汇总(不断更新)
BIO.NIO.AIO有什么区别? BIO.NIO.AIO是Java中用于处理IO的三种不同的方式,它们之间的区别如下: BIO(Blocking IO):同步阻塞IO,传统的IO模型,也称为传统IO ...