Problem:


Score : 400 points

Problem Statement

There are N cells arranged in a row, numbered 1, 2, \ldots, 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 [L_1, R_1], [L_2, R_2], \ldots, [L_K, R_K].
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 \leq i \leq 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 \leq N \leq 2 \times 10^5

1 \leq K \leq \min(N, 10)

1 \leq L_i \leq R_i \leq N

[L_i, R_i] and [L_j, R_j] do not intersect (i \neq j) 

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
L_1 R_1
L_2 R_2
:
L_K R_K

Output

Print the number of ways for Tak to go from Cell 1 to Cell N, modulo 998244353.

Sample Input 1

5 2
1 1
3 4

Sample Output 1

4

The set S is the union of the segment [1, 1] and the segment [3, 4], therefore S = \{ 1, 3, 4 \} holds.

There are 4 possible ways to get to Cell 5:

1 \to 2 \to 3 \to 4 \to 5,

1 \to 2 \to 5,

1 \to 4 \to 5 and

1 \to 5.

Sample Input 2

5 2
3 3
5 5

Sample Output 2

0

Because S = \{ 3, 5 \} holds, you cannot reach to Cell 5.
Print 0.

Sample Input 3

5 1
1 2

Sample Output 3

5

Sample Input 4

60 3
5 8
1 3
10 15

Sample Output 4

221823067

Note that you have to print the answer modulo 998244353.



Sample input:
5 2
1 1
3 4


Sample output:
4

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy

