2021.08.31 CF1558E Up the Strip

the last blog of the summer holiday of 2021

CF1558E   Up   the   Strip \color{green}{\texttt{CF1558E Up the Strip}} CF1558E Up the Strip

[Problem] \color{blue}{\texttt{[Problem]}} [Problem]

在这里插入图片描述

[Solution] \color{blue}{\texttt{[Solution]}} [Solution]

可能会比较容易看出要用 dp 吧。

f x f_{x} fx 表示从 x x x 转移到 1 1 1 的方案数,考虑如何转移。

显然,考虑对于任意一个点 u ( x < u ≤ n ) u(x<u \leq n) u(x<un) u u u 都可以通过方式 1 1 1 转移到 x x x

现在我们需要考虑哪些 u u u 可以通过方式 2 2 2 转移到 x x x

显然,对于 ∀ u = a t + b ( 0 ≤ b < a ) \forall u = at+b(0 \leq b < a) u=at+b(0b<a),都有 ⌊ u t ⌋ = a \left \lfloor \dfrac{u}{t} \right \rfloor = a tu=a,反之亦然。

故而有满足 ⌊ u z ⌋ = x \left \lfloor \dfrac{u}{z} \right \rfloor=x zu=x u ∈ [ z x , z x + z − 1 ] u \in [zx,zx+z-1] u[zx,zx+z1]

所以,我们可以愉快地写出状态转移方程。

由于所有的 u u u 都集中在一个区间中,所以我们可以愉快地使用后缀和优化算法到 O ( n ) \mathcal{O}(n) O(n)

[code] \color{blue}{\texttt{[code]}} [code]

const int N=4e6+100;
int f[N],sum[N],n,mod;
int main(){
	scanf("%d%d",&n,&mod);
	f[n]=1;sum[n]=1;sum[n+1]=0;
	for(int i=n-1;i;i--){
		f[i]=(f[i]+sum[i+1])%mod;
		for(int j=2;1ll*i*j<=n;j++){
			f[i]=(f[i]+sum[i*j]-sum[min((i+1)*j,n+1)])%mod;
			f[i]=(f[i]+mod)%mod;
		}
		sum[i]=(sum[i+1]+f[i])%mod;
	}
	printf("%d",f[1]);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值