input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
c = [list(map(int,input().split())) for _ in range(3)] ok = True lis = [[0,1],[1,2],[2,0]] d = [[c[l[0]][i]-c[l[1]][i] for i in range(3)]for l in lis] e = [[c[i][l[0]]-c[i][l[1]] for i in range(3)]for l in lis] for i in range(3): if d[i].count(d[i][0])!=3: ok = False if e[i].count(e[i][0])!=3: ok = False if ok: print('Yes') else: print('No')
c = [list(map(int,input().split())) for _ in range(3)] a = [0]*3 b = [0]*3 ok = True b = c[0] a[1] = c[1][0]-b[0] a[2] = c[2][0]-b[0] for i in range(3): for j in range(3): if c[i][j]!=a[i]+b[j]: ok = False if ok: print('Yes') else: print('No')
p03435
c = [] for _ in range(3): c.append(list(map(int, input().split()))) a = [] for i in range(3): tmp = [] for j in range(3): tmp.append(c[i][j%3] - c[i][(j+1)%3]) a.append(tmp) for i in range(2): for j in range(3): if a[i][j]!=a[i+1][j]: print('No') break else: continue break else: print('Yes')
c = [] for _ in range(3): c.append(list(map(int, input().split()))) for i in range(2): if c[i][0]-c[i][1]!=c[i+1][0]-c[i+1][1]: print('No') break if c[i][1]-c[i][2]!=c[i+1][1]-c[i+1][2]: print('No') break if c[i][2]-c[i][0]!=c[i+1][2]-c[i+1][0]: print('No') break else: print('Yes')
p03435
c=[0]*3 c_max=0 for i in range(3): c[i]=list(map(int,input().split())) c_max=max(c_max,max(c[i])) c_max+=1 for a1 in range(c_max): for a2 in range(c_max): for a3 in range(c_max): for b1 in range(c_max): for b2 in range(c_max): for b3 in range(c_max): if a1+b1==c[0][0] and a1+b2==c[0][1] and a1+b3==c[0][2] and a2+b1==c[1][0] and a2+b2==c[1][1] and a2+b3==c[1][2] and a3+b1==c[2][0] and a3+b2==c[2][1] and a3+b3==c[2][2]: print('Yes') exit() print('No')
c=[0]*3 c_max=0 for i in range(3): c[i]=list(map(int,input().split())) c_max=max(c_max,max(c[i])) c_max+=1 for a1 in range(c_max): b1 = c[0][0] - a1 b2 = c[0][1] - a1 b3 = c[0][2] - a1 a2 = c[1][0] - b1 a3 = c[2][0] - b1 if a1+b1==c[0][0] and a1+b2==c[0][1] and a1+b3==c[0][2] and a2+b1==c[1][0] and a2+b2==c[1][1] and a2+b3==c[1][2] and a3+b1==c[2][0] and a3+b2==c[2][1] and a3+b3==c[2][2]: print('Yes') exit() print('No')
p03435
area=[list(map(int,input().split())) for i in range(3)] count=0 for a1 in range(area[0][0]+1): b1=area[0][0]-a1 b2=area[0][1]-a1 b3=area[0][2]-a1 a2=area[1][0]-b1 a3=area[2][0]-b1 if b1>=0 and b2>=0 and b3>=0 and a2>=0 and a3>=0: if a2+b2== area[1][1]: if a2+b3==area[1][2]: if a3+b1==area[2][0]: if a3+b2==area[2][1]: if a3+b3==area[2][2]: print('Yes') exit() print('No')
C=[list(map(int,input().split())) for i in range(3)] for a1 in range(101): b1=C[0][0]-a1 b2=C[0][1]-a1 b3=C[0][2]-a1 a2=C[1][0]-b1 a3=C[2][0]-b1 if b1>=0 and b2>=0 and b3>=0 and a2>=0 and a3>=0: if a2+b2==C[1][1]: if a2+b2==C[1][1]: if a2+b3==C[1][2]: if a3+b1==C[2][0]: if a3+b2==C[2][1]: if a3+b3==C[2][2]: print('Yes') exit() print('No')
p03435
grid = [] for _ in range(3): grid.append((list)(list(map(int, input().split())))) for i in range(3): j = (i+1) % 3 diff1 = grid[i][0] - grid[j][0] diff2 = grid[i][1] - grid[j][1] diff3 = grid[i][2] - grid[j][2] if diff1 != diff2 or diff2 != diff3 or diff1 != diff3: print("No") exit() for i in range(3): j = (i+1) % 3 diff1 = grid[0][i] - grid[0][j] diff2 = grid[1][i] - grid[1][j] diff3 = grid[2][i] - grid[2][j] if diff1 != diff2 or diff2 != diff3 or diff1 != diff3: print("No") exit() print("Yes")
grid = [] for _ in range(3): grid.append((list)(list(map(int, input().split())))) for i in range(3): j = (i+1) % 3 diff1 = grid[i][0] - grid[j][0] diff2 = grid[i][1] - grid[j][1] diff3 = grid[i][2] - grid[j][2] if diff1 != diff2 or diff2 != diff3: print("No") exit() print("Yes")
p03435
def main(): c = [list(map(int, input().split())) for _ in range(3)] N = 100 for i in range(-N, N+1): for j in range(-N, N+1): for k in range(-N, N+1): if c[0][0] - i == c[0][1] - j == c[0][2] - k: if c[1][0] - i == c[1][1] - j == c[1][2] - k: if c[2][0] - i == c[2][1] - j == c[2][2] - k: print('Yes') return print('No') main()
a, b, c = list(map(int, input().split())) d, e, f = list(map(int, input().split())) g, h, i = list(map(int, input().split())) if a - b == d - e == g - h and b - c == e - f == h - i: print('Yes') else: print('No')
p03435
c = [list(map(int, input().split())) for i in range(3)] q1 = c[0][0] - c[0][1] == c[1][0] - c[1][1] == c[2][0] - c[2][1] q2 = c[0][0] - c[0][2] == c[1][0] - c[1][2] == c[2][0] - c[2][2] q3 = c[0][0] - c[1][0] == c[0][1] - c[1][1] == c[0][2] - c[1][2] q4 = c[0][0] - c[2][0] == c[0][1] - c[2][1] == c[0][2] - c[2][2] print(("Yes" if q1 and q2 and q3 and q4 else "No"))
[(a, b, c), (d, e, f), (g, h, i)] = [ list(map(int, input().split())) for i in range(3)] q1 = a - b == d - e== g - h q2 = a - c == d - f == g - i q3 = a - d == b - e== c - f q4 = a - g == b - h == c - i print(("Yes" if q1 and q2 and q3 and q4 else "No"))
p03435
def resolve(): g = [list(map(int, input().split())) for _ in range(3)] t = [[None]*3 for _ in range(3)] m = max(list(map(max, g))) for a in range(m+1): for b in range(m+1): for c in range(m+1): for d in range(m+1): for e in range(m+1): for f in range(m+1): y = [a, b, c] x = [d, e, f] ans = True for i in range(3): for j in range(3): if g[i][j] != y[i] + x[j]: ans = False if ans: print('Yes') return print('No') resolve()
def resolve(): g = [list(map(int, input().split())) for _ in range(3)] m = max(list(map(max, g))) for a in range(m+1): for b in range(m+1): for c in range(m+1): y = [a, b, c] x = [g[i][i] - y[i] for i in range(3)] ans = True for i in range(3): for j in range(3): v = y[i] + x[j] if g[i][j] != v: ans = False if ans: print('Yes') return print('No') resolve()
p03435
c = [list(map(int, input().split())) for i in range(3) ] ans = False for i in range(101) : for j in range(101) : for k in range(101) : b1 = c[0][0] - i b2 = c[1][0] - i b3 = c[2][0] - i if j + b1 != c[0][1] : continue elif j + b2 != c[1][1] : continue elif j + b3 != c[2][1] : continue elif k + b1 != c[0][2] : continue elif k + b2 != c[1][2] : continue elif k + b3 != c[2][2] : continue ans = True if ans : print('Yes') else : print('No')
c = [list(map(int, input().split())) for i in range(3) ] ans = False for i in range(101) : a1 = 0 b1 = c[0][0] b2 = c[0][1] b3 = c[0][2] a2 = c[1][0] - b1 a3 = c[2][0] - b1 a = [] a.append(a1) a.append(a2) a.append(a3) if a2 + b2 != c[1][1] : continue elif a3 + b2 != c[2][1] : continue elif a2 + b3 != c[1][2] : continue elif a3 + b3 != c[2][2] : continue ans = True break if ans : print('Yes') else : print('No')
p03435
c = [list(map(int, input().split())) for i in range(3) ] ans = False for i in range(101) : a1 = 0 b1 = c[0][0] b2 = c[0][1] b3 = c[0][2] a2 = c[1][0] - b1 a3 = c[2][0] - b1 a = [] a.append(a1) a.append(a2) a.append(a3) if a2 + b2 != c[1][1] : continue elif a3 + b2 != c[2][1] : continue elif a2 + b3 != c[1][2] : continue elif a3 + b3 != c[2][2] : continue ans = True break if ans : print('Yes') else : print('No')
c = [list(map(int, input().split())) for i in range(3) ] ans = True X = 0 b = [c[i][0] - X for i in range(3)] a = [c[0][i] - b[0] for i in range(3)] for i in range(3) : for j in range(3) : if a[j] + b[i] != c[i][j] : ans = False if ans : print('Yes') else : print('No')
p03435
from itertools import product C = [0 for _ in range(3)] for i in range(3): C[i] = list(map(int, input().split())) x = [0] * 3 y = [0] * 3 for i in range(3): y[i] = C[0][i] - x[0] x[i] = C[i][0] - y[0] ans = 'Yes' for i, j in product(list(range(3)), list(range(3))): if(x[i] + y[j] != C[i][j]): ans = 'No' break print(ans)
C = [[int(x) for x in input().split()] for _ in range(3)] a = [0] * 3 b = [0] * 3 for i in range(3): b[i] = C[0][i] - a[0] a[i] = C[i][0] - b[0] ans = 'Yes' for i in range(3): for j in range(3): if C[i][j] != a[i] + b[j]: ans = 'No' break print(ans)
p03435
l=[] for i in range(3): l1=list(map(int,input().split())) l.append(l1) flag=0 for i in range(-1000,1000,): y0=l[0][0]-i y1=l[1][0]-i y2=l[2][0]-i x1=l[0][1]-y0 x2=l[0][2]-y0 if l[1][1]==y1+x1 and l[1][2]==y1+x2 and l[2][1]==y2+x1 and l[2][2]==y2+x2: flag=1 else: continue if flag==1: print('Yes') else: print('No')
l=[] for i in range(3): l1=list(map(int,input().split())) l.append(l1) flag=0 y0=l[0][0]-0 y1=l[1][0]-0 y2=l[2][0]-0 x1=l[0][1]-y0 x2=l[0][2]-y0 if l[1][1]==y1+x1 and l[1][2]==y1+x2 and l[2][1]==y2+x1 and l[2][2]==y2+x2: flag=1 else: pass if flag==1: print('Yes') else: print('No')
p03435
c = [] for i in range(3): c.append(list(map(int,input().split()))) """ 変数6個で解9個 b1-b2 : c*1-c*2 b2-b3 : c*2-c*3 b3-b1 : c*3-c*1 a1-a2 : c1*-c2* a2-a3 : c2*-c3* a3-a1 : c3*-c1* """ i = 0 flaga12 = True flaga23 = True flaga31 = True flagb12 = True flagb23 = True flagb31 = True b12 = c[i][0]-c[i][1] b23 = c[i][1]-c[i][2] b31 = c[i][2]-c[i][0] for i in range(3): flagb12 = flagb12 and (b12 == c[i][0]-c[i][1]) flagb23 = flagb23 and (b23 == c[i][1]-c[i][2]) flagb31 = flagb31 and (b31 == c[i][2]-c[i][0]) j = 0 a12 = c[0][j]-c[1][j] a23 = c[1][j]-c[2][j] a31 = c[2][j]-c[0][j] for i in range(3): a12 = c[0][j]-c[1][j] a23 = c[1][j]-c[2][j] a31 = c[2][j]-c[0][j] flaga12 = flaga12 and (a12 == c[0][j]-c[1][j]) flaga23 = flaga23 and (a23 == c[1][j]-c[2][j]) flaga31 = flaga31 and (a31 == c[2][j]-c[0][j]) judge = (flaga12 and flaga23 and flaga31 and flagb12 and flagb23 and flagb31) if judge: print("Yes") else: print("No")
f = [] for i in range(3): f.append(list(map(int, input().split()))) """ a1+a2+a3+b1+b2+b3が3通りで合うか """ s1 = f[0][0] + f[1][1] + f[2][2] s2 = f[0][1] + f[1][2] + f[2][0] s3 = f[0][2] + f[1][0] + f[2][1] if s1==s2==s3: print("Yes") else: print("No")
p03435
c = [list(map(int, input().split())) for _ in range(3)] if 2*c[0][2]-c[1][2]-c[2][2]+c[1][0]+c[2][0] == 2*c[0][0]: if 2*c[1][0]-c[2][0]-c[0][0]+c[2][1]+c[0][1] == 2*c[1][1]: if 2*c[2][1]-c[0][1]-c[1][1]+c[0][2]+c[1][2] == 2*c[2][2]: print('Yes') else: print('No') else: print('No') else: print('No')
c = [] for i in range(3): c_ = list(map(int, input().split())) c.append(c_) if c[2][2] == c[0][2] + c[2][0] -c[0][0] and c[2][2] == c[1][2] + c[2][1] -c[1][1] and c[1][1] == c[0][1] + c[1][0] -c[0][0]: print('Yes') else: print('No')
p03435
c = [list(map(int, input().split())) for i in range(3)] res = 0 ans = 0 for i in range(3): res += sum(c[i]) ans += c[i][i]*3 if res == ans: print('Yes') else: print('No')
c=[list(map(int,input().split())) for i in range(3)] if (c[0][0]+c[1][1]+c[2][2])*2==c[0][1]+c[0][2]+c[1][0]+c[1][2]+c[2][0]+c[2][1]: print('Yes') else: print('No')
p03435
from collections import defaultdict, Counter from itertools import product, groupby, count, permutations, combinations from math import pi, sqrt from collections import deque from bisect import bisect, bisect_left, bisect_right from string import ascii_lowercase from functools import lru_cache import sys sys.setrecursionlimit(10000) INF = float("inf") YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no" def solve(): C = [] for _ in range(3): C.append(list(map(int, input().split()))) for a1 in range(0, 101): for a2 in range(0, 101): for a3 in range(0, 101): b1_ok = (C[0][0] - a1) == (C[1][0] - a2) == (C[2][0] - a3) b2_ok = (C[0][1] - a1) == (C[1][1] - a2) == (C[2][1] - a3) b3_ok = (C[0][2] - a1) == (C[1][2] - a2) == (C[2][2] - a3) if b1_ok and b2_ok and b3_ok: return Yes return No def main(): print((solve())) if __name__ == '__main__': main()
#!/usr/bin/python3 from collections import defaultdict, Counter from itertools import product, groupby, count, permutations, combinations from math import pi, sqrt from collections import deque from bisect import bisect, bisect_left, bisect_right from string import ascii_lowercase from functools import lru_cache import sys sys.setrecursionlimit(10000) INF = float("inf") YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no" dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0] def inside(y, x, H, W): return 0 <= y < H and 0 <= x < W def ok(a1, c): b1 = c[0][0] - a1 b2 = c[0][1] - a1 b3 = c[0][2] - a1 if len(set([c[1][0] - b1, c[1][1] - b2, c[1][2] - b3])) != 1: return False if len(set([c[2][0] - b1, c[2][1] - b2, c[2][2] - b3])) != 1: return False return True def main(): c = [] for _ in range(3): c.append(list(map(int, input().split()))) for a1 in range(101): if ok(a1, c): print(Yes) return print(No) if __name__ == '__main__': main()
p03435
import sys def solve(): input = sys.stdin.readline C = [[int(c) for c in input().split()] for _ in range(3)] possible = False for i in range(101): for j in range(101): for k in range(101): a11 = C[0][0] - i a12 = C[0][1] - j a13 = C[0][2] - k a21 = C[1][0] - i a22 = C[1][1] - j a23 = C[1][2] - k a31 = C[2][0] - i a32 = C[2][1] - j a33 = C[2][2] - k if a11 == a12 == a13 and a21 == a22 == a23 and a31 == a32 == a33: possible = True break if possible: break if possible: break print(("Yes" if possible else "No")) return 0 if __name__ == "__main__": solve()
def solve(): C = [[int(c) for c in input().split()] for _ in range(3)] diffr1 = C[0][1] - C[0][0] diffr2 = C[0][2] - C[0][1] diffc1 = C[1][0] - C[0][0] diffc2 = C[2][0] - C[1][0] if C[1][1] - C[1][0] == C[2][1] - C[2][0] == diffr1: if C[1][2] - C[1][1] == C[2][2] - C[2][1] == diffr2: if C[1][1] - C[0][1] == C[1][2] - C[0][2] == diffc1: if C[2][1] - C[1][1] == C[2][2] - C[1][2] == diffc2: return "Yes" return "No" if __name__ == "__main__": print((solve()))
p03435
import sys input = sys.stdin.readline c = [[int(x) for x in input().split()] for _ in range(3)] for i in range(101): for j in range(101): for k in range(101): b1, b2, b3 = [x - i for x in c[0]] if [b1 + j, b2 + j, b3 + j] == c[1] and [b1 + k, b2 + k, b3 + k] == c[2]: print("Yes") sys.exit() print("No")
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") c = [[int(x) for x in input().split()] for _ in range(3)] flag = 1 for a1 in range(101): for a2 in range(101): for a3 in range(101): if c[0][0] - a1 == c[1][0] - a2 and c[1][0] - a2 == c[2][0] - a3: if c[0][1] - a1 == c[1][1] - a2 and c[1][1] - a2 == c[2][1] - a3: if c[0][2] - a1 == c[1][2] - a2 and c[1][2] - a2 == c[2][2] - a3: flag = 0 if flag: print('No') else: print('Yes')
p03435
c11,c12,c13 = list(map(int,input().split())) c21,c22,c23 = list(map(int,input().split())) c31,c32,c33 = list(map(int,input().split())) count = 0 if c21 - c11 == c22 - c12 == c23 - c13: count += 1 if c31 - c21 == c32 - c22 == c33 - c23: count += 1 if c12 - c11 == c22 - c21 == c32 - c31: count += 1 if c13 - c12 == c23 - c22 == c33 - c32: count += 1 if count == 4: print("Yes") else: print("No")
c11,c12,c13 = list(map(int,input().split())) c21,c22,c23 = list(map(int,input().split())) c31,c32,c33 = list(map(int,input().split())) count = 0 if c11-c12 == c21-c22 == c31-c32: count += 1 if c12-c13 == c22-c23 == c32-c33: count += 1 if c21-c11 == c22-c12 == c23-c13: count += 1 if c21-c31 == c22-c32 == c23-c33: count += 1 if count == 4: print("Yes") else: print("No")
p03435
c = [list(map(int, input().split())) for _ in range(3)] ok = False for a1 in range(101): for a2 in range(101): for a3 in range(101): b1 = c[0][0] - a1 b2 = c[0][1] - a1 b3 = c[0][2] - a1 a = [a1, a2, a3] b = [b1, b2, b3] ok_sub = True for i in range(3): for j in range(3): if a[i] + b[j] != c[i][j]: ok_sub = False if ok_sub: ok = True print('Yes') if ok else print('No')
c = [list(map(int, input().split())) for _ in range(3)] ok = True b = [c[0][0], c[0][1], c[0][2]] a = [0, c[1][0] - b[0], c[2][0] - b[0]] for i in range(3): for j in range(3): if a[i] + b[j] != c[i][j]: ok = False print('Yes') if ok else print('No')
p03435
l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) l3 = list(map(int, input().split())) for a1 in range(101): b1 = [l1[0]-a1, l1[1]-a1, l1[2]-a1] for a2 in range(101): b2 = [l2[0] - a2, l2[1] - a2, l2[2] - a2] for a3 in range(101): b3 = [l3[0] - a3, l3[1] - a3, l3[2] - a3] if b1 == b2 == b3: print("Yes") exit() else: print("No")
c = [list(map(int, input().split())) for i in range(3)] for a1 in range(101): for a2 in range(101): for a3 in range(101): if (c[0][0] - a1 == c[1][0] - a2 == c[2][0] - a3) and (c[0][1] - a1 == c[1][1] - a2 == c[2][1] - a3) and (c[0][2] - a1 == c[1][2] - a2 == c[2][2] - a3): print("Yes") exit() else: print("No")
p03435
c = [list(map(int, input().split())) for i in range(3)] for a1 in range(101): for a2 in range(101): for a3 in range(101): if (c[0][0] - a1 == c[1][0] - a2 == c[2][0] - a3) and (c[0][1] - a1 == c[1][1] - a2 == c[2][1] - a3) and (c[0][2] - a1 == c[1][2] - a2 == c[2][2] - a3): print("Yes") exit() else: print("No")
c1 = list(map(int, input().split())) c2 = list(map(int, input().split())) c3 = list(map(int, input().split())) for b1 in range(101): for b2 in range(101): for b3 in range(101): if c1[0] - b1 == c1[1] - b2 == c1[2] - b3: if c2[0] - b1 == c2[1] - b2 == c2[2] - b3: if c3[0] - b1 == c3[1] - b2 == c3[2] - b3: print("Yes") exit() else: print("No")
p03435
c = [] for _ in range(3): c.append([int(i) for i in input().split()]) def validate(a, b, c): for i in range(3): for j in range(3): if c[i][j] != a[i] + b[j]: return False return True found = False min_b1 = min(c[0][0], c[1][0], c[2][0]) for b1 in range(min_b1 + 1): a = [c[0][0] - b1, c[1][0] - b1, c[2][0] - b1] b2s = [c[0][1] - a[0], c[1][1] - a[1], c[2][1] - a[2]] if b2s[0] != b2s[1] or b2s[0] != b2s[2]: continue b3s = [c[0][2] - a[0], c[1][2] - a[1], c[2][2] - a[2]] if b3s[0] != b3s[1] or b3s[0] != b3s[2]: continue b = [b1, b2s[0], b3s[0]] if not validate(a, b, c): continue found = True break print(("Yes" if found else "No"))
c = [list(map(int, input().split())) for _ in range(3)] for i in range(2): for j in range(2): d = c[i][j] - c[i+1][j] e = c[i][j+1] - c[i+1][j+1] if d != e: print("No") exit(0) for i in range(2): for j in range(2): d = c[i][j] - c[i][j+1] e = c[i+1][j] - c[i+1][j+1] if d != e: print("No") exit(0) print("Yes")
p03435
# ABC 088 C from collections import deque def resolve(): def all_same(a, b, c): if a ==b and b ==c: return True return False c = [] for _ in range(3): c.append(list(map(int, input().split()))) ans = True if not all_same(c[0][0]-c[1][0], c[0][1] - c[1][1], c[0][2]-c[1][2]): ans = False if not all_same(c[0][0]-c[2][0], c[0][1] - c[2][1], c[0][2]-c[2][2]): ans = False if not all_same(c[0][0] - c[0][1], c[1][0] - c[1][1], c[2][0] - c[2][1]): ans = False if not all_same(c[0][0] - c[0][2], c[1][0] - c[1][2], c[2][0] - c[2][2]): ans = False if ans: print('Yes') else: print('No') if __name__ == "__main__": resolve()
def resolve(): c = [] c.append(list(map(int, input().split()))) c.append(list(map(int, input().split()))) c.append(list(map(int, input().split()))) a01 = c[0][0] - c[0][1] a02 = c[0][0] - c[0][2] a12 = c[0][1] - c[0][2] b01 = c[0][0] - c[1][0] b02 = c[0][0] - c[2][0] b12 = c[1][0] - c[2][0] for i in range(3): if ((c[i][0] - c[i][1]) != a01) or (c[i][0] - c[i][2] != a02) or (c[i][1] - c[i][2] != a12): return False if ((c[0][i] - c[1][i]) != b01 or (c[0][i] - c[2][i]) != b02 or (c[1][i] - c[2][i]) != b12): return False return True if __name__ == "__main__": if (resolve()): print("Yes") else: print("No")
p03435
c1 = list(map(int,input().split())) c2 = list(map(int,input().split())) c3 = list(map(int,input().split())) if not c1[0]-c1[1] == c2[0]-c2[1] == c3[0]-c3[1]: print('No') exit() if not c1[2]-c1[1] == c2[2]-c2[1] == c3[2]-c3[1]: print('No') exit() if not c1[0]-c2[0] == c1[1]-c2[1] == c1[2]-c2[2]: print('No') exit() if not c3[0]-c2[0] == c3[1]-c2[1] == c3[2]-c2[2]: print('No') exit() print('Yes')
a1,a2,a3 = list(map(int,input().split())) b1,b2,b3 = list(map(int,input().split())) c1,c2,c3 = list(map(int,input().split())) if not (a1-a2 == b1-b2 == c1-c2) or not (a2-a3 == b2-b3 == c2-c3): print('No') elif not (a1-b1 == a2-b2 == a3-b3) or not (b1-c1 == b2-c2 == b3-c3): print('No') else: print('Yes')
p03435
C = [list(map(int, input().split())) for _ in range(3)] a = [0] * 3 b = [0] * 3 for i in range(101): a[0] = i flg = True for j in range(3): b[j] = C[0][j] - i if b[j] < 0: flg = False break if flg: for j in range(1, 3): a[j] = C[j][0] - b[0] if a[j] < 0: flg = False break if flg: for j in range(3): for k in range(3): if C[j][k] != a[j] + b[k]: flg = False break if flg: print('Yes') quit() print('No')
C = [list(map(int, input().split())) for _ in range(3)] a = [0] * 3 b = [0] * 3 flg = True for j in range(3): b[j] = C[0][j] - a[0] for j in range(1, 3): a[j] = C[j][0] - b[0] for j in range(3): for k in range(3): if C[j][k] != a[j] + b[k]: flg = False break if flg: print('Yes') quit() print('No')
p03435
c = [[]]*3 for i in range(3): c[i] = list(map(int, input().split())) ans = 0 for i in range(3): ans += sum(c[i]) if ans % 3 == 0: arr = [[0]* 6]*3 for i in range(3): arr[i][0] = c[i][0] - c[i][1] arr[i][1] = c[i][0] - c[i][2] arr[i][2] = c[i][1] - c[i][0] arr[i][3] = c[i][1] - c[i][2] arr[i][4] = c[i][2] - c[i][0] arr[i][5] = c[i][2] - c[i][1] for i in range(6): if arr[0][i] == arr[1][i] and arr[0][i] == arr[2][i]: continue else: print("No") exit(0) for i in range(3): arr[i][0] = c[0][i] - c[1][i] arr[i][1] = c[0][i] - c[2][i] arr[i][2] = c[1][i] - c[0][i] arr[i][3] = c[1][i] - c[2][i] arr[i][4] = c[2][i] - c[1][i] arr[i][5] = c[2][i] - c[0][i] for i in range(6): if arr[0][i] == arr[1][i] and arr[0][i] == arr[2][i]: continue else: print("No") exit(0) arr = [0]*3 sum = 0 for i in range(3): arr[0] += c[i][i] for i in range(3): if i + 1 < 3: arr[1] += c[i][i+1] else: arr[1] += c[i][0] for i in range(3): if i + 2 < 3: arr[2] += c[i][i+2] else: arr[2] += c[i][i-1] if arr[0] != arr[1] or arr[0] != arr[2]: print("No") exit(0) print("Yes") exit(0) print("No")
c = [[]]*3 for i in range(3): c[i] = list(map(int, input().split())) ans = 0 for i in range(3): ans += sum(c[i]) if ans % 3 == 0: arr = [0]*3 sum = 0 for i in range(3): arr[0] += c[i][i] for i in range(3): if i + 1 < 3: arr[1] += c[i][i+1] else: arr[1] += c[i][0] for i in range(3): if i + 2 < 3: arr[2] += c[i][i+2] else: arr[2] += c[i][i-1] if arr[0] != arr[1] or arr[0] != arr[2]: print("No") exit(0) print("Yes") exit(0) print("No")
p03435
import sys input = sys.stdin.readline sys.setrecursionlimit(1000000) from collections import deque def getN(): return int(eval(input())) def getList(): return list(map(int, input().split())) import math import bisect from logging import getLogger, StreamHandler, DEBUG, WARNING logger = getLogger(__name__) handler = StreamHandler() handler.setLevel(DEBUG) logger.setLevel(DEBUG) # handler.setLevel(WARNING) # logger.setLevel(WARNING) logger.addHandler(handler) # class SegTree(): # def __init__(self, n): # self.value = [0 for i in range(n*2)] # MOD = 10 ** 9 + 7 # def getinvmod(n): # return [pow(i, MOD-2, MOD) for i in range(n+1)] def judge(a1, a2, a3, nums): b1 = nums[0][0] - a1 b2 = nums[0][1] - a1 b3 = nums[0][2] - a1 if a2 + b1 == nums[1][0] and a2 + b2 == nums[1][1] and a2 + b3 == nums[1][2]: if a3 + b1 == nums[2][0] and a3 + b2 == nums[2][1] and a3 + b3 == nums[2][2]: return True return False def main(): nums = [getList() for _ in range(3)] for a1 in range(101): for a2 in range(101): for a3 in range(101): if judge(a1, a2, a3, nums): print("Yes") return print("No") if __name__ == "__main__": main()
import sys # input = sys.stdin.buffer.readline def getN(): return int(eval(input())) def getNM(): return list(map(int, input().split())) def getlist(): return list(map(int, input().split())) import math import bisect import heapq from collections import defaultdict, Counter, deque MOD = 10**9 + 7 INF = 10**15 def main(): a,b,c = getlist() s12 = b - a s13 = c - a s23 = c - b for i in range(2): d,e,f = getlist() # print(d,e,f) # print(s12,s23,s13) if e-d != s12: # print(e-d) print("No") return if f-d != s13: print("No") return if f-e != s23: print("No") return print("Yes") return if __name__ == '__main__': main() """ 9999 3 2916 """
p03435
c = [list(map(int,input().split())) for _ in range(3)] for i in range(2): x = c[0][i] - c[0][i+1] y = c[1][i] - c[1][i+1] z = c[2][i] - c[2][i+1] if not (x==y and y==z and z==x): print('No') exit() for i in range(2): x = c[i][0] - c[i+1][0] y = c[i][1] - c[i+1][1] z = c[i][2] - c[i+1][2] if not (x==y and y==z and z==x): print('No') exit() print('Yes')
l = [list(map(int,input().split())) for _ in range(3)] m = [[0, 0, 0] for _ in range(3)] for i in range(3): for j in range(3): m[i][j] = l[i][j] - l[0][j] for i in range(3): f = True for j in range(3): if m[i][j] != m[i][0]: f = False break if not f: print('No') exit() print('Yes')
p03435
C = [list(map(int,input().split())) for _ in range(3)] b = [0]*3 for i in range(3): b[i] = C[0][i]-0 a = [0]*3 for i in range(3): a[i] = C[i][0] - b[0] ans = "Yes" for i in range(3): for j in range(3): if(C[i][j] != a[i] + b[j]): ans = "No" print(ans)
C = [list(map(int,input().split())) for _ in range(3)] #a1 = 0 と固定 b = [] for i in range(3): b.append(C[0][i] - 0) a = [] for i in range(3): a.append(C[i][0] - b[0]) ans = "Yes" for i in range(3): for j in range(3): if(C[i][j] != a[i] + b[j]): ans = "No" print(ans)
p03435
# -*- coding: utf-8 -*- def solve(): c = [list(map(int, input().split())) for _ in range(3)] for a0 in range(-100, 101) : b = [c[0][i] - a0 for i in range(3)] if any(c[1][0] - b[0] != c[1][i] - b[i] for i in range(3)): continue if any(c[2][0] - b[0] != c[2][i] - b[i] for i in range(3)): continue return True if __name__ == '__main__': if solve() : print("Yes") else : print("No")
# -*- coding: utf-8 -*- def solve(): c = [list(map(int, input().split())) for _ in range(3)] for a0 in range(-100, 101) : b = [c[0][i] - a0 for i in range(3)] if any(c[1][0] - b[0] != c[1][i] - b[i] for i in range(3)): continue if any(c[2][0] - b[0] != c[2][i] - b[i] for i in range(3)): continue return True return False if solve() : print("Yes") else : print("No")
p03435
c=[[int(x) for x in input().split()] for _ in range(3)] ans="No" for a1 in c[0]: for a2 in c[1]: for a3 in c[2]: b1=c[0][0]-a1 b2=c[1][1]-a2 b3=c[2][2]-a3 if a1+b2==c[0][1] and a1+b3==c[0][2] and a2+b1==c[1][0] and a2+b3==c[1][2] and a3+b1==c[2][0] and a3+b2==c[2][1]: ans = "Yes" break if ans=="Yes":break if ans=="Yes":break print(ans)
c=[[int(x) for x in input().split()] for _ in range(3)] ans="No" for a1 in c[0]: for a2 in c[1]: for a3 in c[2]: b1=c[0][0]-a1 b2=c[1][1]-a2 b3=c[2][2]-a3 if a1+b2==c[0][1] and a1+b3==c[0][2] and a2+b1==c[1][0] and a2+b3==c[1][2] and a3+b1==c[2][0] and a3+b2==c[2][1]: ans = "Yes" break print(ans)
p03435
c = [] for i in range(3): c.append(list(map(int, input().split()))) ans = 'Yes' for i in range(2): a0 = c[0][i] - c[0][i + 1] a1 = c[1][i] - c[1][i + 1] a2 = c[2][i] - c[2][i + 1] if not (a0 == a1 == a2): ans = 'No' break print(ans)
c = [list(map(int, input().split())) for i in range(3)] ans = 'Yes' for i in range(2): for j in range(2): if c[i][j] - c[i][j + 1] != c[i + 1][j] - c[i + 1][j + 1]: ans = 'No' break print(ans)
p03435
def c_takahashi_information(C): a = [] b = [] for a_0 in range(min(C[0]) + 1): for a_1 in range(min(C[1]) + 1): for a_2 in range(min(C[2]) + 1): tmp1 = [a_0, a_1, a_2] tmp2 = [[C[i][j] - tmp1[i] for j in range(3)] for i in range(3)] tmp2 = list(map(list,set(map(tuple, tmp2)))) a.append(tmp1) b.append(tmp2) for aa, bb in zip(a, b): for bbb in bb: f = True for i in range(3): for j in range(3): if aa[i] + bbb[j] != C[i][j]: f = False break if not f: break if f: return 'Yes' return 'No' C = [[int(i) for i in input().split()] for j in range(3)] print((c_takahashi_information(C)))
def c_takahashi_information(C): a = [min(C[0]), min(C[1]), min(C[2])] tmp = [[C[i][j] - a[i] for j in range(3)] for i in range(3)] b = list(map(list, set(map(tuple, tmp)))) for bb in b: f = True for i in range(3): for j in range(3): if a[i] + bb[j] != C[i][j]: f = False break if not f: break if f: return 'Yes' return 'No' C = [[int(i) for i in input().split()] for j in range(3)] print((c_takahashi_information(C)))
p03435
def c_takahashi_information(C): a = [min(C[0]), min(C[1]), min(C[2])] tmp = [[C[i][j] - a[i] for j in range(3)] for i in range(3)] b = list(map(list, set(map(tuple, tmp)))) for bb in b: f = True for i in range(3): for j in range(3): if a[i] + bb[j] != C[i][j]: f = False break if not f: break if f: return 'Yes' return 'No' C = [[int(i) for i in input().split()] for j in range(3)] print((c_takahashi_information(C)))
def c_takahashi_information(C): b = [C[0][i] - 0 for i in range(3)] a = [0] + [C[i][0] - b[0] for i in range(1, 3)] # a_1=0と仮定してよい for i in range(3): for j in range(3): if a[i] + b[j] != C[i][j]: return 'No' return 'Yes' C = [[int(i) for i in input().split()] for j in range(3)] print((c_takahashi_information(C)))
p03435
a = list(map(int,input().split())) b = list(map(int,input().split())) c = list(map(int,input().split())) A = list([x-min(a) for x in a]) B = list([x-min(b) for x in b]) C = list([x-min(c) for x in c]) A.sort() B.sort() C.sort() if A==B==C: print('Yes') else: print('No')
a = list(map(int,input().split())) b = list(map(int,input().split())) c = list(map(int,input().split())) A = list([x-min(a) for x in a]) B = list([x-min(b) for x in b]) C = list([x-min(c) for x in c]) if A==B==C: print('Yes') else: print('No')
p03435
def main(): c11, c12, c13 = list(map(int, input().split())) c21, c22, c23 = list(map(int, input().split())) c31, c32, c33 = list(map(int, input().split())) r1max = max([c11, c12, c13]) r2max = max([c21, c22, c23]) r3max = max([c31, c32, c33]) c1max = max([c11, c21, c31]) c2max = max([c12, c22, c32]) c3max = max([c13, c23, c33]) for a1 in range(0, r1max + 1): for a2 in range(0, r2max + 1): for a3 in range(0, r3max + 1): for b1 in range(0, c1max + 1): C11 = a1 + b1 C21 = a2 + b1 C31 = a3 + b1 if C11 == c11 and C21 == c21 and C31 == c31: for b2 in range(0, c2max + 1): C12 = a1 + b2 C22 = a2 + b2 C32 = a3 + b2 if C12 == c12 and C22 == c22 and C32 == c32: for b3 in range(0, c3max + 1): C13 = a1 + b3 C23 = a2 + b3 C33 = a3 + b3 if C13 == c13 and C23 == c23 and C33 == c33: print('Yes') return print('No') return if __name__ == '__main__': main()
def main(): c11, c12, c13 = list(map(int, input().split())) c21, c22, c23 = list(map(int, input().split())) c31, c32, c33 = list(map(int, input().split())) r1max = max([c11, c12, c13]) r2max = max([c21, c22, c23]) r3max = max([c31, c32, c33]) c1max = max([c11, c21, c31]) c2max = max([c12, c22, c32]) c3max = max([c13, c23, c33]) for a1 in range(0, r1max + 1): for a2 in range(0, r2max + 1): for a3 in range(0, r3max + 1): m = max([a1, a2, a3]) for b1 in range(0, c1max - m + 1): C11 = a1 + b1 C21 = a2 + b1 C31 = a3 + b1 if C11 == c11 and C21 == c21 and C31 == c31: for b2 in range(0, c2max - m + 1): C12 = a1 + b2 C22 = a2 + b2 C32 = a3 + b2 if C12 == c12 and C22 == c22 and C32 == c32: for b3 in range(0, c3max - m + 1): C13 = a1 + b3 C23 = a2 + b3 C33 = a3 + b3 if C13 == c13 and C23 == c23 and C33 == c33: print('Yes') return print('No') return if __name__ == '__main__': main()
p03435
#f = open("input.txt") #N, K = [int(x) for x in f.readline().split(' ')] #lines = f.readlines() #f.close() import sys N, K = [int(x) for x in sys.stdin.readline().split(' ')] lines = sys.stdin.readlines() schedule = [0]*N for line in lines: strs = line.split(' ') schedule[int(strs[0])-1] = int(strs[1]) number = [0]*9 for i in range(9): l1 = i // 3 l2 = i % 3 if(schedule[0]!=l2+1 and schedule[0]!=0): number[i] = 0 elif(schedule[1]!=l1+1 and schedule[1]!=0): number[i] = 0 else: number[i] = 1 for s in schedule[2:]: new_number = [0]*9 for i in range(9): l1 = i // 3 l2 = i % 3 for j in range(3): new_number[i] += number[l2*3 + j] if((s==0 or s==l1+1) and not (l2==j and l1==j)) else 0 for i in range(9): number[i] = new_number[i] % 10000 sum = 0 for n in number: sum += n print((sum % 10000))
#f = open("input.txt") #N, K = [int(x) for x in f.readline().split(' ')] #lines = f.readlines() #f.close() from sys import stdin N, K = [int(x) for x in stdin.readline().split(' ')] lines = stdin.readlines() schedule = [0]*N for line in lines: strs = line.split(' ') schedule[int(strs[0])-1] = int(strs[1]) number = [1]*9 for i in range(9): l1 = i // 3 l2 = i % 3 if schedule[0]!=l2+1 and schedule[0]!=0: number[i] = 0 elif schedule[1]!=l1+1 and schedule[1]!=0: number[i] = 0 for s in schedule[2:]: tmp = [0]*9 for i in range(9): l1 = i // 3 l2 = i % 3 tmp[i] = [number[l2*3+j] for j in range(3) \ if (s==0 or s==l1+1) and not (l2==j and l1==j)] for i in range(9): number[i] = sum(tmp[i]) print((sum(number) % 10000))
p00491
#coding:utf-8 import sys import copy def main(): line = sys.stdin.readline() while line.split() != ["0","0"]: analy(line) line = sys.stdin.readline() return def analy(line): field = [int(n) for n in line.split()] num = int(sys.stdin.readline()) data = [] for i in range(num): a = sys.stdin.readline() b = [int(n) for n in a.split()] data.append(b) print((compute(field, data))) def compute(field, ng): list = [] for i in range(field[1]): for j in range(field[0]): if i == 0 : if [j+1,i+1] in ng: list.append(0) elif j == 0: list.append(1) else: list.append(list[j-1]) else: if [j+1,i+1] in ng: list[j] = 0 elif j != 0: #no left the same. list[j] = list[j-1] + list[j] return list[len(list)-1] if __name__ == "__main__": main()
#coding:utf-8 import sys import copy def main(): line = sys.stdin.readline() #case while line.split() != ["0","0"]: analy(line) line = sys.stdin.readline() return def analy(line): #整列 field = [int(n) for n in line.split()] # 横、縦 num = int(sys.stdin.readline()) data = [] for i in range(num): a = sys.stdin.readline() b = [int(n) for n in a.split()] #横、縦 data.append(b) print((compute(field, data))) def compute(field, ng): list = [] for i in range(field[1]): for j in range(field[0]): #横→縦に移動していく if i == 0 : #一番下 if [j+1,i+1] in ng: list.append(0) elif j == 0: #左がない list.append(1) else: list.append(list[j-1]) else: if [j+1,i+1] in ng: list[j] = 0 elif j != 0: #no left >> the same. list[j] = list[j-1] + list[j] return list[len(list)-1] if __name__ == "__main__": main()
p00438
import sys import itertools # import numpy as np import time import math from heapq import heappop, heappush from collections import defaultdict from collections import Counter from collections import deque sys.setrecursionlimit(10 ** 7) INF = 10 ** 18 MOD = 10 ** 9 + 7 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # map(int, input().split()) N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) S.append(0) T.append(0) MAX = 2005 dp0 = [[0] * MAX for _ in range(MAX)] dp1 = [[0] * MAX for _ in range(MAX)] dp0[0][0] = 1 for i in range(N + 1): for j in range(M + 1): dp0[i + 1][j] += dp0[i][j] % MOD dp1[i][j] += dp0[i][j] % MOD dp1[i][j + 1] += dp1[i][j] % MOD if S[i] == T[j]: dp0[i + 1][j + 1] += dp1[i][j] % MOD print((dp1[N][M] % MOD))
import sys import itertools # import numpy as np import time import math from heapq import heappop, heappush from collections import defaultdict from collections import Counter from collections import deque sys.setrecursionlimit(10 ** 7) INF = 10 ** 18 MOD = 10 ** 9 + 7 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # map(int, input().split()) N, M = list(map(int, input().split())) S = [0] + list(map(int, input().split())) T = [0] + list(map(int, input().split())) dp_cum = [[0] * (M+1) for _ in range(N+1)] dp_cum[0][0] = 1 for n in range(N+1): dp_cum[n][0] = 1 for m in range(M+1): dp_cum[0][m] = 1 for n in range(1,N+1): for m in range(1,M+1): now = 0 if S[n] == T[m]: now = dp_cum[n-1][m-1] dp_cum[n][m] = dp_cum[n-1][m] + dp_cum[n][m-1] - dp_cum[n-1][m-1] + now dp_cum[n][m] %= MOD print((dp_cum[N][M]))
p03003
MOD = 10 ** 9 + 7 _, _ = list(map(int, input().strip().split())) S = list(map(int, input().strip().split())) T = list(map(int, input().strip().split())) sdp = [[0] * (len(T) + 1) for _ in range(len(S) + 1)] for i, s in enumerate(S): for j, t in enumerate(T): v = sdp[i][j + 1] + sdp[i + 1][j] + (1 if s == t else -sdp[i][j]) sdp[i + 1][j + 1] = v % MOD print((sdp[-1][-1] + 1)) # for row in sdp: # print(('{: 4d} ' * len(row)).format(*row)) ## print(sdp)
MOD = 10 ** 9 + 7 def main(S, T): sdp = [[0] * (len(T) + 1) for _ in range(len(S) + 1)] for i, s in enumerate(S): for j, t in enumerate(T): v = sdp[i][j + 1] + sdp[i + 1][j] + (1 if s == t else -sdp[i][j]) sdp[i + 1][j + 1] = v % MOD return (sdp[-1][-1] + 1) if __name__ == '__main__': _, _ = list(map(int, input().strip().split())) S = input().strip().split() T = input().strip().split() # S = range(1000) # T = range(1000) print((main(S, T)))
p03003
MOD = 10 ** 9 + 7 def main(S, T): sdp = [[0] * (len(T) + 1) for _ in range(len(S) + 1)] for i, s in enumerate(S): for j, t in enumerate(T): v = sdp[i][j + 1] + sdp[i + 1][j] + (1 if s == t else -sdp[i][j]) sdp[i + 1][j + 1] = v % MOD return (sdp[-1][-1] + 1) if __name__ == '__main__': _, _ = list(map(int, input().strip().split())) S = input().strip().split() T = input().strip().split() # S = range(1000) # T = range(1000) print((main(S, T)))
MOD = 10 ** 9 + 7 def main(S, T): l = len(T) + 1 dp = [0] * l # sdp = [[0] * (len(T) + 1) for _ in range(len(S) + 1)] # for i, s in enumerate(S): # for j, t in enumerate(T): # v = sdp[i][j + 1] + sdp[i + 1][j] + (1 if s == t else -sdp[i][j]) # sdp[i + 1][j + 1] = v % MOD for i, s in enumerate(S): tmp = [0] * l for j, t in enumerate(T): v = dp[j + 1] + tmp[j] + (1 if s == t else -dp[j]) tmp[j + 1] = v % MOD dp = tmp # return (sdp[-1][-1] + 1) return dp[-1] + 1 if __name__ == '__main__': if True: _, _ = list(map(int, input().strip().split())) S = input().strip().split() T = input().strip().split() else: S = list(range(1000)) T = list(range(1000)) print((main(S, T)))
p03003
# -*- coding: utf-8 -*- """ Created on Sun Jun 16 20:56:46 2019 @author: Yamazaki Kenichi """ N, M = list(map(int,input().split())) S = list(map(int,input().split())) T = list(map(int,input().split())) mod = 10**9 + 7 A = [[1 for j in range(M+1)]] A += [[0 for j in range(M+1)] for i in range(N)] A += [[1 for j in range(M+1)]] for i in range(N): # print(i,j) for j in range(M): # print(i,j) if T[j] == S[i]: A[i+1][j] = A[-1][j-1] % mod else: A[i+1][j] = A[i+1][j-1] % mod A[-1][j] += A[i+1][j] A[-1][j] %= mod # print(A) ans = A[-1][-2] print(ans)
# -*- coding: utf-8 -*- """ Created on Sun Jun 16 22:15:18 2019 @author: Yamazaki Kenichi """ N, M = list(map(int,input().split())) S = list(map(int,input().split())) T = list(map(int,input().split())) mod = 10**9 + 7 A = [[1 for j in range(M+1)]] A += [[0 for j in range(M+1)]] #A += [[0 for j in range(M+1)] for i in range(N)] A += [[1 for j in range(M+1)]] for i in range(N): # print(i,j) tmp2 = 0 for j in range(M): # print(i,j) if T[j] == S[i]: tmp = A[-1][j-1] % mod # A[i+1][j] = A[-1][j-1] % mod else: tmp = tmp2 % mod # A[i+1][j] = A[i+1][j-1] % mod tmp2 = tmp A[-1][j] += tmp # A[-1][j] += A[i+1][j] A[-1][j] %= mod # print(A) ans = A[-1][-2] % mod print(ans)
p03003
import sys,heapq from collections import deque,defaultdict printn = lambda x: sys.stdout.write(x) inn = lambda : int(eval(input())) inl = lambda: list(map(int, input().split())) inm = lambda: list(map(int, input().split())) DBG = True and False R = 10**9 + 7 def ddprint(x): if DBG: print(x) n,m = inm() s = inl() t = inl() dp = [ [0] * (m+1) for i in range(n) ] dp[0][0] = 1 if s[0]==t[0] else 0 for i in range(1,n): dp[i][0] = dp[i-1][0] + (1 if s[i]==t[0] else 0) for j in range(1,m): dp[0][j] = dp[0][j-1] + (1 if s[0]==t[j] else 0) sh = defaultdict(list) for i in range(1,n): sh[s[i]].append(i) for j in range(1,m): x = 0 acc = [0] * (n+1) for z in sh[t[j]]: # s[z]==t[j] x = (x+dp[z-1][j-1]+1)%R # z>0 acc[z] = x if j==14: ddprint("dp {} z {} acc {}".format(dp[z-1][j-1], z, acc[z])) for i in range(1,n): acc[i] = (acc[i-1] if acc[i]==0 else acc[i]) for i in range(1,n): x = dp[i][j-1] #for z in sh[t[j]]: # s[z]==t[j] # if z<=i: # x = (x+dp[z-1][j-1]+1)%R # z>0 x += acc[i] if t[j]==s[0]: x += 1 dp[i][j] = x%R if i==18 and j==14: ddprint("{} {} {}".format(dp[i][j-1], acc[i], t[j]==s[0])) ddprint("i {} j {} dpij {}".format(i,j,dp[i][j])) if DBG: for i in range(n): print((dp[i])) print(((dp[n-1][m-1]+1)%R))
import sys,heapq from collections import deque,defaultdict printn = lambda x: sys.stdout.write(x) inn = lambda : int(eval(input())) inl = lambda: list(map(int, input().split())) inm = lambda: list(map(int, input().split())) DBG = True and False R = 10**9 + 7 def ddprint(x): if DBG: print(x) n,m = inm() s = inl() t = inl() dp = [ [0] * (m+1) for i in range(n) ] dp[0][0] = 1 if s[0]==t[0] else 0 for i in range(1,n): dp[i][0] = dp[i-1][0] + (1 if s[i]==t[0] else 0) for j in range(1,m): dp[0][j] = dp[0][j-1] + (1 if s[0]==t[j] else 0) sh = defaultdict(list) for i in range(1,n): sh[s[i]].append(i) for j in range(1,m): x = 0 acc = [0] * (n+1) for z in sh[t[j]]: # s[z]==t[j] x = (x+dp[z-1][j-1]+1)%R # z>0 acc[z] = x for i in range(1,n): acc[i] = (acc[i-1] if acc[i]==0 else acc[i]) for i in range(1,n): x = dp[i][j-1] #for z in sh[t[j]]: # s[z]==t[j] # if z<=i: # x = (x+dp[z-1][j-1]+1)%R # z>0 x += acc[i] if t[j]==s[0]: x += 1 dp[i][j] = x%R if DBG: for i in range(n): print((dp[i])) print(((dp[n-1][m-1]+1)%R))
p03003
#解説読み 2次元dp mod = 10**9 + 7 n,m = list(map(int,input().split())) A = list(map(int,input().split())) B = list(map(int,input().split())) tmp = [1 for _ in range(m+1)] dp = [tmp] for ni in range(n): last = tmp tmp = [1] for mi in range(m): if A[ni] != B[mi]: x = tmp[-1]+last[mi+1]-last[mi] else: x = tmp[-1]+last[mi+1] x %= mod tmp.append(x) dp.append(tmp) print((dp[-1][-1]))
#15:35 h,w = list(map(int,input().split())) a = list(map(int,input().split())) b = list(map(int,input().split())) mod = 10 ** 9 + 7 now = [1 for _ in range(w+1)] for i in range(h): last = now now = [1] for j in range(w): if a[i] == b[j]: now.append((last[j+1]+now[-1])%mod) else: now.append((last[j+1]+now[-1]-last[j])%mod) #print(now) print((now[-1]))
p03003
from collections import defaultdict import sys from copy import copy input = sys.stdin.readline mod = 10**9+7 N, M = list(map(int, input().split())) S = "".join(input().split()) T = "".join(input().split()) dS = defaultdict(int) dT = defaultdict(int) dS[""] += 1 dT[""] += 1 for i in range(N): tmp = copy(dS) for key in tmp: dS[key+S[i]] += tmp[key] for i in range(M): tmp = copy(dT) for key in tmp: dT[key+T[i]] += tmp[key] ans = 0 for key in dS: ans += dS[key]*dT[key]%mod print((ans%mod))
mod = 10**9 + 7 N, M = list(map(int, input().split())) S = [None] + list(map(int, input().split())) T = [None] + list(map(int, input().split())) dp = [[0 for _ in range(M+1)] for _ in range(N+1)] for i in range(1, N+1): for j in range(1, M+1): if S[i] == T[j]: dp[i][j] = (dp[i][j-1] + dp[i-1][j] + 1)%mod else: dp[i][j] = (dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1])%mod print(((dp[N][M]+1)%mod))
p03003
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[[0]*m for _ in range(n)] sum=[[0]*m for _ in range(n)] mod=10**9+7 for i in range(n): if s[i]==t[0]: dp[i][0]=1 else: dp[i][0]=0 for i in range(m): if t[i]==s[0]: dp[0][i]=1 else: dp[0][i]=0 sum[0][0]=dp[0][0] for i in range(1,n): sum[i][0]=sum[i-1][0]+dp[i][0] for i in range(1,m): sum[0][i]=sum[0][i-1]+dp[0][i] for i in range(1,n): for j in range(1,m): if s[i]==t[j]: dp[i][j]=(sum[i-1][j-1]+1)%mod else: dp[i][j]=0 sum[i][j]=(sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+dp[i][j])%mod ans=0 for i in range(n): for j in range(m): ans+=dp[i][j] ans=ans%mod print(((ans+1)%mod))
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[[0]*m for _ in range(n)] sum=[[0]*m for _ in range(n)] mod=10**9+7 ans=0 for i in range(n): if s[i]==t[0]: dp[i][0]=1 else: dp[i][0]=0 ans+=dp[i][0] sum[i][0]=sum[i-1][0]+dp[i][0] for i in range(m): if t[i]==s[0]: dp[0][i]=1 else: dp[0][i]=0 ans+=dp[0][i] sum[0][i]=sum[0][i-1]+dp[0][i] for i in range(1,n): for j in range(1,m): if s[i]==t[j]: dp[i][j]=(sum[i-1][j-1]+1)%mod else: dp[i][j]=0 sum[i][j]=(sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+dp[i][j])%mod ans+=dp[i][j] ans%=mod print(((ans-dp[0][0]+1)%mod))
p03003
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[[0]*m for _ in range(n)] sum=[[0]*m for _ in range(n)] mod=10**9+7 ans=0 for i in range(n): if s[i]==t[0]: dp[i][0]=1 else: dp[i][0]=0 ans+=dp[i][0] sum[i][0]=sum[i-1][0]+dp[i][0] for i in range(m): if t[i]==s[0]: dp[0][i]=1 else: dp[0][i]=0 ans+=dp[0][i] sum[0][i]=sum[0][i-1]+dp[0][i] for i in range(1,n): for j in range(1,m): if s[i]==t[j]: dp[i][j]=(sum[i-1][j-1]+1)%mod else: dp[i][j]=0 sum[i][j]=(sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+dp[i][j])%mod ans+=dp[i][j] ans%=mod print(((ans-dp[0][0]+1)%mod))
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[[0]*(m+1) for _ in range(n+1)] sum=[[0]*(m+1) for _ in range(n+1)] mod=10**9+7 ans=0 for i in range(n): for j in range(m): if s[i]==t[j]: dp[i+1][j+1]=(sum[i][j]+1)%mod else: dp[i+1][j+1]=0 sum[i+1][j+1]=(sum[i][j+1]+sum[i+1][j]-sum[i][j]+dp[i+1][j+1])%mod ans+=dp[i+1][j+1] ans%=mod print(((ans+1)%mod))
p03003
''' 自宅用PCでの解答 ''' import math #import numpy as np import itertools import queue import bisect from collections import deque,defaultdict import heapq as hpq from sys import stdin,setrecursionlimit #from scipy.sparse.csgraph import dijkstra #from scipy.sparse import csr_matrix ipt = stdin.readline setrecursionlimit(10**7) mod = 10**9+7 dir = [(-1,0),(0,-1),(1,0),(0,1)] alp = "abcdefghijklmnopqrstuvwxyz" def main(): n,m = list(map(int,ipt().split())) s = [int(i) for i in ipt().split()] t = [int(i) for i in ipt().split()] ans = 0 dp = [[1]*(m+1) for i in range(n+1)] for i,si in enumerate(s): for j,tj in enumerate(t): if si == tj: dp[i+1][j+1] = dp[i][j+1]+dp[i+1][j] else: dp[i+1][j+1] = dp[i][j+1]+dp[i+1][j]-dp[i][j] print((dp[n][m]%mod)) return None if __name__ == '__main__': main()
''' 自宅用PCでの解答 ''' import math #import numpy as np import itertools import queue import bisect from collections import deque,defaultdict import heapq as hpq from sys import stdin,setrecursionlimit #from scipy.sparse.csgraph import dijkstra #from scipy.sparse import csr_matrix ipt = stdin.readline setrecursionlimit(10**7) mod = 10**9+7 dir = [(-1,0),(0,-1),(1,0),(0,1)] alp = "abcdefghijklmnopqrstuvwxyz" def main(): n,m = list(map(int,ipt().split())) s = [int(i) for i in ipt().split()] t = [int(i) for i in ipt().split()] ans = 0 dp = [[1]*(m+1) for i in range(n+1)] for i,si in enumerate(s): for j,tj in enumerate(t): if si == tj: dp[i+1][j+1] = dp[i][j+1]+dp[i+1][j] else: dp[i+1][j+1] = dp[i][j+1]+dp[i+1][j]-dp[i][j] dp[i+1][j+1] %= mod print((dp[n][m])) return None if __name__ == '__main__': main()
p03003
mod = 10**9+7 from bisect import bisect from collections import defaultdict N,M = list(map(int,input().split())) S = list(map(int,input().split())) T = list(map(int,input().split())) dp = [[0]*(M+1) for _ in range(N+1)] for n in range(N): for m in range(M): dp[n][m] = dp[n-1][m]+dp[n][m-1]-dp[n-1][m-1] if S[n]==T[m]: dp[n][m] += dp[n-1][m-1]+1 dp[n][m] %= mod print((dp[N-1][M-1]+1))
mod = 10**9+7 N,M = list(map(int,input().split())) S = list(map(int,input().split())) T = list(map(int,input().split())) dp = [[0]*(len(T)+1) for _ in range(len(S)+1)] for s in range(len(S)+1): for t in range(len(T)+1): if s == 0 or t == 0: dp[s][t] = 1 for s in range(len(S)): for t in range(len(T)): if S[s]==T[t]: dp[s+1][t+1] = (dp[s+1][t]+dp[s][t+1])%mod else: dp[s+1][t+1] = (dp[s+1][t]+dp[s][t+1]-dp[s][t])%mod print((dp[len(S)][len(T)]))
p03003
N, M = list(map(int, input().split())) S = [int(i) for i in input().split()] T = [int(i) for i in input().split()] MOD = 10**9 + 7 ns = len(S) nt = len(T) dp = [[0] * (nt + 1) for i in range(ns + 1)] for i in range(1, ns + 1): for j in range(1, nt + 1): if (S[i - 1] == T[j - 1]): dp[i][j] = 1 + dp[i][j - 1] + dp[i - 1][j] else: dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1] print(((dp[ns][nt] + 1) % MOD))
N, M = list(map(int, input().split())) S = [int(i) for i in input().split()] T = [int(i) for i in input().split()] MOD = 10**9 + 7 ns = len(S) nt = len(T) dp = [[0] * (nt + 1) for i in range(ns + 1)] for i in range(1, ns + 1): for j in range(1, nt + 1): if (S[i - 1] == T[j - 1]): dp[i][j] = (1 + dp[i][j - 1] + dp[i - 1][j]) % MOD else: dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1]) % MOD print(((dp[ns][nt] + 1) % MOD))
p03003
#!/usr/bin/env python3 import sys from pprint import pprint MOD = 1000000007 # type: int def solve(N: int, M: int, S: "List[int]", T: "List[int]"): dp = [[0] * (M + 1) for _ in range(N + 1)] for i in range(N + 1): dp[i][0] = 1 for j in range(M + 1): dp[0][j] = 1 for i in range(N): for j in range(M): dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] if not S[i] == T[j]: dp[i + 1][j + 1] -= dp[i][j] #pprint(dp) ret = dp[N][M] % MOD print(ret) return def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() N = int(next(tokens)) # type: int M = int(next(tokens)) # type: int S = [ int(next(tokens)) for _ in range(N) ] # type: "List[int]" T = [ int(next(tokens)) for _ in range(M) ] # type: "List[int]" solve(N, M, S, T) if __name__ == '__main__': main()
#!/usr/bin/env python3 import sys from pprint import pprint MOD = 1000000007 # type: int def solve(N: int, M: int, S: "List[int]", T: "List[int]"): dp = [[0] * (M + 1) for _ in range(N + 1)] for i in range(N + 1): dp[i][0] = 1 for j in range(M + 1): dp[0][j] = 1 for i in range(N): for j in range(M): dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] if not S[i] == T[j]: dp[i + 1][j + 1] -= dp[i][j] dp[i + 1][j + 1] %= MOD #pprint(dp) ret = dp[N][M] print(ret) return def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() N = int(next(tokens)) # type: int M = int(next(tokens)) # type: int S = [ int(next(tokens)) for _ in range(N) ] # type: "List[int]" T = [ int(next(tokens)) for _ in range(M) ] # type: "List[int]" solve(N, M, S, T) if __name__ == '__main__': main()
p03003
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n):l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n):l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n):l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n):l[i] = LS() return l sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): x,n = LI() if x < n: print((0)) else: print((10)) return #B def B(): n,x = LI() l = LI() d = 0 ans = 1 for i in range(n): d += l[i] if d <= x: ans += 1 print(ans) return #C def C(): w,h,x,y = LI() ans = w*h/2 k = 0 if x == w/2 and y == h/2: k = 1 print((ans,k)) return #D def D(): n,k = LI() a = LI() b = [a[i] for i in range(n)] for i in range(n-1): b[i+1] += b[i] b.insert(0,0) ans = 0 for l in range(n): r = bisect.bisect_left(b,b[l]+k) ans += n-r+1 print(ans) return #E def E(): n,m = LI() s = LI() t = LI() dp = [[0 for i in range(m+1)] for j in range(n+1)] for i in range(n+1): dp[i][0] = 1 for j in range(m+1): dp[0][j] = 1 for i in range(1,n+1): for j in range(1,m+1): if s[i-1] == t[j-1]: dp[i][j] += dp[i-1][j]+dp[i][j-1] else: dp[i][j] += dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1] if dp[i][j] >= mod: dp[i][j] %= mod print((dp[n][m]%mod)) return #F def F(): n = I() x = [None for i in range(n)] y = [None for i in range(n)] f = [None for i in range(n)] for i in range(n): a,b,d = LS() a = int(a) b = int(b) x[i] = a y[i] = b if d == "R": f[i] = (1,0) elif d == "L": f[i] = (-1,0) elif d == "U": f[i] = (0,1) else: f[i] = (0,-1) dx = max(x)-min(x) dy = max(x)-min(y) ans = dx*dy if ans == 0: print((0)) quit() l = 0 r = 100000000 while r-l > 0.5: m = (l+r)/2 px = [x[i]+f[i][0]*m for i in range(n)] py = [y[i]+f[i][1]*m for i in range(n)] dx = max(x)-min(x) dy = max(x)-min(y) if dx*dy < ans: ans = dx*dy return #Solve if __name__ == "__main__": E()
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n):l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n):l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n):l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n):l[i] = LS() return l sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): x,n = LI() if x < n: print((0)) else: print((10)) return #B def B(): n,x = LI() l = LI() d = 0 ans = 1 for i in range(n): d += l[i] if d <= x: ans += 1 print(ans) return #C def C(): w,h,x,y = LI() ans = w*h/2 k = 0 if x == w/2 and y == h/2: k = 1 print((ans,k)) return #D def D(): n,k = LI() a = LI() b = [a[i] for i in range(n)] for i in range(n-1): b[i+1] += b[i] b.insert(0,0) ans = 0 for l in range(n): r = bisect.bisect_left(b,b[l]+k) ans += n-r+1 print(ans) return #E def E(): n,m = LI() s = LI() t = LI() dp = [[0 for i in range(m+1)] for j in range(n+1)] for i in range(n+1): dp[i][0] = 1 for j in range(m+1): dp[0][j] = 1 for i in range(1,n+1): for j in range(1,m+1): if s[i-1] == t[j-1]: dp[i][j] += dp[i-1][j]+dp[i][j-1] else: dp[i][j] += dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1] if dp[i][j] >= mod or 0 > dp[i][j]: dp[i][j] %= mod print((dp[n][m])) return #F def F(): n = I() x = [None for i in range(n)] y = [None for i in range(n)] f = [None for i in range(n)] for i in range(n): a,b,d = LS() a = int(a) b = int(b) x[i] = a y[i] = b if d == "R": f[i] = (1,0) elif d == "L": f[i] = (-1,0) elif d == "U": f[i] = (0,1) else: f[i] = (0,-1) dx = max(x)-min(x) dy = max(x)-min(y) ans = dx*dy if ans == 0: print((0)) quit() l = 0 r = 100000000 while r-l > 0.5: m = (l+r)/2 px = [x[i]+f[i][0]*m for i in range(n)] py = [y[i]+f[i][1]*m for i in range(n)] dx = max(x)-min(x) dy = max(x)-min(y) if dx*dy < ans: ans = dx*dy return #Solve if __name__ == "__main__": E()
p03003
#!/usr/bin/env python3 import sys from collections.abc import Iterable from math import * from itertools import * from collections import * from functools import * from operator import * try: from math import gcd except Exception: from fractions import gcd MOD = 1000000007 # type: int def solve(N: int, M: int, S: "List[int]", T: "List[int]"): dp = [[0] * (M + 1) for _ in range(N + 1)] for i in range(N): for j in range(M): dp[i + 1][j + 1] = (dp[i+1][j] + dp[i][j+1] - dp[i][j]) % MOD if S[i] == T[j]: dp[i+1][j+1] += dp[i][j] + 1 dp[i+1][j+1] %= MOD return (dp[-1][-1] + 1) % MOD def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() N = int(next(tokens)) # type: int M = int(next(tokens)) # type: int S = [ int(next(tokens)) for _ in range(N) ] # type: "List[int]" T = [ int(next(tokens)) for _ in range(M) ] # type: "List[int]" result = solve(N, M, S, T) if isinstance(result, Iterable) and not isinstance(result, str): result = '\n'.join([str(v) for v in result]) print(result) if __name__ == '__main__': main()
#!/usr/bin/env python3 import sys from math import * from itertools import * from collections import * from functools import * from operator import * try: from math import gcd except Exception: from fractions import gcd MOD = 1000000007 # type: int def solve(N: int, M: int, S: "List[int]", T: "List[int]"): dp = [[0] * (M + 1) for _ in range(N + 1)] for i in range(N): for j in range(M): dp[i + 1][j + 1] = (dp[i+1][j] + dp[i][j+1] - dp[i][j]) % MOD if S[i] == T[j]: dp[i+1][j+1] += dp[i][j] + 1 dp[i+1][j+1] %= MOD return (dp[-1][-1] + 1) % MOD def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() N = int(next(tokens)) # type: int M = int(next(tokens)) # type: int S = [ int(next(tokens)) for _ in range(N) ] # type: "List[int]" T = [ int(next(tokens)) for _ in range(M) ] # type: "List[int]" result = solve(N, M, S, T) print(result) if __name__ == '__main__': main()
p03003
[N,M] = list(map(int,input().split())) S = list(map(int,input().split())) T = list(map(int,input().split())) ta = [[0] * (M+1) for i in range(N+1)] ta[0][0]=1 wa = [[0] * (M+1) for i in range(N+1)] wa[0][:]=[1]*(M+1) for i in range(N+1): wa[i][0]=1 output = 1 for i in range(1,N+1): for j in range(1,M+1): if S[i-1]==T[j-1]: ta[i][j]=wa[i-1][j-1] wa[i][j]=wa[i-1][j]+wa[i][j-1] else: wa[i][j]=wa[i-1][j]+wa[i][j-1]-wa[i-1][j-1] output = wa[N][M]%(10**9+7) print((int(output)))
[N,M] = list(map(int,input().split())) S = list(map(int,input().split())) T = list(map(int,input().split())) ta = [[0] * (M+1) for i in range(N+1)] ta[0][0]=1 wa = [[0] * (M+1) for i in range(N+1)] wa[0][:]=[1]*(M+1) for i in range(N+1): wa[i][0]=1 output = 1 for i in range(1,N+1): for j in range(1,M+1): if S[i-1]==T[j-1]: ta[i][j]=wa[i-1][j-1] wa[i][j]=(wa[i-1][j]+wa[i][j-1])%(10**9+7) else: wa[i][j]=(wa[i-1][j]+wa[i][j-1]-wa[i-1][j-1]+(10**9+7))%(10**9+7) output = wa[N][M] print((int(output)))
p03003
n,m=list(map(int,input().split())) S=list(map(int,input().split())) T=list(map(int,input().split())) mod=10**9+7 dp = [0]*m for s in S: k=0 d = [0]*(m+1) for i in range(m): if T[i]==s: k+=dp[i-1]+1 d[i]=(dp[i]+k)%mod dp=d print((dp[-2]+1))
n,m=list(map(int,input().split())) S=list(map(int,input().split())) T=list(map(int,input().split())) p=[0]*m for s in S: k=0 d=[0]*(m+1) for i in range(m): if T[i]==s: k+=p[i-1]+1 d[i]=(p[i]+k)%(10**9+7) p=d print((p[-2]+1))
p03003
N, M = list(map(int, input().split())) *S, = list(map(int, input().split())) *T, = list(map(int, input().split())) MOD = 10**9 + 7 P = [] for i in range(N): for j in range(M): if S[i] == T[j]: P.append((i, j)) P.sort(key=lambda x: (x[0], -x[1])) data = [0]*(M+2) def get(k): s = 0 while k: s += data[k] k -= k & -k return s % MOD def add(k, x): while k <= M: data[k] = (data[k] + x) % MOD k += k & -k s = 0 for p, q in P: v = get(q+1) + 1 add(q+2, v); s += v print(((s + 1) % MOD))
N, M = list(map(int, input().split())) *S, = list(map(int, input().split())) *T, = list(map(int, input().split())) data = [0]*(M+2) def get(k): s = 0 while k: s += data[k] k -= k & -k return s % MOD def add(k, x): while k <= M: data[k] = (data[k] + x) % MOD k += k & -k MOD = 10**9 + 7 s = 0 rM = list(range(M-1, -1, -1)) for i in range(N): si = S[i] for j in rM: if si == T[j]: v = get(j+1) + 1 add(j+2, v); s += v print(((s + 1) % MOD))
p03003
mod=10**9+7 n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp1=[1]*(m+1) dp2=[1]*(m+1) for i in range(n): if i%2==0: for j in range(m): if s[i]==t[j]:dp1[j+1]=dp2[j+1]+dp1[j] else:dp1[j+1]=dp2[j+1]+dp1[j]-dp2[j] dp1[j+1]%=mod else: for j in range(m): if s[i]==t[j]:dp2[j+1]=dp1[j+1]+dp2[j] else:dp2[j+1]=dp1[j+1]+dp2[j]-dp1[j] dp2[j+1]%=mod if n%2==0:print((dp2[-1])) else:print((dp1[-1]))
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[(m+1)*[1]for _ in range(n+1)] for i in range(n): for j in range(m): dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j] if s[i]!=t[j]:dp[i+1][j+1]-=dp[i][j] dp[i+1][j+1]%=(10**9+7) print((dp[-1][-1]))
p03003
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[(m+1)*[1]for _ in range(n+1)] for i in range(n): for j in range(m): dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j] if s[i]!=t[j]:dp[i+1][j+1]-=dp[i][j] dp[i+1][j+1]%=(10**9+7) print((dp[-1][-1]))
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[[1]*(m+1)for _ in range(n+1)] dp[0][0]=1 for i in range(1,n+1): for j in range(1,m+1): dp[i][j]=dp[i-1][j]+dp[i][j-1] if s[i-1]!=t[j-1]: dp[i][j]-=dp[i-1][j-1] dp[i][j]%=10**9+7 print((dp[n][m]))
p03003
mod = 10**9+7 n, m = list(map(int,input().split())) s = list(map(int,input().split())) t = list(map(int,input().split())) dp = [[0]*(m+1) for _ in range(n+1)] for i in range(n)[::-1]: for j in range(m)[::-1]: if s[i] == t[j]: dp[i][j] += 1 + dp[i+1][j+1] dp[i][j] %= mod dp[i][j] += dp[i+1][j] + dp[i][j+1] - dp[i+1][j+1] dp[i][j] %= mod print(((dp[0][0]+1)%mod))
mod = 10**9+7 n, m = list(map(int,input().split())) s = list(map(int,input().split())) t = list(map(int,input().split())) dp = [0]*(m+1) for i in range(n)[::-1]: old = 0 for j in range(m)[::-1]: tmp = dp[j] + dp[j+1] - old tmp %= mod if s[i]== t[j]: tmp += 1 + old tmp %= mod dp[j], old = tmp, dp[j] print(((dp[0]+1)%mod))
p03003
def solve(): MOD = 10**9 + 7 N, M = list(map(int, input().split())) Ss = list(map(int, input().split())) Ts = list(map(int, input().split())) dp = [[0]*(M+1) for _ in range(N+1)] for i in range(N+1): dp[i][0] = 1 for j in range(M+1): dp[0][j] = 1 for i, S in enumerate(Ss, start=1): for j, T in enumerate(Ts, start=1): dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] if S == T: dp[i][j] += dp[i-1][j-1] dp[i][j] %= MOD print((dp[N][M])) solve()
def solve(): MOD = 10**9 + 7 N, M = list(map(int, input().split())) Ss = list(map(int, input().split())) Ts = list(map(int, input().split())) dp = [1]*(M+1) for i, S in enumerate(Ss, 1): dp2 = [1]*(M+1) for j, T in enumerate(Ts, 1): dp2[j] = dp[j] + dp2[j-1] if S != T: dp2[j] -= dp[j-1] dp2[j] %= MOD dp = dp2 print((dp[-1])) solve()
p03003
import sys sys.setrecursionlimit(1000000000) mod = 10**9+7 N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) memo = [[-1]*(M+1) for _ in range(N)] List = [[]for _ in range(N)] for i in range(N): for j in range(M): if S[i]==T[j]: List[i].append(j) def dp(i, j): if i==N-1: cnt = 0 for k in List[i]: if j<k: cnt+=1 return (cnt+1)%mod res = 0 for k in List[i]: if j<k: if memo[i+1][k]<0: memo[i+1][k]=dp(i+1, k) res+=memo[i+1][k] if memo[i+1][j]<0: memo[i+1][j]=dp(i+1, j) res+=memo[i+1][j] return res%mod print((dp(0, -1)%mod))
mod = 10**9+7 N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) dp = [[0]*(M+1) for _ in range(N+1)] dp[0][0] = 1 Sum = [[1]*(M+1) for _ in range(N+1)] for i in range(1, N+1): for j in range(1, M+1): if S[i-1] == T[j-1]: dp[i][j] = Sum[i-1][j-1]%mod else: dp[i][j] = 0 Sum[i][j] = (Sum[i-1][j]+Sum[i][j-1]-Sum[i-1][j-1]+dp[i][j])%mod print((Sum[N][M]%mod))
p03003
MOD = int(1e9 + 7) def f(N, M, s, t): dp = [[0] * (M + 1) for _ in range(N + 1)] cums = [[0] * (M + 1) for _ in range(N + 1)] for i in range(1, N + 1): for j in range(1, M + 1): dp[i][j] = 0 if s[i-1] != t[j-1] else (cums[i-1][j-1] + 1) % MOD cums[i][j] = cums[i-1][j] + cums[i][j-1] - cums[i-1][j-1] + dp[i][j] return((cums[N][M] + 1) % MOD) n, m = list(map(int, input().split())) s = list(map(int, input().split())) t = list(map(int, input().split())) print((f(n, m, s, t)))
# coding: utf-8 MOD = int(1e9 + 7) def f(N, M, s, t): dp = [[0] * (M + 1) for _ in range(N + 1)] cums = [[0] * (M + 1) for _ in range(N + 1)] for i in range(1, N + 1): for j in range(1, M + 1): dp[i][j] = 0 if s[i-1] != t[j-1] else (cums[i-1][j-1] + 1) % MOD cums[i][j] = cums[i-1][j] + cums[i][j-1] - cums[i-1][j-1] + dp[i][j] return((cums[N][M] + 1) % MOD) n, m = list(map(int, input().split())) s = list(map(int, input().split())) t = list(map(int, input().split())) print((f(n, m, s, t)))
p03003
import sys from collections import deque # 双方向キュー from collections import defaultdict # 初期化済み辞書 from heapq import heapify, heappush, heappop, heappushpop # プライオリティキュー from bisect import bisect_left, bisect_right # 二分探索 #import numpy as np # 1.8.2 #import scipy # 0.13.3 s2nn = lambda s: [int(c) for c in s.split(' ')] ss2nn = lambda ss: [int(s) for s in ss] ss2nnn = lambda ss: [s2nn(s) for s in ss] i2s = lambda: sys.stdin.readline().rstrip() i2n = lambda: int(i2s()) i2nn = lambda: s2nn(i2s()) ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)] ii2nn = lambda n: ss2nn(ii2ss(n)) ii2nnn = lambda n: ss2nnn(ii2ss(n)) MOD = int(1e+9) + 7 def main(): N, M = i2nn() S = i2nn() T = i2nn() rui = [[0] * (M + 1) for _ in range(N + 1)] cnt = [[0] * (M) for _ in range(N)] for i, s in enumerate(S): for j, t in enumerate(T): c = 0 if s == t: c += rui[i][j] + 1 cnt[i][j] = c rui[i+1][j+1] = rui[i+1][j] + rui[i][j+1] - rui[i][j] + c n = 1 for v in cnt: n = (n + sum(v)) % MOD #print(rui) #print(cnt) print(n) main()
import sys from collections import deque # 双方向キュー from collections import defaultdict # 初期化済み辞書 from heapq import heapify, heappush, heappop, heappushpop # プライオリティキュー from bisect import bisect_left, bisect_right # 二分探索 #import numpy as np # 1.8.2 #import scipy # 0.13.3 s2nn = lambda s: [int(c) for c in s.split(' ')] ss2nn = lambda ss: [int(s) for s in ss] ss2nnn = lambda ss: [s2nn(s) for s in ss] i2s = lambda: sys.stdin.readline().rstrip() i2n = lambda: int(i2s()) i2nn = lambda: s2nn(i2s()) ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)] ii2nn = lambda n: ss2nn(ii2ss(n)) ii2nnn = lambda n: ss2nnn(ii2ss(n)) MOD = int(1e+9) + 7 def main(): N, M = i2nn() S = i2nn() T = i2nn() rui = [[0] * (M + 1) for _ in range(N + 1)] cnt = [[0] * (M) for _ in range(N)] n = 1 for i, s in enumerate(S): for j, t in enumerate(T): c = 0 if s == t: c += rui[i][j] + 1 #cnt[i][j] = c rui[i+1][j+1] = rui[i+1][j] + rui[i][j+1] - rui[i][j] + c n = (n + c) % MOD #for v in cnt: # n = (n + sum(v)) % MOD #print(rui) #print(cnt) print(n) main()
p03003
import sys from collections import deque # 双方向キュー from collections import defaultdict # 初期化済み辞書 from heapq import heapify, heappush, heappop, heappushpop # プライオリティキュー from bisect import bisect_left, bisect_right # 二分探索 #import numpy as np # 1.8.2 #import scipy # 0.13.3 s2nn = lambda s: [int(c) for c in s.split(' ')] ss2nn = lambda ss: [int(s) for s in ss] ss2nnn = lambda ss: [s2nn(s) for s in ss] i2s = lambda: sys.stdin.readline().rstrip() i2n = lambda: int(i2s()) i2nn = lambda: s2nn(i2s()) ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)] ii2nn = lambda n: ss2nn(ii2ss(n)) ii2nnn = lambda n: ss2nnn(ii2ss(n)) MOD = int(1e+9) + 7 def main(): N, M = i2nn() S = i2nn() T = i2nn() rui = [[0] * (M + 1) for _ in range(N + 1)] cnt = [[0] * (M) for _ in range(N)] n = 1 for i, s in enumerate(S): for j, t in enumerate(T): c = 0 if s == t: c += rui[i][j] + 1 #cnt[i][j] = c rui[i+1][j+1] = rui[i+1][j] + rui[i][j+1] - rui[i][j] + c n = (n + c) % MOD #for v in cnt: # n = (n + sum(v)) % MOD #print(rui) #print(cnt) print(n) main()
import sys from collections import deque # 双方向キュー from collections import defaultdict # 初期化済み辞書 from heapq import heapify, heappush, heappop, heappushpop # プライオリティキュー from bisect import bisect_left, bisect_right # 二分探索 #import numpy as np # 1.8.2 #import scipy # 0.13.3 s2nn = lambda s: [int(c) for c in s.split(' ')] ss2nn = lambda ss: [int(s) for s in ss] ss2nnn = lambda ss: [s2nn(s) for s in ss] i2s = lambda: sys.stdin.readline().rstrip() i2n = lambda: int(i2s()) i2nn = lambda: s2nn(i2s()) ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)] ii2nn = lambda n: ss2nn(ii2ss(n)) ii2nnn = lambda n: ss2nnn(ii2ss(n)) MOD = int(1e+9) + 7 def main(): N, M = i2nn() S = i2nn() T = i2nn() rui = [[0] * (M + 1) for _ in range(N + 1)] #cnt = [[0] * (M) for _ in range(N)] n = 1 for i, s in enumerate(S): for j, t in enumerate(T): c = 0 if s == t: c += rui[i][j] + 1 #cnt[i][j] = c rui[i+1][j+1] = (rui[i+1][j] + rui[i][j+1] - rui[i][j] + c) % MOD n = (n + c) % MOD #for v in cnt: # n = (n + sum(v)) % MOD #print(rui) #print(cnt) print(n) main()
p03003
n,m=list(map(int,input().split())) s=[int(i) for i in input().split()] t=[int(i) for i in input().split()] maxim=(10**3)*2 mod=10**9+7 dp=[[0 for i in range(m+1)]for j in range(n+1)] for i in range(n): for j in range(m): x=0 if s[i]==t[j]: x=dp[i][j]+1 dp[i+1][j+1]=(dp[i+1][j]+dp[i][j+1]-dp[i][j]+x)%mod #print(dp) print(((dp[n][m]+1)%mod))
n,m=list(map(int,input().split())) s=[int(i) for i in input().split()] t=[int(i) for i in input().split()] mod=10**9+7 dp=[[0 for i in range(m+1)]for j in range(n+1)] for i in range(n): for j in range(m): x=0 if s[i]==t[j]: x=dp[i][j]+1 dp[i+1][j+1]=(dp[i+1][j]+dp[i][j+1]-dp[i][j]+x)%mod #print(dp) print(((dp[n][m]+1)%mod))
p03003
md=10**9+7 n,m=list(map(int,input().split())) s=list(input().split()) t=list(input().split()) dp=[[1]*(m+1) for _ in range(n+1)] for i,sk in enumerate(s): dp[i+1]=dp[i][:] cs=0 for j,tk in enumerate(t): if sk==tk: cs+=dp[i][j] dp[i+1][j+1]+=cs print((dp[-1][-1]%md))
def f(n, m): s = list(input().split()) t = list(input().split()) dp = [[1] * (m + 1) for _ in range(n + 1)] for i, sk in enumerate(s): dpi1 = dp[i + 1] = dp[i][:] cs = 0 for j, tk in enumerate(t): if sk == tk: cs = (cs + dp[i][j]) % md dpi1[j + 1] = (dpi1[j + 1] + cs) % md # for x in dp: # print(*x) print((dp[-1][-1])) md = 10 ** 9 + 7 n, m = list(map(int, input().split())) f(n, m)
p03003
def f(n, m): s = list(map(int,input().split())) t = list(map(int,input().split())) dp = [[1] * (m + 1) for _ in range(n + 1)] for i, sk in enumerate(s): dpi1 = dp[i + 1] = dp[i][:] cs = 0 for j, tk in enumerate(t): if sk == tk: cs = (cs + dp[i][j]) % md dpi1[j + 1] = (dpi1[j + 1] + cs) % md # for x in dp: # print(*x) print((dp[-1][-1])) md = 10 ** 9 + 7 n, m = list(map(int, input().split())) f(n, m)
def f(n, m): s = list(map(int,input().split())) t = list(map(int,input().split())) dpi = [1] * (m + 1) for i, sk in enumerate(s): dpi1 = dpi[:] cs = 0 for j, tk in enumerate(t): if sk == tk: cs = (cs + dpi[j]) % md dpi1[j + 1] = (dpi1[j + 1] + cs) % md dpi=dpi1 # for x in dp: # print(*x) print((dpi1[-1])) md = 10 ** 9 + 7 n, m = list(map(int, input().split())) f(n, m)
p03003
N , M = list(map(int,input().split())) S = list(map(int,input().split())) T = list(map(int,input().split())) # 累積和参照のために0の行,列を追加 sm = [[0] * (M+1) for _ in range(N+1)] for i , s in enumerate(S) : l1 = sm[i][:] l2 = 0 for j , t in enumerate(T) : new = l1[j+1] + l2 if s == t : new += 1 # dp = sm[i-1][j-1] + 1 else : new -= l1[j] # dp = 0 l2 = new sm[i+1][j+1] = new ans = (sm[-1][-1] + 1) % (10**9 + 7) print(ans)
N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) MOD = 10**9 + 7 dp = [[0] * (M + 1) for _ in range(N + 1)] # dp[s][t] for i, s in enumerate(S, start=1): for j, t in enumerate(T, start=1): v = dp[i - 1][j] + dp[i][j - 1] if s == t: v += 1 else: v -= dp[i - 1][j - 1] dp[i][j] = v % MOD print(((dp[-1][-1] + 1) % MOD))
p03003
N, M = list(map(int, input().split())) S = list(input().split()) T = list(input().split()) MOD = 10**9+7 dp = [[0] * (M+1) for _ in range(N+1)] sum = [[0] * (M+1) for _ in range(N+1)] dp[0][0] = 1 for i in range(N+1): sum[i][0] = 1 for j in range(M+1): sum[0][j] = 1 for i in range(N): for j in range(M): dp[i+1][j+1] = (S[i] == T[j]) * sum[i][j] % MOD sum[i+1][j+1] = (sum[i+1][j] + sum[i][j+1] - sum[i][j] + dp[i+1][j+1]) % MOD print((sum[N][M]))
N, M = list(map(int, input().split())) S = list(input().split()) T = list(input().split()) MOD = 10**9+7 dp = [[0] * (M+1) for _ in range(N+1)] sum = [[0] * (M+1) for _ in range(N+1)] dp[0][0] = 1 for i in range(N+1): sum[i][0] = 1 for j in range(M+1): sum[0][j] = 1 for i in range(N): for j in range(M): if S[i] == T[j]: dp[i+1][j+1] = sum[i][j] sum[i+1][j+1] = (sum[i+1][j] + sum[i][j+1] - sum[i][j] + dp[i+1][j+1]) % MOD print((sum[N][M]))
p03003
import sys sys.setrecursionlimit(2147483647) INF=float("inf") MOD=10**9+7 input=lambda :sys.stdin.readline().rstrip() def resolve(): n,m=list(map(int,input().split())) S=list(map(int,input().split())) T=list(map(int,input().split())) C=[[0]*(m+1) for _ in range(n+1)] from itertools import product for i,j in product(list(range(n)),list(range(m))): dp=C[i][j]+1 if(S[i]==T[j]) else 0 C[i+1][j+1]=dp+C[i][j+1]+C[i+1][j]-C[i][j] C[i+1][j+1]%=MOD print((C[n][m]+1)) resolve()
import sys sys.setrecursionlimit(2147483647) INF=float("inf") MOD=10**9+7 input=lambda:sys.stdin.readline().rstrip() def resolve(): n,m=list(map(int,input().split())) S=list(map(int,input().split())) T=list(map(int,input().split())) C=[[0]*(m+1) for _ in range(n+1)] # dp の累積和を持つ for i in range(n): C[i][0]=1 for j in range(m): C[0][j]=1 for i in range(n): for j in range(m): if(S[i]==T[j]): C[i+1][j+1]+=C[i][j] C[i+1][j+1]+=C[i+1][j]+C[i][j+1]-C[i][j] C[i+1][j+1]%=MOD print(((C[-1][-1]+2)%MOD)) resolve()
p03003
def I(): return int(eval(input())) def MI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 N,M=MI() S=LI() T=LI() dp=[[0]*(M+1) for _ in range(N+1)] dp[0][0]=1 #dp[i][j]はSのi文字目,Tのj文字目を使う時の通り数 cumsum=[[0]*(M+2) for _ in range(N+2)] #2D累積和 for i in range(1,N+2): cumsum[i][1]=1 for j in range(1,M+2): cumsum[1][j]=1 for i in range(N): for j in range(M): if S[i]==T[j]: dp[i+1][j+1]=(cumsum[i+1][j+1])%mod cumsum[i+2][j+2]=(cumsum[i+2][j+1]+cumsum[i+1][j+2]-cumsum[i+1][j+1]+dp[i+1][j+1])%mod print((cumsum[-1][-1])) """ for i in range(N+2): print(' '.join(map(str, cumsum[i]))) for i in range(N+1): print(' '.join(map(str, dp[i])))""" main()
def I(): return int(eval(input())) def MI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) def main(): #シンプルに!! mod=10**9+7 N,M=MI() S=LI() T=LI() dp=[[0]*(M+1) for _ in range(N+1)] #i文字目までとj文字目まで見た. for i in range(N+1): dp[i][0]=1 for j in range(M+1): dp[0][j]=1 for i in range(N): for j in range(M): #まとめたせいで繊維が少し不自然かも #基本はdp[i][j]を2回数えているのでひく,でもi文字目とj文字目が一致していればたす dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j] if S[i]!=T[j]: dp[i+1][j+1]-=dp[i][j] dp[i+1][j+1]%=mod print((dp[-1][-1])) main()
p03003
def I(): return int(eval(input())) def MI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) def main(): #シンプルに!! mod=10**9+7 N,M=MI() S=LI() T=LI() dp=[[0]*(M+1) for _ in range(N+1)] #i文字目までとj文字目まで見た. for i in range(N+1): dp[i][0]=1 for j in range(M+1): dp[0][j]=1 for i in range(N): for j in range(M): #まとめたせいで繊維が少し不自然かも #基本はdp[i][j]を2回数えているのでひく,でもi文字目とj文字目が一致していればたす dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j] if S[i]!=T[j]: dp[i+1][j+1]-=dp[i][j] dp[i+1][j+1]%=mod print((dp[-1][-1])) main()
def I(): return int(eval(input())) def MI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 N,M=MI() S=LI() T=LI() dp=[[0]*(M+1) for _ in range(N+1)] # dp[i][j]はSをi文字,Tをj文字見た時の通り数 for i in range(N+1): dp[i][0]=1 for j in range(M+1): dp[0][j]=1 for i in range(N): for j in range(M): dp[i+1][j+1]=dp[i+1][j]+dp[i][j+1] if S[i]!=T[j]: dp[i+1][j+1]-=dp[i][j] dp[i+1][j+1]%=mod print((dp[-1][-1])) main()
p03003
N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) MOD = 10**9 + 7 dp = [[0] * (M + 2) for _ in range(N + 2)] sdp = [[0] * (M + 2) for _ in range(N + 2)] dp[0][0] = 1 sdp[1][1] = 1 for i in range(N + 1): for j in range(M + 1): if i - 1 >= 0 and j - 1 >= 0 and S[i - 1] == T[j - 1]: dp[i][j] = sdp[i][j] sdp[i + 1][j + 1] = sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j] + dp[i][j] sdp[i + 1][j + 1] %= MOD sdp[i + 1][j + 1] %= MOD print((sdp[N + 1][M + 1]))
# dp[i][j] SとTの最後の文字を採用するような文字列の個数。これにより各DPテーブルの値が排反になる。元の文字列の最後を必ず採用するので N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) MOD = 10**9 + 7 dp = [[0] * (M + 2) for _ in range(N + 2)] sdp = [[0] * (M + 2) for _ in range(N + 2)] dp[0][0] = 1 sdp[1][1] = 1 for i in range(N + 1): for j in range(M + 1): if i - 1 >= 0 and j - 1 >= 0 and S[i - 1] == T[j - 1]: dp[i][j] = sdp[i][j] sdp[i + 1][j + 1] = sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j] + dp[i][j] sdp[i + 1][j + 1] %= MOD sdp[i + 1][j + 1] %= MOD print((sdp[N + 1][M + 1]))
p03003
N,M=list(map(int,input().split())) S=list(map(int,input().split())) T=list(map(int,input().split())) dp=[[0]*(M+1) for i in range(N+1)] mod=10**9+7 def Mod(n,mod): return n-mod if n>=mod else n for n in range(1,N+1): for m in range(1,M+1): x=dp[n-1][m-1]+1 if S[n-1]==T[m-1] else 0 dp[n][m]=Mod(dp[n-1][m]+dp[n][m-1]-dp[n-1][m-1]+x,mod) print(((dp[-1][-1]+1)%mod))
N,M=list(map(int,input().split())) S=list(map(int,input().split())) T=list(map(int,input().split())) dp=[[0]*(M+1) for i in range(N+1)] mod=10**9+7 for n in range(1,N+1): for m in range(1,M+1): x=1 if S[n-1]==T[m-1] else -dp[n-1][m-1] y=dp[n-1][m]+dp[n][m-1]+x dp[n][m]=y if y<mod else y-mod print(((dp[-1][-1]+1)%mod))
p03003
N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) S.append(0) T.append(0) MOD = 10 ** 9 + 7 dp0 = [[0 for _ in range(2005)] for _ in range(2005)] dp1 = [[0 for _ in range(2005)] for _ in range(2005)] dp0[0][0] = 1 for i in range(N + 1): for j in range(M + 1): dp0[i + 1][j] += dp0[i][j] dp1[i][j] += dp0[i][j] dp1[i][j + 1] += dp1[i][j] if S[i] == T[j]: dp0[i + 1][j + 1] += dp1[i][j] ans = dp1[N][M] print((ans % MOD))
N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) S.append(0) T.append(0) MOD = 1000000007 dp0 = [[0 for _ in range(2005)] for _ in range(2005)] dp1 = [[0 for _ in range(2005)] for _ in range(2005)] dp0[0][0] = 1 for i in range(N + 1): for j in range(M + 1): dp0[i + 1][j] += dp0[i][j] % MOD dp1[i][j] += dp0[i][j] % MOD dp1[i][j + 1] += dp1[i][j] % MOD if S[i] == T[j]: dp0[i + 1][j + 1] += dp1[i][j] % MOD ans = dp1[N][M] print((ans % MOD))
p03003
# 解説AC from collections import defaultdict N,M = list(map(int, input().split())) S = [int(i) for i in input().split()] T = [int(i) for i in input().split()] MOD = 10 ** 9 + 7 # dp[i][j]: S[:i]とT[:j]での共通部分列の個数 dp = [[0] * (M + 1) for _ in range(N + 1)] # 初期条件 for i in range(N + 1): dp[i][0] = 1 for j in range(M + 1): dp[0][j] = 1 # 貰うDP for i, s in enumerate(S): for j, t in enumerate(T): dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] if s == t: dp[i + 1][j + 1] += dp[i][j] # ダブルカウントを除く dp[i + 1][j + 1] -= dp[i][j] dp[i + 1][j + 1] %= MOD print((dp[N][M]))
# 解説AC N,M = list(map(int, input().split())) S = [int(i) for i in input().split()] T = [int(i) for i in input().split()] MOD = 10 ** 9 + 7 # dp[i][j]: S[:i]とT[:j]での共通部分列の個数 dp = [[0] * (M + 1) for _ in range(N + 1)] def main(): # 初期条件 for i in range(N + 1): dp[i][0] = 1 for j in range(M + 1): dp[0][j] = 1 # 貰うDP for i, s in enumerate(S): for j, t in enumerate(T): dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] if s == t: dp[i + 1][j + 1] += dp[i][j] # ダブルカウントを除く dp[i + 1][j + 1] -= dp[i][j] dp[i + 1][j + 1] %= MOD print((dp[N][M])) if __name__ == "__main__": main()
p03003
# 解説AC from collections import defaultdict N,M = list(map(int, input().split())) S = [int(i) for i in input().split()] T = [int(i) for i in input().split()] MOD = 10 ** 9 + 7 # (i, j): S[:i]とT[:j]での共通部分列の個数 dp = defaultdict(int) # 初期条件 for i in range(N + 1): dp[(i, 0)] = 1 for j in range(M + 1): dp[(0, j)] = 1 # 貰うDP for i, s in enumerate(S): for j, t in enumerate(T): dp[(i + 1, j + 1)] = dp[(i, j + 1)] + dp[(i + 1, j)] if s == t: dp[(i + 1, j + 1)] += dp[(i, j)] # ダブルカウントを除く dp[(i + 1, j + 1)] -= dp[(i, j)] dp[(i + 1, j + 1)] %= MOD print((dp[(N, M)]))
# 解説AC N,M = list(map(int, input().split())) S = [int(i) for i in input().split()] T = [int(i) for i in input().split()] MOD = 10 ** 9 + 7 # dp[i][j]: S[:i]とT[:j]の共通部分列のうち、 # S[i - 1] == T[j - 1]で、これを選ぶ場合の個数 dp = [[0] * (M + 1) for _ in range(N + 1)] # DPの二次元累積和 sdp = [[0] * (M + 1) for _ in range(N + 1)] # 初期条件 dp[0][0] = 1 for i in range(N + 1): sdp[i][0] = 1 for j in range(M + 1): sdp[0][j] = 1 for i, s in enumerate(S): for j, t in enumerate(T): if s == t: dp[i + 1][j + 1] = sdp[i][j] sdp[i + 1][j + 1] = sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j] + dp[i + 1][j + 1] sdp[i + 1][j + 1] %= MOD print((sdp[N][M]))
p03003
# 入力 N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) MOD = 10**9 + 7 class ModInt: def __init__(self, x): self.x = x % MOD def __str__(self): return str(self.x) __repr__ = __str__ def __add__(self, other): return ( ModInt(self.x + other.x) if isinstance(other, ModInt) else ModInt(self.x + other) ) def __sub__(self, other): return ( ModInt(self.x - other.x) if isinstance(other, ModInt) else ModInt(self.x - other) ) def __mul__(self, other): return ( ModInt(self.x * other.x) if isinstance(other, ModInt) else ModInt(self.x * other) ) def __truediv__(self, other): return ( ModInt( self.x * pow(other.x, MOD - 2, MOD) ) if isinstance(other, ModInt) else ModInt(self.x * pow(other, MOD - 2, MOD)) ) def __pow__(self, other): return ( ModInt( pow(self.x, other.x, MOD) ) if isinstance(other, ModInt) else ModInt(pow(self.x, other, MOD)) ) def __radd__(self, other): return ModInt(other + self.x) def __rsub__(self, other): return ModInt(other - self.x) def __rmul__(self, other): return ModInt(other * self.x) def __rtruediv__(self, other): return ModInt(other * pow(self.x, MOD - 2, MOD)) def __rpow__(self, other): return ModInt(pow(other, self.x, MOD)) # LCSと同様にDPで解を求める dp = [[ModInt(1) for _ in range(M + 1)] for _ in range(N + 1)] for i in range(1, N + 1): for j in range(1, M + 1): dp[i][j] = ( dp[i - 1][j] + dp[i][j - 1] if S[i - 1] == T[j - 1] else (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) ) ans = dp[N][M] # 出力 print(ans)
# 入力 N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) MOD = 10**9 + 7 class ModInt: def __init__(self, x): self.x = x % MOD def __str__(self): return str(self.x) __repr__ = __str__ def __add__(self, other): return ( ModInt(self.x + other.x) if isinstance(other, ModInt) else ModInt(self.x + other) ) def __sub__(self, other): return ( ModInt(self.x - other.x) if isinstance(other, ModInt) else ModInt(self.x - other) ) def __mul__(self, other): return ( ModInt(self.x * other.x) if isinstance(other, ModInt) else ModInt(self.x * other) ) def __truediv__(self, other): return ( ModInt( self.x * pow(other.x, MOD - 2, MOD) ) if isinstance(other, ModInt) else ModInt(self.x * pow(other, MOD - 2, MOD)) ) def __pow__(self, other): return ( ModInt( pow(self.x, other.x, MOD) ) if isinstance(other, ModInt) else ModInt(pow(self.x, other, MOD)) ) def __radd__(self, other): return ModInt(other + self.x) def __rsub__(self, other): return ModInt(other - self.x) def __rmul__(self, other): return ModInt(other * self.x) def __rtruediv__(self, other): return ModInt(other * pow(self.x, MOD - 2, MOD)) def __rpow__(self, other): return ModInt(pow(other, self.x, MOD)) # LCSと同様にDPで解を求める dp = [[ModInt(1) for _ in range(M + 1)] for _ in range(N + 1)] for i in range(1, N + 1): for j in range(1, M + 1): dp[i][j] = ( dp[i - 1][j] + dp[i][j - 1] if S[i - 1] == T[j - 1] else dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] ) ans = dp[N][M] # 出力 print(ans)
p03003
from copy import deepcopy as de n,m =[int(i) for i in input().split()] s=[int(i) for i in input().split()] t=[int(i) for i in input().split()] a=[1]*(m+1) for i,b in enumerate(s): d=de(a) k=0 for j,c in enumerate(t): d[j]=(k+d[j])%1000000007 if b==c: k=(k+a[j])%1000000007 d[-1]=(k+d[-1])%1000000007 a=de(d) print((a[-1]%1000000007))
n,m =[int(i) for i in input().split()] s=[int(i) for i in input().split()] t=[int(i) for i in input().split()] a=[1]*(m+1) for b in s: d=a[:] k=0 for j,c in enumerate(t): d[j]=(k+d[j])%1000000007 if b==c: k=(k+a[j])%1000000007 d[-1]=(k+d[-1])%1000000007 a=d[:] print((a[-1]))
p03003
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n):l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n):l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n):l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n):l[i] = LS() return l sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): x,n = LI() if x < n: print((0)) else: print((10)) return #B def B(): n,x = LI() l = LI() d = 0 ans = 1 for i in range(n): d += l[i] if d <= x: ans += 1 print(ans) return #C def C(): w,h,x,y = LI() ans = w*h/2 k = 0 if x == w/2 and y == h/2: k = 1 print((ans,k)) return #D def D(): n,k = LI() a = LI() b = [a[i] for i in range(n)] for i in range(n-1): b[i+1] += b[i] b.insert(0,0) ans = 0 for l in range(n): r = bisect.bisect_left(b,b[l]+k) ans += n-r+1 print(ans) return #E def E(): n,m = LI() s = LI() t = LI() dp = [[0 for i in range(m+1)] for j in range(n+1)] for i in range(n+1): dp[i][0] = 1 for j in range(m+1): dp[0][j] = 1 for i in range(1,n+1): for j in range(1,m+1): if s[i-1] == t[j-1]: dp[i][j] = dp[i-1][j]+dp[i][j-1] else: dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1] if dp[i][j] < 0 or dp[i][j] >= mod: dp[i][j] %= mod print((dp[n][m])) return #F def F(): n = I() x = [None for i in range(n)] y = [None for i in range(n)] f = [None for i in range(n)] for i in range(n): a,b,d = input().split() print((a,b,d)) a = int(a) b = int(b) x[i] = a y[i] = b if d == "R": f[i] = (1,0) elif d == "L": f[i] = (-1,0) elif d == "U": f[i] = (0,1) else: f[i] = (0,-1) dx = max(x)-min(x) dy = max(x)-min(y) ans = dx*dy if ans == 0: print((0)) quit() ma = 100000000 p = [0,ma/3,2*ma/3,ma] for i in range(100): x1 = [x[i]+f[i][0]*p[1] for i in range(n)] x2 = [x[i]+f[i][0]*p[2] for i in range(n)] dx1 = max(x1)-min(x1) dx2 = max(x2)-min(x2) if dx1 < dx2: p[3] = p[2] p[1] = (p[3]-p[0])/3+p[0] p[2] = (p[3]-p[0])/3*2+p[0] else: p[0] = p[1] p[1] = (p[3]-p[0])/3+p[0] p[2] = (p[3]-p[0])/3*2+p[0] print((x1,p)) a = sum(p)/4 print(p) p = [-ma,2*ma/3-ma,2*ma/3*2-ma,ma] for i in range(100): x1 = [y[i]+f[i][1]*p[1] for i in range(n)] x2 = [y[i]+f[i][1]*p[2] for i in range(n)] dx1 = max(x1)-min(x1) dx2 = max(x2)-min(x2) if dx1 < dx2: p[3] = p[2] p[1] = (p[3]-p[0])/3+p[0] p[2] = (p[3]-p[0])/3*2+p[0] else: p[0] = p[1] p[1] = (p[3]-p[0])/3+p[0] p[2] = (p[3]-p[0])/3*2+p[0] b = sum(p)/4 print((a,b)) return #Solve if __name__ == "__main__": E()
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n):l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n):l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n):l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n):l[i] = LS() return l sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): x,n = LI() if x < n: print((0)) else: print((10)) return #B def B(): n,x = LI() l = LI() d = 0 ans = 1 for i in range(n): d += l[i] if d <= x: ans += 1 print(ans) return #C def C(): w,h,x,y = LI() ans = w*h/2 k = 0 if x == w/2 and y == h/2: k = 1 print((ans,k)) return #D def D(): n,k = LI() a = LI() b = [a[i] for i in range(n)] for i in range(n-1): b[i+1] += b[i] b.insert(0,0) ans = 0 for l in range(n): r = bisect.bisect_left(b,b[l]+k) ans += n-r+1 print(ans) return #E def E(): n,m = LI() s = LI() t = LI() dp = [[0 for i in range(m+1)] for j in range(n+1)] for i in range(n+1): dp[i][0] = 1 for j in range(m+1): dp[0][j] = 1 for i in range(1,n+1): for j in range(1,m+1): if s[i-1] == t[j-1]: dp[i][j] = dp[i-1][j]+dp[i][j-1] else: dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+mod if dp[i][j] >= mod: dp[i][j] %= mod print((dp[n][m])) return #F def F(): return #Solve if __name__ == "__main__": E()
p03003
import sys def I(): return int(sys.stdin.readline()) def LI(): return list(map(int,sys.stdin.readline().split())) mod = 10**9 + 7 ans = int(0) N, M = LI() S = LI() T = LI() dp = [[0]*(M+1) for _ in range(N+1)] sumdp = [[0]*(M+1) for _ in range(N+1)] for i in range(1,N+1): for j in range(1,M+1): if S[i-1]==T[j-1]: dp[i][j] = sumdp[i-1][j-1]+1 sumdp[i][j] = sumdp[i-1][j] + sumdp[i][j-1] - sumdp[i-1][j-1] + dp[i][j] #累積和の更新 else: sumdp[i][j] = sumdp[i-1][j] + sumdp[i][j-1] - sumdp[i-1][j-1] # ans += dp[i][j] #print(dp) #for row in dp: # ans+= sum(row)%mod print(((sumdp[N][M]+1)%mod))
import sys def I(): return int(sys.stdin.readline()) def LI(): return list(map(int,sys.stdin.readline().split())) mod = 10**9 + 7 ans = int(0) N, M = LI() S = LI() T = LI() dp = [[0]*(M+1) for _ in range(N+1)] sumdp = [[0]*(M+1) for _ in range(N+1)] for i in range(1,N+1): for j in range(1,M+1): if S[i-1]==T[j-1]: dp[i][j] = sumdp[i-1][j-1]+1 sumdp[i][j] = sumdp[i - 1][j] + sumdp[i][j - 1] +1 # sumdp[i][j] = sumdp[i-1][j] + sumdp[i][j-1] - sumdp[i-1][j-1] + dp[i][j] #累積和の更新 else: sumdp[i][j] = sumdp[i-1][j] + sumdp[i][j-1] - sumdp[i-1][j-1] sumdp[i][j] %=mod # ans += dp[i][j] #print(dp) #for row in dp: # ans+= sum(row)%mod print(((sumdp[N][M]+1)%mod))
p03003
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) mod = 10**9+7 dp = [[0 for j in range(m)]for i in range(n)] for i in range(m): if s[0] ==t[i]: dp[0][i] = 1 for i in range(n): if s[i] ==t[0]: dp[i][0] = 1 for i in range(n): for j in range(m): if s[i] == t[j]: sumdp = 0 for k in range(i): for l in range(j): sumdp += dp[k][l] dp[i][j] = (sumdp + 1) % mod res = 1 for i in range(n): for j in range(m): res += dp[i][j] res %= mod print(res)
n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) mod = 10**9+7 dp = [[0 for j in range(m)]for i in range(n)] for i in range(m): if s[0] ==t[i]: dp[0][i] = 1 for i in range(n): if s[i] ==t[0]: dp[i][0] = 1 sumdp = [[0 for j in range(m)]for i in range(n)] if s[0] ==t[0]: sumdp[0][0] =1 for i in range(m-1): sumdp[0][i+1] =sumdp[0][i] if s[0] == t[i+1]: sumdp[0][i+1] += 1 for i in range(n-1): sumdp[i+1][0]=sumdp[i][0] if s[i+1] ==t[0]: sumdp[i+1][0] += 1 for i in range(n-1): for j in range(m-1): if s[i+1] == t[j+1]: dp[i+1][j+1] = (sumdp[i][j] + 1) % mod sumdp[i+1][j+1] = (sumdp[i][j+1]+sumdp[i+1][j] - sumdp[i][j]+dp[i+1][j+1])%mod print(((sumdp[n-1][m-1]+1)%mod))
p03003
N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) dp = [1] * (M+1) dpleft = [0] * (M) MOD = 10 ** 9 + 7 ndp = [1] * (M+1) for n in range(N): for m in range(M): if S[n] == T[m]: ndp[m+1] = ndp[m] + dp[m+1] dpleft[m] = dp[m+1] else: ndp[m+1] = ndp[m] + dpleft[m] dp[m] = ndp[m] dp[M] = ndp[M] print((dp[-1] % MOD))
N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) dp = [1] * (M+1) dpleft = [0] * (M) MOD = 10 ** 9 + 7 ndp = [1] * (M+1) for n in range(N): for m in range(M): if S[n] == T[m]: ndp[m+1] = (ndp[m] + dp[m+1]) % MOD dpleft[m] = dp[m+1] else: ndp[m+1] = (ndp[m] + dpleft[m]) % MOD dp[m] = ndp[m] dp[-1] = ndp[-1] print((dp[-1]))
p03003
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10**9) INF=10**18 MOD=10**9+7 input=lambda: sys.stdin.readline().rstrip() YesNo=lambda b: bool([print('Yes')] if b else print('No')) YESNO=lambda b: bool([print('YES')] if b else print('NO')) int1=lambda x:int(x)-1 def main(): N,M=map(int,input().split()) S=tuple(map(int,input().split())) T=tuple(map(int,input().split())) dp=[[0]*(M+1) for _ in range(N+1)] SUM=[[0]*(M+2) for _ in range(N+2)] ans=1 for s in range(N): for t in range(M): if S[s]==T[t]: dp[s+1][t+1]=SUM[s+1][t+1]+1 ans+=dp[s+1][t+1] ans%=MOD else: dp[s+1][t+1]=0 SUM[s+2][t+2]=SUM[s+1][t+2]+SUM[s+2][t+1]-SUM[s+1][t+1]+dp[s+1][t+1] print(ans) if __name__ == '__main__': main()
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10**9) INF=10**18 MOD=10**9+7 input=lambda: sys.stdin.readline().rstrip() YesNo=lambda b: bool([print('Yes')] if b else print('No')) YESNO=lambda b: bool([print('YES')] if b else print('NO')) int1=lambda x:int(x)-1 def main(): N,M=map(int,input().split()) S=tuple(map(int,input().split())) T=tuple(map(int,input().split())) dp=[[0]*(M+1) for _ in range(N+1)] SUM=[[0]*(M+2) for _ in range(N+2)] ans=1 for s in range(N): for t in range(M): if S[s]==T[t]: dp[s+1][t+1]=SUM[s+1][t+1]+1 ans+=dp[s+1][t+1] ans%=MOD else: dp[s+1][t+1]=0 SUM[s+2][t+2]=SUM[s+1][t+2]+SUM[s+2][t+1]-SUM[s+1][t+1]+dp[s+1][t+1] SUM[s+2][t+2]%=MOD print(ans) if __name__ == '__main__': main()
p03003
MOD = 10 ** 9 + 7 N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) dp = [[0] * (M + 1) for _ in range(N + 1)] dp[0][0] = 1 sm = [[0] * (M + 1) for _ in range(N + 1)] for i in range(1, N + 1): for j in range(1, M + 1): if S[i - 1] == T[j - 1]: dp[i][j] = (sm[i - 1][j - 1] + 1) % MOD else: dp[i][j] = 0 sm[i][j] = (sm[i - 1][j] + sm[i][j - 1] - sm[i - 1][j - 1] + dp[i][j]) % MOD print((sm[N][M] + 1))
MOD = 10 ** 9 + 7 N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) sm = [[0] * (M + 1) for _ in range(N + 1)] for i in range(N + 1): sm[i][0] = 1 for j in range(M + 1): sm[0][j] = 1 for i in range(1, N + 1): for j in range(1, M + 1): tmp = sm[i - 1][j - 1] if S[i - 1] == T[j - 1] else 0 sm[i][j] = (sm[i - 1][j] + sm[i][j - 1] - sm[i - 1][j - 1] + tmp) % MOD print((sm[N][M]))
p03003
def main(): e=enumerate n,m,*u=list(map(int,open(0).read().split())) dp=[[1]*(m+1)for _ in range(n+1)] for i,s in e(u[:n]): for j,t in e(u[n:]): dp[i+1][j+1]=(dp[i][j+1]+dp[i+1][j]-dp[i][j]*(s!=t))%(10**9+7) print((dp[n][m])) main()
def main(): e=enumerate n,m,*u=list(map(int,open(0).read().split())) dp=[[1]*(m+1)for _ in range(n+1)] dpi=dp[0] for i,s in e(u[:n]): dpi1=dp[i+1] for j,t in e(u[n:]): dpi1[j+1]=(dpi[j+1]+dpi1[j]-dpi[j]*(s!=t))%(10**9+7) dpi=dpi1 print((dpi[m])) main()
p03003
def main(): N, M = list(map(int, input().split())) S = list(map(int, input().split())) + [0] T = list(map(int, input().split())) + [0] dp0 = [[0] * (M + 5) for _ in range(N + 5)] dp1 = [[0] * (M + 5) for _ in range(N + 5)] MOD = 10 ** 9 + 7 dp0[0][0] = 1 for i in range(N + 1): for j in range(M + 1): dp0[i+1][j] += dp0[i][j] dp1[i][j] += dp0[i][j] dp1[i][j+1] += dp1[i][j] if S[i] == T[j]: dp0[i+1][j+1] += dp1[i][j] ans = dp1[N][M] % MOD print(ans) if __name__ == "__main__": main()
def main(): N, M = list(map(int, input().split())) S = list(map(int, input().split())) + [0] T = list(map(int, input().split())) + [0] dp0 = [[0] * (M + 5) for _ in range(N + 5)] dp1 = [[0] * (M + 5) for _ in range(N + 5)] MOD = 10 ** 9 + 7 dp0[0][0] = 1 for i in range(N + 1): for j in range(M + 1): dp0[i+1][j] += dp0[i][j] dp0[i+1][j] %= MOD dp1[i][j] += dp0[i][j] dp1[i][j] %= MOD dp1[i][j+1] += dp1[i][j] dp1[i][j+1] %= MOD if S[i] == T[j]: dp0[i+1][j+1] += dp1[i][j] dp0[i+1][j+1] %= MOD ans = dp1[N][M] % MOD print(ans) if __name__ == "__main__": main()
p03003
mod=1000000007 n,m=list(map(int,input().split())) S=tuple(map(int,input().split())) T=tuple(map(int,input().split())) DP=[[0]*(m+1) for _ in range(n+1)] for i,s in enumerate(S): for j,t in enumerate(T): if s==t: DP[i+1][j+1]=DP[i][j+1]+DP[i+1][j]+1 else: DP[i+1][j+1]=DP[i][j+1]+DP[i+1][j]-DP[i][j] DP[i+1][j+1]%=mod print(((DP[-1][-1]+1)%mod))
mod=1000000007 n,m=list(map(int,input().split())) S=tuple(map(int,input().split())) T=tuple(map(int,input().split())) DP=[[0]*(m+1) for _ in range(n+1)] for i in range(n+1): DP[i][0]=1 for i in range(m+1): DP[0][i]=1 for i,s in enumerate(S): for j,t in enumerate(T): if s==t: DP[i+1][j+1]=DP[i][j+1]+DP[i+1][j] else: DP[i+1][j+1]=DP[i][j+1]+DP[i+1][j]-DP[i][j] DP[i+1][j+1]%=mod print(((DP[-1][-1])%mod))
p03003
import sys readline = sys.stdin.readline N, M = list(map(int, readline().split())) S = list(map(int, readline().split())) T = list(map(int, readline().split())) dp = [[0] * (M + 1) for i in range(N + 1)] for x in range(1, N + 1): for y in range(1, M + 1): dp[x][y] = dp[x][y - 1] + dp[x - 1][y] - dp[x - 1][y - 1] if S[x - 1] == T[y - 1]: dp[x][y] += dp[x - 1][y - 1] + 1 ans = (dp[N][M] + 1) % (10 ** 9 + 7) print(ans)
import sys readline = sys.stdin.readline N, M = list(map(int, readline().split())) S = list(map(int, readline().split())) T = list(map(int, readline().split())) P = 10 ** 9 + 7 dp = [[0] * (M + 1) for i in range(N + 1)] for x in range(1, N + 1): for y in range(1, M + 1): dp[x][y] = dp[x][y - 1] + dp[x - 1][y] - dp[x - 1][y - 1] if S[x - 1] == T[y - 1]: dp[x][y] += dp[x - 1][y - 1] + 1 dp[x][y] = dp[x][y] % P ans = dp[N][M] + 1 print((ans % P))
p03003
n, m = list(map(int, input().split())) s = list(map(int, input().split())) t = list(map(int, input().split())) MOD = 10**9 + 7 dp = [[0]*(m+1) for i in range(n+1)] for si in range(n+1): dp[si][0] = 1 for ti in range(m+1): dp[0][ti] = 1 for si in range(n): for ti in range(m): if s[si] == t[ti]: dp[si+1][ti+1] = dp[si][ti+1] + dp[si+1][ti] dp[si+1][ti+1] %= MOD else: dp[si+1][ti+1] = dp[si][ti+1] + dp[si+1][ti] - dp[si][ti] dp[si+1][ti+1] %= MOD print((dp[-1][-1] % MOD))
def count_cs(str1, str2, MOD): """文字列str1, str2の共通部分列(Common Subsequence, CS)を数え上げる。 添字が異なる場合は異なる部分列として考える。 計算量 O(|str1||str2|) """ dp = [[0] * (len(str2) + 1) for _ in range(len(str1) + 1)] for i in range(len(str1)): for j in range(len(str2)): if str1[i] == str2[j]: dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] + 1 else: dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] - dp[i][j] dp[i + 1][j + 1] %= MOD return (dp[len(str1)][len(str2)] + 1) % MOD n, m = list(map(int, input().split())) s = list(map(int, input().split())) t = list(map(int, input().split())) MOD = 10 ** 9 + 7 print((count_cs(s, t, MOD)))
p03003
from collections import defaultdict as dd N, M = list(map(int, input().split())) MOD = 10**9 + 7 s = list(map(int, input().split())) t = list(map(int, input().split())) dp = [[0 for i in range(M+1)] for j in range(N+1)] for j in range(M): dp[1][j + 1] = dp[1][j] + 1 if s[0] == t[j] else dp[1][j] for i in range(N): dp[i + 1][1] = dp[i][1] + 1 if t[0] == s[i] else dp[i][1] if N > 1 and M > 1: for i in range(1, N): for j in range(1, M): if s[i] == t[j]: dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] + 1 else: dp[i + 1][j + 1] = dp[i][j+1] + dp[i+1][j] - dp[i][j] print(((dp[N][M] + 1)%MOD))
N, M = list(map(int, input().split())) MOD = 10**9 + 7 s = list(map(int, input().split())) t = list(map(int, input().split())) dp = [[0 for i in range(M+1)] for j in range(N+1)] for j in range(M): dp[1][j + 1] = dp[1][j] + 1 if s[0] == t[j] else dp[1][j] for i in range(N): dp[i + 1][1] = dp[i][1] + 1 if t[0] == s[i] else dp[i][1] for i in range(1, N): for j in range(1, M): if s[i] == t[j]: dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] + 1)%MOD else: dp[i + 1][j + 1] = (dp[i][j+1] + dp[i+1][j] - dp[i][j])%MOD print(((dp[N][M] + 1)%MOD))
p03003
N,M=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) mod=10**9+7 sm=[[ 0 for i in range(M)] for j in range(N)] s1=0 s2=0 if N>M: for i in range(N): if t[0]==s[i]: a=1 else: a=0 s1=s1+a sm[i][0]=s1 if i<M: if s[0]==t[i]: a=1 else: a=0 s2=s2+a sm[0][i]=s2 else: for i in range(M): if s[0]==t[i]: a=1 else: a=0 s2=s2+a sm[0][i]=s2 if i<N: if t[0]==s[i]: a=1 else: a=0 s1=s1+a sm[i][0]=s1 for i in range(1,N): for j in range(1,M): if s[i] != t[j]: sm[i][j]=sm[i-1][j]+sm[i][j-1]-sm[i-1][j-1] else: sm[i][j]=sm[i-1][j]+sm[i][j-1]-sm[i-1][j-1]+sm[i-1][j-1]+1 ans=(sm[N-1][M-1]+1)%mod print(ans)
N,M=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) mod=10**9+7 sm=[[ 0 for i in range(M)] for j in range(N)] s1=0 s2=0 if N>M: for i in range(N): if t[0]==s[i]: a=1 else: a=0 s1=s1+a sm[i][0]=s1 if i<M: if s[0]==t[i]: a=1 else: a=0 s2=s2+a sm[0][i]=s2 else: for i in range(M): if s[0]==t[i]: a=1 else: a=0 s2=s2+a sm[0][i]=s2 if i<N: if t[0]==s[i]: a=1 else: a=0 s1=s1+a sm[i][0]=s1 for i in range(1,N): for j in range(1,M): if s[i] != t[j]: sm[i][j]=(sm[i-1][j]+sm[i][j-1]-sm[i-1][j-1])%mod else: sm[i][j]=(sm[i-1][j]+sm[i][j-1]-sm[i-1][j-1]+sm[i-1][j-1]+1)%mod ans=(sm[N-1][M-1]+1)%mod print(ans)
p03003
def main(): mod = 10**9 + 7 N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) u = [1] * (N + 1) v = [1] * (N + 1) for t in T: for i, s in enumerate(S, 1): if s == t: v[i] = v[i - 1] + u[i] else: v[i] = v[i - 1] + u[i] - u[i - 1] u, v = v, u return u[-1] % mod print((main()))
def main(): mod = 10**9 + 7 N, M = list(map(int, input().split())) S = list(map(int, input().split())) T = list(map(int, input().split())) V = [1] * N for t in T: p, q = 1, 1 for i, (s, v) in enumerate(zip(S, V)): V[i] = q = v + q if s == t else v + q - p p = v return V[-1] % mod print((main()))
p03003
#!/usr/bin/env python3 MOD = 10**9 + 7 n, m = list(map(int, input().split())) s = list(map(int, input().split())) t = list(map(int, input().split())) dp = [[0] * (m + 1) for _ in range(n + 1)] dp[0][0] = 0 for i in range(n): for j in range(m): dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] - dp[i][j] if s[i] == t[j]: dp[i + 1][j + 1] += dp[i][j] + 1 dp[i + 1][j + 1] %= MOD print((dp[n][m] + 1))
#!/usr/bin/env python3 MOD = 10**9 + 7 n, m = list(map(int, input().split())) s = list(map(int, input().split())) t = list(map(int, input().split())) dp = [[0] * (m + 1) for _ in range(n + 1)] for i in range(n + 1): dp[i][0] = 1 for j in range(m + 1): dp[0][j] = 1 for i in range(n): for j in range(m): dp[i + 1][j + 1] += dp[i + 1][j] dp[i + 1][j + 1] += dp[i][j + 1] dp[i + 1][j + 1] += -dp[i][j] if s[i] == t[j]: dp[i + 1][j + 1] += dp[i][j] dp[i + 1][j + 1] %= MOD print((dp[n][m]))
p03003
import copy N, M = list(map(int, input().split())) S_array = list(map(int, input().split())) T_array = list(map(int, input().split())) mod = 10**9 + 7 add_num_array = [0] * N ans_array = [0] * (N + 1) ans_old = [1] * (N + 1) for t in T_array: ans_array[0] = 1 for i, s in enumerate(S_array): if s == t: add_num_array[i] = (add_num_array[i] + ans_old[i]) % mod ans_array[i + 1] = (ans_array[i] + add_num_array[i]) % mod ans_old = copy.deepcopy(ans_array) # print(ans_array) print((ans_array[-1]))
N, M = list(map(int, input().split())) S_array = list(map(int, input().split())) T_array = list(map(int, input().split())) mod = 10**9 + 7 add_num_array = [0] * N ans_array = [0] * (N + 1) ans_old = [1] * (N + 1) for t in T_array: ans_array = [1] * (N+1) for i, s in enumerate(S_array): if s == t: add_num_array[i] = (add_num_array[i] + ans_old[i]) % mod ans_array[i + 1] = (ans_array[i] + add_num_array[i]) % mod ans_old = ans_array print((ans_array[-1]))
p03003
def main(): from collections import defaultdict import bisect n,m = list(map(int,input().split())) s = list(map(int,input().split())) t = list(map(int,input().split())) mod = 10**9+7 p = max(n,m) if n>m: t.extend((0,)*(n-m)) else: s.extend((0,)*(m-n)) dcs = defaultdict(list) dct = defaultdict(list) dp = [[1 for i in range(p+1)] for j in range(p+1)] for i in range(p): if s[i] != 0: dcs[s[i]].append(i) if t[i] != 0: dct[t[i]].append(i) def update(i,j): val = dp[i-1][j-1] x = s[i-1] y = t[j-1] if dct[x]: k = bisect.bisect_left(dct[x],j-1) if k != 0: val += dp[i][dct[x][k-1]+1]-dp[i-1][dct[x][k-1]+1] if dcs[y]: l = bisect.bisect_left(dcs[y],i-1) if l != 0: val += dp[dcs[y][l-1]+1][j]-dp[dcs[y][l-1]+1][j-1] if x == y: val += dp[i-1][j-1] dp[i][j] = val%mod for i in range(1,p+1): for j in range(1,p+1): update(i,j) print((dp[-1][-1])) main()
from collections import defaultdict import bisect n,m = list(map(int,input().split())) s = list(map(int,input().split())) t = list(map(int,input().split())) mod = 10**9+7 p = max(n,m) if n>m: t.extend((0,)*(n-m)) else: s.extend((0,)*(m-n)) dp = [[1 for i in range(p+1)] for j in range(p+1)] for i in range(1,p+1): for j in range(1,p+1): x = s[i-1] y = t[j-1] if x == y: dp[i][j] = (dp[i-1][j]+dp[i][j-1])%mod else: dp[i][j] = (dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1])%mod print((dp[-1][-1]))
p03003
MOD = 10 ** 9 + 7 n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) dp = [[0 for _ in range(m + 1)] for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if a[i - 1] == b[j - 1]: dp[i][j] = 1 + dp[i - 1][j] + dp[i][j - 1] if dp[i][j] >= MOD: dp[i][j] -= MOD else: dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] if dp[i][j] >= MOD: dp[i][j] -= MOD ret = dp[n][m] + 1 if ret >= MOD: ret -= MOD print(ret)
MOD = 10 ** 9 + 7 n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) dp = [[0 for _ in range(m + 1)] for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if a[i - 1] == b[j - 1]: dp[i][j] = (1 + dp[i - 1][j] + dp[i][j - 1]) % MOD else: dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) % MOD print(((dp[n][m] + 1) % MOD))
p03003
import sys sys.setrecursionlimit(10**6+32) mod=10**9+7 input=sys.stdin.buffer.readline n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[ [0 for _ in range(m)] for _ in range(n)] for i in range(0,n): for j in range(0,m): ret=0 if i-1>=0: ret+=dp[i-1][j] if j-1>=0: ret+=dp[i][j-1] if i-1>=0 and j-1>=0: ret-=dp[i-1][j-1] if s[i]==t[j]: ret+=1 if i-1>=0 and j-1>=0: ret+=dp[i-1][j-1] ret%=mod if ret<0: ret+=mod dp[i][j]=ret print(((dp[n-1][m-1]+1)%mod))
import sys mod=10**9+7 input=sys.stdin.buffer.readline n,m=list(map(int,input().split())) s=list(map(int,input().split())) t=list(map(int,input().split())) dp=[ [0 for _ in range(m)] for _ in range(n)] for i in range(0,n): for j in range(0,m): ret=0 if i-1>=0: ret+=dp[i-1][j] if j-1>=0: ret+=dp[i][j-1] if i-1>=0 and j-1>=0: ret-=dp[i-1][j-1] if s[i]==t[j]: ret+=1 if i-1>=0 and j-1>=0: ret+=dp[i-1][j-1] if ret>=mod: ret%=mod if ret<0: ret+=mod dp[i][j]=ret print((dp[n-1][m-1]+1))
p03003
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): N, M = list(map(int, readline().split())) S = list(map(int, readline().split())) T = list(map(int, readline().split())) dp = [[0] * (M + 1) for _ in range(N + 1)] dp[0][0] = 1 for i in range(N + 1): dp[i][0] = 1 for j in range(M + 1): dp[0][j] = 1 for i in range(N): for j in range(M): dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] - dp[i][j]) % MOD if S[i] == T[j]: dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j]) % MOD print((dp[N][M])) return if __name__ == '__main__': main()
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): N, M = list(map(int, readline().split())) S = list(map(int, readline().split())) T = list(map(int, readline().split())) dp = [[0] * (M + 1) for _ in range(N + 1)] for i in range(N + 1): dp[i][0] = 1 for j in range(M + 1): dp[0][j] = 1 for i in range(N): for j in range(M): if S[i] == T[j]: dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j]) % MOD else: dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] - dp[i][j]) % MOD print((dp[N][M])) return if __name__ == '__main__': main()
p03003