import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class Point():
    def __init__(self,y,x):
        self.x=x
        self.y=y
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputintlist(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloatlist(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    from queue import PriorityQueue
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

def to10(A):
    ret=0
    for a in range(len(A)-1,-1,-1):
        ret+=X[a]*2**(N-1-a)
    return ret
######################################################################################################
md=998244353
n,k=inputintlist()
S=set()
for a in range(k):
    l,r=inputintlist()
    for b in range(l,r+1):
        S.add(b)
dp=[0]*n
dp[0]=1
for a in range(n):
    for b in S:
        if a+b<n:
            dp[a+b]+=dp[a]
            dp[a+b]%=md
print(dp[n-1])

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
MOD=998244353
S=[0]*(N+1)
for _ in range(K):
    L,R=map(int,input().split())
    for i in range(L,R+1):
        S[i]=1
dp=[0]*(N+1)
dp[1]=1
for i in range(2,N+1):
    for j in range(i):
        if S[j]==1:
            dp[i]+=dp[i-j]
            dp[i]%=MOD
print(dp[N]%MOD)

Submission status: Time Limit Exceeded

Solution code:
# もらうdpでとく
N, K = map(int, input().split())  # N個のマス, K個の集合
move = []
for _ in range(K):
    l, r = map(int, input().split())
    for d in range(l, r+1):
        move.append(d)
move.sort()

MOD = 998244353
dp = [0]*(N)  # スタート地点0~ゴール地点N-1までのN個
dp[0] = 1
for i in range(N):
    for d in move:
        if i-d < 0:  # 遷移元がないならcontinue
            continue
        dp[i] += dp[i-d]
        dp[i] %= MOD
print(dp[N-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
MOD = 998244353

N, K = [int(a) for a in input().split()]
L = []
R = []
S = set()
for _ in range(K):
    l,r = [int(a) for a in input().split()]
    L.append(l)
    R.append(r)
    S = S | set(list(range(l, r+1)))
# print(S)

q = deque()
q.append(1)
ans = 0
while q:
    v = q.popleft()
    if v == N:
        ans = (ans%MOD + 1)%MOD
    else:
        for n in S:
            if n+v <= N:
                q.append(n+v)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7
M = 998244353


def main():
    N, K = map(int, readline().split())
    S = set()
    for k in range(K):
        L, R = map(int, readline().split())
        S |= set(range(L, R + 1))

    dp = [0] * (N + 1)
    dp[1] = 1
    S = list(S)

    for i in range(1, N + 1):
        for s in S:
            if i - s > 0:
                dp[i] += dp[i - s]
                dp[i] %= M
            else:
                continue
    print(dp[N])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, K  = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(K)]
L.sort()
S = set()
for l in L:
  S = S|set([i for i in range(l[0], l[1]+1)])
DP = [0]*N
DP[0]=1
for i in range(1, N):
  for j in S:
    if i>=j:
      DP[i] += DP[i-j]
      DP[i] %= 998244353
    else:
      break
  continue
print(DP[-1])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
num = []

for i in range(k):
    l,r = map(int,input().split())
    for j in range(l,r+1):
        num.append(j)

A = [1]
for i in range(1,n):
    a = 0
    for j in num:
        if i-j > -1:
            a += A[i-j]
        else:
            a += 0
    A.append(a)
print(A[n-1]%998244353)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int, input().split())
MOD = 998244353
a = [0]*(n+1)

for i in range(k):
    l,r=map(int, input().split())
    a[l-1] += 1
    a[r] -= 1

now = 0
s = []
for i in range(n):
    now += a[i]
    if(now > 0): s.append(i+1)

ans = [0]*n
ans[0] = 1
for i in range(0, n):
    for j in s:

        if(j+i < n):
            ans[j+i] += ans[i]
            ans[j+i] = ans[j+i] % MOD

print(ans[-1])

Submission status: Time Limit Exceeded

Solution code:
mod=998244353
n,k=map(int,input().split())
step=[]
for i in range(k):
    l,r=map(int,input().split())
    for p in range(l,r+1):
        step.append(p)
step=sorted(step)
dp=[0 for i in range(n+1)]
dp[1]=1
for i in range(1,n+1):
    for s in step:
        if i+s<=n:
            dp[i+s]+=dp[i]
        else:
            break
print(dp[n]%mod)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

def solve():
    MOD = 998244353

    def makeBIT(numEle):
        numPow2 = 2 ** (numEle-1).bit_length()
        data = [0] * (numPow2+1)
        return data, numPow2
    def setInit(As):
        for iB, A in enumerate(As, 1):
            data[iB] = A
        for iB in range(1, numPow2):
            i = iB + (iB & -iB)
            data[i] += data[iB]
    def addValue(iA, A):
        iB = iA + 1
        while iB <= numPow2:
            data[iB] += A
            iB += iB & -iB
    def getSum(iA):
        iB = iA + 1
        ans = 0
        while iB > 0:
            ans += data[iB]
            iB -= iB & -iB
        return ans
    def getRangeSum(iFr, iTo):
        return getSum(iTo) - getSum(iFr-1)


    N, K = map(int, input().split())
    LRs = [tuple(map(int, input().split())) for _ in range(K)]

    data, numPow2 = makeBIT(N)

    addValue(0, 1)

    for i in range(1, N):
        for L, R in LRs:
            x, y = i-R, i-L
            if y < 0:
                continue
            if x < 0:
                x = 0
            v = getRangeSum(x, y)
            addValue(i, v)

    ans = getRangeSum(N-1, N-1) % MOD
    print(ans)


solve()

Submission status: Time Limit Exceeded

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  field=[LI() for _ in range(k)]
  field.sort()

  rng=[]
  for x,y in field:
    for z in range(x,y+1):
      rng.append(z)

  # print(rng)

  dp=[0]*(n+1)
  dp[1]=1

  for i in range(1,n):
    for dx in rng:
      if i+dx>n:
        break
      dp[i+dx]+=dp[i]
      dp[i+dx]%=mod
    # print(dp)
  
  return dp[n]%mod

# main()
print(main())

Submission status: Time Limit Exceeded

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def soinsubunkai_e(n,m): #高速素因数分解 m[n]=nを割ることの出来る最小の素数
    a = set()
    i = 1
    while n>1:
        a.add(m[n])
        n=n//m[n]
    nokori=n
    a.add(nokori)
    return a

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]


def era(n): #b[n]=nを割ることの出来る最小の素数　の配列を生成　エラトステネスの篩
    b=[i for i in range(n+1)]
    for i in range(2,int(math.sqrt(n)+10)):
        j=1
        temp=i*j
        while temp<=n:
            b[temp]=min(i,b[temp])
            temp+=i
    return b


def main():
    #l,r,d=map(int,input().split())
    MODT=998244353
    n,k=map(int,input().split())
    step= set()
    for i in range(k):
        l,r=map(int,input().split())
        aa=[l+j for j in range(r-l+1)]
        aa=set(aa)
        step=step.union(aa)
    step2=set()
    for i in step:
        step2.add(i%MODT)
    dp=[0 for i in range(n*2+2)]
    dp[1]=1
    for i in range(1,n+1):
        for j in step2:
            dp[i+j]+=dp[i]
            dp[i+j]%=MODT
 #   print(dp)
    print(dp[n]%MODT)






if __name__ == "__main__":

    main()

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
s = set()
ans = [0]*n + [1]
for _ in range(k):
  a, b = map(int, input().split())
  i = a
  while i<=b:
    s.add(i)
    i = i + 1

for i in reversed(range(1, n)):
  for j in s:
    if i + j <= n:
      ans[i] = ans[i] + ans[i+j]
print(ans[1]%998244353)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
import numpy as np
MOD = 998244353

lst = np.zeros((N+1), int)
for _ in range(K):
  l, r = map(int, input().split())
  for i in range(l, r+1):
    lst[i] = 1
    
#print(lst)

dp = np.zeros((N), int)
dp[0] = 1

for i in range(N):
  dp[i:] += lst[:N-i] * dp[i] % MOD
  #print(dp)
  #print(dp[i:], lst[:N-i])
   
print(dp[N-1] % MOD)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
grid = []
for i in range(k):
    array = list(map(int, input().strip().split(' ')))
    grid.append(array)

S = []

for i in range(k):
    for j in range(grid[i][1]-grid[i][0]+1):
        S.append(grid[i][0]+j)
S.sort()
#print(S)
array = [1]

for i in range(2,n+1):
    count = 0
    for j in S:
        if i - j > 0:
            count += array[i-j-1]
            count = count % 998244353
            #print(i,j,array[i-j-1])
    array.append(count)
    #print(i,count)

print(array[n-1])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,raw_input().split())
jis = [map(int, raw_input().split()) for _ in range(k)]
jis.sort()
dp = [0 for _ in range(n)]
dp[0] = 1
mod = 998244353
for i in range(1,n):
	flag = True
	for u,v in jis:
		if flag is False: break
		for w in range(u,v+1):
			if i-w>=0:
				dp[i] += dp[i - w]
				dp[i] %= mod
			else:
				flag = False
				break
