# AtCoder Beginner Contest 179 D题解

## Problem Statement

There are N cells arranged in a row, numbered 1,2,…,N from left to right.

Tak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the procedure described below.

You are given an integer K that is less than or equal to 10, and K non-intersecting segments [L1,R1],[L2,R2],……,[LK,RK]. Let S be the union of these K segments. Here, the segment [l,r] denotes the set consisting of all integers i that satisfy l≤i≤r.

When you are on Cell i, pick an integer d from S and move to Cell i+d.You cannot move out of the cells.

To help Tak, find the number of ways to go to Cell N, modulo 998244353.

## Constraints

2≤N≤2×105
1≤K≤min(N,10)
1≤Li≤Ri≤N
[Li,Ri]and [Lj,Rj] do not intersect (i≠j)
All values in input are integers.

## 代码

#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define pb push_back
using namespace std;
const int mod=998244353;
const int maxn=1e6+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
ll k,n,l[maxn],r[maxn],dp[maxn],sum[maxn];
int main()
{
scanf("%lld%lld",&n,&k);
for(int i=1;i<=k;i++)
scanf("%lld%lld",&l[i],&r[i]);
sum[1]=1;//dp[i]的前缀和
for(int i=2;i<=n;i++)
{
for(int j=1;j<=k;j++)
dp[i]=(dp[i]+sum[max((ll)0,i-l[j])]-sum[max((ll)0,i-r[j]-1)])%mod;//走到i的方案数,注意防止越界情况
sum[i]=(sum[i-1]+dp[i])%mod;
}
printf("%lld\n",(dp[n]+mod)%mod);
return 0;
}

• 点赞
• 评论 1
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

我他喵的

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

03-18 354