print dp[n-1]

Submission status: Time Limit Exceeded

Solution code:
q = 998244353

N, K = map(int, input().split())

S = []
for _ in range(K):
    S.append(list(map(int, input().split())))
S.sort()

result = [0] * N
result[0] = 1

for i in range(N):
    for l, r in S:
        if i + l >= N:
            break
        for s in range(l, r+1):
            if i + s < N:
                result[i + s] = (result[i + s] + result[i]) % q
            else:
                break

print(result[-1])

Submission status: Time Limit Exceeded

Solution code:
import math

class SegmentTree():
    def __init__(self, size):
        self.size = 2**((size-1).bit_length())
        self.val = [0]*(self.size*2)
    
    def update(self, i, x):
        i += self.size
        self.val[i] = x
        
        while i > 0:
            i = i >> 1
            self.val[i] = self.val[2*i] + self.val[2*i+1]
    
    def rangeSum(self, l, r):
        l += self.size
        r += self.size

        l_sum = 0
        r_sum = 0

        while l < r:
            if l & 1:
                l_sum += self.val[l]
                l += 1
            l = l >> 1

            if r & 1:
                r -= 1
                r_sum += self.val[r]
            r = r >> 1

        return l_sum + r_sum
        
p = 998244353

N, K = map(int, input().split())

hop = []

for i in range(K):
    L, R = map(int, input().split())
    for j in range(L, R + 1):
        hop.append(j)

dp = SegmentTree(N)

dp.update(0,1)

for i in range(N - 1):
    for j in range(len(hop)):
        d_val = dp.rangeSum(i, i + 1) % p
        if i + hop[j] < N:
            d_val_n = dp.rangeSum(i + hop[j], i + hop[j] + 1) % p
            dp.update(i + hop[j], d_val_n + d_val)

print(dp.rangeSum(N - 1, N) % p)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, k = map(int, input().split())
    ds = set()
    for _ in range(k):
        l, r = map(int, input().split())
        ds = ds | {i for i in range(l, r + 1)}
    dp = [0] * (n + 1)
    dp[1] = 1

    for i in range(2, n + 1):
        for d in ds:
            if i - d >= 0:
                dp[i] += dp[i - d]

    print(dp[-1] % 998244353)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    S = set()
    for _ in range(K):
        l, r = map(int, input().split())
        for n in range(l, r + 1):
            S.add(n)
    MOD = 998244353

    dp = [0] * N
    dp[0] = 1
    for i in range(N):
        for d in S:
            if i >= d:
                dp[i] += dp[i - d] % MOD
    print(dp[-1] % MOD)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
lr = [map(int, input().split()) for i in range(k)]
l, r = zip(*lr)


m = 998244353


d = []
for i in range(k):
    for j in range(l[i],r[i]+1):
        d.append(j)

dp = [0 for i in range(n)]
dp[0] = 1

for i in range(n):
    for a in d:
        if i + a >= n:
            continue
        dp[i+a] += dp[i]
        dp[i+a] %= m

print(dp[n-1])

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
value = []
for _ in range(k):
    l, r = map(int, input().split())
    tmp = [i for i in range(l, r + 1)]
    value += tmp
value.sort()
mod = 998244353
dp = [0 for _ in range(n)]
dp[0] = 1
for i in range(n - 1):
    for dist in value:
        if i + dist <= n - 1:
            dp[i + dist] = (dp[i] + dp[i + dist]) % mod
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
s = set()
ans = [0]*n + [1] + [0]*n
for _ in range(k):
  a, b = map(int, input().split())
  i = a
  while i<=b:
    s.add(i)
    i = i + 1

for i in reversed(range(1, n)):
  for j in s:
    ans[i] = ans[i] + ans[i+j]
print(ans[1]%998244353)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = [False]*(N+1)
for _ in range(K):
    l, r = map(int, input().split())
    for a in range(l, r+1):
        A[a] = True

S = []
for i in range(1,N+1):
    if A[i]:
        S.append(i)
        
mod = 998244353
        
table = [0]*N
table[0] = 1

for i in range(N):
    for s in S:
        if i + s < N:
            table[i+s] += table[i]
            table[i+s] %= mod
            
print(table[N-1]%mod)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N,K = map(int,input().split())
S = []
dp = [0]*N
dp[0] = 1
MOD = 998244353
for i in range(K):
    l,r = map(int,input().split())
    S.append((l,r))

for i in range(1,N):
    for l,r in S:
        if i-r>=0:
            for x in range(l,r+1):
                dp[i] += dp[i-x]
        else:
            for x in range(l,r+1):
                if i-x>=0:
                    dp[i] += dp[i-x]
                else:
                    break
print(dp[N-1]%MOD)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(10**8)
n, k = map(int, input().split())
a = []
for _ in range(k):
    l, r = map(int, input().split())
    for u in range(l, r+1):
        a.append(u)
a.sort()
mod = 998244353


@lru_cache(maxsize=10**8)
def calc(x):
    if x <= 0:
        return 1
    ret = 0
    for u in a:
        if x - u >= 0:
            ret += calc(x - u)
            ret %= mod
    return ret


print(calc(n-1))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,raw_input().split())
jis = [map(int, raw_input().split()) for _ in range(k)]
dp = [0 for _ in range(n)]
dp[0] = 1
mod = 998244353
for i in range(1,n):
	for u,v in jis:
		for w in range(u,v+1):
			if i-w>=0:
				dp[i] += dp[i - w]
				dp[i] %= mod
print dp[n-1]

Submission status: Time Limit Exceeded

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def soinsubunkai_e(n,m): #高速素因数分解 m[n]=nを割ることの出来る最小の素数
    a = set()
    i = 1
    while n>1:
        a.add(m[n])
        n=n//m[n]
    nokori=n
    a.add(nokori)
    return a

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]


def era(n): #b[n]=nを割ることの出来る最小の素数　の配列を生成　エラトステネスの篩
    b=[i for i in range(n+1)]
    for i in range(2,int(math.sqrt(n)+10)):
        j=1
        temp=i*j
        while temp<=n:
            b[temp]=min(i,b[temp])
            temp+=i
    return b


def main():
    #l,r,d=map(int,input().split())
    MODT=998244353
    n,k=map(int,input().split())
    step= set()
    for i in range(k):
        l,r=map(int,input().split())
        aa=[l+j for j in range(r-l+1)]
        aa=set(aa)
        step=step.union(aa)
    dp=[0 for i in range(n*2+2)]
    dp[1]=1
    for i in range(1,n+1):
        for j in step:
            dp[i+j]+=dp[i]
            dp[i+j]%=MODT
 #   print(dp)
    print(dp[n]%MODT)






if __name__ == "__main__":

    main()

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
mod =  998244353
S = set([])
for i in range(k):
    L,R = map(int,input().split())
    for j in range(L,R+1):
        S.add(j)
S = list(S)
S = sorted(S)
dp = [0]*n
dp[0] = 1
for i in range(n):
    for x in S:
        if i+x < n:
            dp[i+x] += dp[i]
            dp[i+x] %= mod
print(dp[-1]%mod)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
mod = 998244353
s = list()
for _ in range(k):
    l, r = map(int, input().split())
    s += range(l, r + 1)
s.sort()
dp = [0 for _ in range(n)]
dp[0] = 1
for i in range(n):
    for x in s:
        if x + i > n - 1:
            break
        dp[i + x] = (dp[i + x] + dp[i]) % mod

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
MOD = 998244353
N, K = [int(x) for x in input().split()]
S = set()
for _ in range(K):
    L, R = [int(x) for x in input().split()]
    for i in range(L, R+1):
        S.add(i)
dp = [0] * (N+1)
dp[0] = 1
for i in range(N):
    for s in S:
        if i + s >= N: break
        dp[i+s] += dp[i]
print(dp[N-1] % MOD)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
l, r = map(int, input().split())
s = list(range(l, r+1))
for i in range(1, k):
    l, r = map(int, input().split())
    s = s + list(range(l, r+1))
dp = [0] * n
dp[0] = 1
for i in range(1, n):
    for j in s:
        if i >= j:
            dp[i] += dp[i-j]
    dp[i] = dp[i]%998244353
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
L, R = [], []
for _ in range(K):
  l, r = map(int, input().split())
  L.append(l)
  R.append(r)
mod = 998244353

dp = [0] * N
dp[0] = 1

for i in range(N):
  for l, r in zip(L, R):
    for j in range(l, r + 1):
      if i - j >= 0:
        dp[i] = (dp[i] + dp[i - j]) % mod

print(dp[-1])