description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
k = int(input())
A = [0] + list(input().strip())
x = k - 1
B = []
r = 1
while x >= 0:
B = list(range(r, r + (1 << x))) + B
r += 1 << x
x -= 1
B = [-1] + B
B_INV = [-1] * (1 << k)
for i in range(1, 1 << k):
B_INV[B[i]] = i
C = [-1] * (1 << k)
for i in range(len(A)):
if A[B[i]] == "0":
C[i] = 0
elif A[B[i]] == "1":
C[i] = 1
else:
C[i] = 2
q = int(input())
seg_el = 1 << k
SEG = [1] * (2 * seg_el)
for i in range(seg_el - 1, 0, -1):
if C[i] == 1:
SEG[i] = SEG[i * 2 + 1]
elif C[i] == 0:
SEG[i] = SEG[2 * i]
else:
SEG[i] = SEG[2 * i] + SEG[2 * i + 1]
def update(i, s):
while i != 0:
if C[i] == 1:
SEG[i] = SEG[i * 2 + 1]
elif C[i] == 0:
SEG[i] = SEG[2 * i]
else:
SEG[i] = SEG[2 * i] + SEG[2 * i + 1]
i >>= 1
for queries in range(q):
x, y = input().split()
x = int(x)
if y == "0":
y = 0
elif y == "1":
y = 1
else:
y = 2
C[B_INV[x]] = y
update(B_INV[x], y)
print(SEG[1]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | from sys import stdin, stdout
input = stdin.readline
def f(ind):
if len(tree[ind]) == 0:
if s[ind] == "?":
return 2
else:
return 1
if s[ind] == "?":
return f(tree[ind][0]) + f(tree[ind][1])
if s[ind] == "0":
return f(tree[ind][1])
return f(tree[ind][0])
def f1(ind):
if len(tree[ind]) == 0:
if s[ind] == "?":
dp[ind] = 2
else:
dp[ind] = 1
return dp[ind]
x = f1(tree[ind][0])
y = f1(tree[ind][1])
if s[ind] == "?":
dp[ind] = x + y
return dp[ind]
if s[ind] == "0":
dp[ind] = y
return dp[ind]
dp[ind] = x
return dp[ind]
t = 1
for _ in range(t):
k = int(input())
s1 = input().strip()
n = len(s1)
s1 = s1[::-1]
s = [i for i in s1]
dp = [(-1) for i in range(n)]
tree = [[] for i in range(n)]
par = [(-1) for i in range(n)]
for i in range(n):
if 2 * i + 1 >= n:
continue
tree[i].append(2 * i + 1)
tree[i].append(2 * i + 2)
par[2 * i + 1] = i
par[2 * i + 2] = i
f1(0)
Q = int(input())
for q in range(Q):
p, c = input().split()
p = int(p)
a = n - p
s[a] = c
val = a
while 1:
if val == -1:
stdout.write(str(dp[0]) + "\n")
break
if len(tree[val]) == 0:
if s[val] == "?":
dp[val] = 2
else:
dp[val] = 1
val = par[val]
continue
if s[val] == "?":
dp[val] = dp[tree[val][0]] + dp[tree[val][1]]
elif s[val] == "1":
dp[val] = dp[tree[val][0]]
else:
dp[val] = dp[tree[val][1]]
val = par[val] | ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR STRING RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
from sys import stdin
ANS = []
k = int(stdin.readline())
n = 2**k - 1
s = list(stdin.readline()[:-1])
s.reverse()
dp = [0] * n
for i in range(n - 1, -1, -1):
if 2 * i + 1 >= n:
if s[i] == "?":
dp[i] = 2
else:
dp[i] = 1
else:
if s[i] == "1" or s[i] == "?":
dp[i] += dp[2 * i + 1]
if s[i] == "0" or s[i] == "?":
dp[i] += dp[2 * i + 2]
q = int(stdin.readline())
for loop in range(q):
p, c = stdin.readline()[:-1].split()
p = n - int(p)
s[p] = c
while True:
dp[p] = 0
if 2 * p + 1 >= n:
if s[p] == "?":
dp[p] = 2
else:
dp[p] = 1
else:
if s[p] == "1" or s[p] == "?":
dp[p] += dp[2 * p + 1]
if s[p] == "0" or s[p] == "?":
dp[p] += dp[2 * p + 2]
if p == 0:
break
p = (p - 1) // 2
ANS.append(str(dp[0]))
print("\n".join(ANS)) | IMPORT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
k = int(input())
s = input().strip()
a = ["*"] + list(s[::-1])
dp = [0] * (1 << k)
for i in range((1 << k) - 1, 0, -1):
if 2 * i < 1 << k:
if a[i] == "1":
dp[i] = dp[2 * i]
elif a[i] == "0":
dp[i] = dp[2 * i + 1]
else:
dp[i] = dp[2 * i] + dp[2 * i + 1]
elif a[i] == "?":
dp[i] = 2
else:
dp[i] = 1
def solve():
idx, char = input().split()
idx = int(idx)
idx = (1 << k) - idx
a[idx] = char
cur = idx
while cur:
if 2 * cur >= 1 << k:
if a[cur] == "?":
dp[cur] = 2
else:
dp[cur] = 1
cur //= 2
continue
if a[cur] == "1":
dp[cur] = dp[2 * cur]
elif a[cur] == "0":
dp[cur] = dp[2 * cur + 1]
else:
dp[cur] = dp[2 * cur] + dp[2 * cur + 1]
cur //= 2
cur = 1
ans = 1
while 2 * cur < 1 << k:
if a[cur] == "1":
cur = 2 * cur
elif a[cur] == "0":
cur = 2 * cur + 1
else:
break
print(dp[cur])
for nq in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR IF BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
class segtree:
global s
def __init__(self, init_val, segfunc, k):
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = 0
self.k2 = 2**k
self.k3 = 2 ** (k - 1)
self.num = self.k2
self.tree = [0] * self.num
def update(self, k, x):
self.tree[k] = x
if k < self.k3:
t = s[self.k2 - k - 1]
if t == "?":
self.tree[k] = self.tree[k * 2] + self.tree[k * 2 ^ 1]
elif t == "1":
self.tree[k] = self.tree[min(k * 2, k * 2 ^ 1)]
else:
self.tree[k] = self.tree[max(k * 2, k * 2 ^ 1)]
while k > 1:
t = s[self.k2 - (k >> 1) - 1]
if t == "?":
self.tree[k >> 1] = self.tree[k] + self.tree[k ^ 1]
elif t == "1":
self.tree[k >> 1] = self.tree[min(k, k ^ 1)]
else:
self.tree[k >> 1] = self.tree[max(k, k ^ 1)]
k >>= 1
def get(self, k):
return self.tree[k]
def deb(self):
return self.tree
def segf(x, y):
return x + y
k = int(input())
s = list(input())
q = int(input())
num2 = 2 ** (k - 1)
n = 2**k
seg = segtree([0] * 2**k, segf, k)
for i in range(num2):
if s[i] == "?":
seg.update(n - i - 1, 2)
else:
seg.update(n - i - 1, 1)
for i in range(q):
p, c = input().split()
p = int(p)
s[p - 1] = c
if p <= num2:
if c == "?":
seg.update(n - p, 2)
else:
seg.update(n - p, 1)
seg.update(n - p, seg.get(n - p))
print(seg.get(1)) | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | from sys import stdin
input = stdin.readline
def answer():
for i in range(n, 0, -1):
i = -i
if 2 * -i > n:
if s[i] == "0" or s[i] == "1":
dp[i] = 1
else:
dp[i] = 2
else:
if s[i] == "1":
dp[i] = dp[2 * i]
elif s[i] == "0":
dp[i] = dp[2 * i - 1]
else:
dp[i] = dp[2 * i] + dp[2 * i - 1]
parent[2 * i] = i
parent[2 * i - 1] = i
def update_ans(i):
if 2 * -i > n:
if s[i] == "0" or s[i] == "1":
dp[i] = 1
else:
dp[i] = 2
elif s[i] == "1":
dp[i] = dp[2 * i]
elif s[i] == "0":
dp[i] = dp[2 * i - 1]
else:
dp[i] = dp[2 * i] + dp[2 * i - 1]
while parent[i]:
if 2 * parent[i] == i:
left = i
right = i - 1
else:
left = i + 1
right = i
if s[parent[i]] == "1":
dp[parent[i]] = dp[left]
elif s[parent[i]] == "0":
dp[parent[i]] = dp[right]
else:
dp[parent[i]] = dp[left] + dp[right]
i = parent[i]
k = int(input())
s = list(input()[:-1])
n = len(s)
dp = [0] * (n + 1)
parent = [0] * (n + 1)
answer()
for q in range(int(input())):
ind, ch = input().split()
ind = int(ind)
s[ind - 1] = ch
update_ans(-(n - ind + 1))
print(dp[n]) | ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP NUMBER VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_DEF IF BIN_OP NUMBER VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
n = int(sys.stdin.readline())
string = list(sys.stdin.readline().strip())
arr = list(reversed(string))
length = len(arr)
ans_arr = [(1) for item in range(2 * (length + 1) + 1)]
for i in range(length - 1, -1, -1):
pos = i + 1
if arr[i] == "0":
ans_arr[pos] = ans_arr[2 * pos + 1]
elif arr[i] == "1":
ans_arr[pos] = ans_arr[2 * pos]
else:
ans_arr[pos] = ans_arr[2 * pos] + ans_arr[2 * pos + 1]
q = int(input())
for j in range(q):
change_index, value = map(str, sys.stdin.readline().split())
v_index = len(string) - int(change_index)
arr[v_index] = value
while v_index + 1 > 0:
if arr[v_index] == "0":
ans_arr[v_index + 1] = ans_arr[2 * (v_index + 1) + 1]
elif arr[v_index] == "1":
ans_arr[v_index + 1] = ans_arr[2 * (v_index + 1)]
else:
ans_arr[v_index + 1] = (
ans_arr[2 * (v_index + 1)] + ans_arr[2 * (v_index + 1) + 1]
)
v_index = (v_index - 1) // 2
print(ans_arr[1]) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
def input():
return sys.stdin.readline().strip()
k = int(input())
s = [""] + list(reversed(list(input())))
out = [1] * 2 ** (k + 1)
def update(i):
if s[i] == "1":
out[i] = out[i * 2]
elif s[i] == "0":
out[i] = out[i * 2 + 1]
else:
out[i] = out[i * 2] + out[i * 2 + 1]
for i in range(2**k - 1, 0, -1):
update(i)
ans = []
q = int(input())
for _ in range(q):
i, c = input().split()
i = 2**k - int(i)
if c == s[i]:
ans.append(out[1])
else:
s[i] = c
update(i)
while i > 1 and (s[i // 2] == "?" or i & 1 ^ int(s[i // 2])):
i //= 2
update(i)
ans.append(out[1])
print("\n".join(map(str, ans))) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER STRING BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | def solve(s: str) -> int:
n = len(s)
dp = [-1] * n
for idx in range(n - 1, -1, -1):
if idx * 2 + 1 >= n:
dp[idx] = 2 if s[idx] == "?" else 1
elif s[idx] == "?":
dp[idx] = dp[idx * 2] + dp[idx * 2 + 1]
elif s[idx] == "0":
dp[idx] = dp[idx * 2 + 1]
else:
dp[idx] = dp[idx * 2]
return dp
input()
s = " " + input()[::-1]
dp = solve(s)
s = [i for i in s]
q = []
for _ in range(int(input())):
q.append(input().split())
for idx, c in q:
idx = len(s) - int(idx)
s[idx] = c
while idx:
if idx * 2 + 1 >= len(s):
if s[idx] == "?":
dp[idx] = 2
else:
dp[idx] = 1
elif s[idx] == "?":
dp[idx] = dp[idx * 2] + dp[idx * 2 + 1]
elif s[idx] == "0":
dp[idx] = dp[idx * 2 + 1]
else:
dp[idx] = dp[idx * 2]
idx //= 2
print(dp[1]) | FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR STRING NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
k = int(input())
s = list(input().rstrip())
n = len(s) + 1
x = [0]
p2 = 1
d = dict()
for _ in range(k):
for i in range(2 * p2, p2, -1):
x.append(s[-(i - 1)])
d[-(i - 1) % n] = len(x) - 1
p2 *= 2
cnt = [1] * (2 * n)
for i in range(n - 1, 0, -1):
if x[i] == "?":
cnt[i] = cnt[2 * i] + cnt[2 * i + 1]
else:
cnt[i] = cnt[2 * i + int(x[i])]
q = int(input())
for _ in range(q):
p, c = input().rstrip().split()
u = d[int(p)]
x[u] = c
if c == "?":
cnt[u] = cnt[2 * u] + cnt[2 * u + 1]
else:
cnt[u] = cnt[2 * u + int(c)]
while u ^ 1:
u //= 2
if x[u] == "?":
cnt[u] = cnt[2 * u] + cnt[2 * u + 1]
else:
cnt[u] = cnt[2 * u + int(x[u])]
ans = cnt[1]
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
k = int(input())
s = list(input())[::-1]
n = 2**k - 1
dp = [0] * n
for i in range(n - 1, -1, -1):
x = 2 * i + 1
y = 2 * i + 2
if x < n and y < n:
if s[i] == "1":
dp[i] = dp[x]
if s[i] == "0":
dp[i] = dp[y]
if s[i] == "?":
dp[i] = dp[x] + dp[y]
elif s[i] == "?":
dp[i] = 2
else:
dp[i] = 1
q = int(input())
for q in range(q):
x, y = sys.stdin.readline().split()
a = int(x)
b = str(y)
ind = n - a
if s[ind] == b:
print(dp[0])
continue
s[ind] = b
f = 0
while f != 1:
x = 2 * ind + 1
y = 2 * ind + 2
if x < n and y < n:
if s[ind] == "1":
dp[ind] = dp[x]
elif s[ind] == "0":
dp[ind] = dp[y]
else:
dp[ind] = dp[x] + dp[y]
elif s[ind] == "?":
dp[ind] = 2
else:
dp[ind] = 1
if ind == 0:
f = 1
ind = ind // 2 + ind % 2 - 1
sys.stdout.write(str(dp[0]) + "\n") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
zz = 1
sys.setrecursionlimit(10**5)
if zz:
input = sys.stdin.readline
else:
sys.stdin = open("input.txt", "r")
sys.stdout = open("all.txt", "w")
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]
def fori(n):
return [fi() for i in range(n)]
def inc(d, c, x=1):
d[c] = d[c] + x if c in d else x
def ii():
return input().rstrip()
def li():
return [int(xx) for xx in input().split()]
def fli():
return [float(x) for x in input().split()]
def dadd(d, p, val):
if p in d:
d[p].append(val)
else:
d[p] = [val]
def gi():
return [xx for xx in input().split()]
def gtc(tc, *ans):
print("Case #" + str(tc) + ":", *ans)
def cil(n, m):
return n // m + int(n % m > 0)
def fi():
return int(input())
def pro(a):
return reduce(lambda a, b: a * b, a)
def swap(a, i, j):
a[i], a[j] = a[j], a[i]
def bits(i, n):
p = bin(i)[2:]
return (n - len(p)) * "0" + p
def prec(a, pre):
for i in a:
pre.append(pre[-1] + i)
pre.pop(0)
def YN(flag):
print("YES" if flag else "NO")
def si():
return list(input().rstrip())
def mi():
return map(int, input().split())
def gh():
sys.stdout.flush()
def isvalid(i, j, n, m):
return 0 <= i < n and 0 <= j < m
def bo(i):
return ord(i) - ord("a")
def graph(n, m):
for i in range(m):
x, y = mi()
a[x].append(y)
a[y].append(x)
t = 1
INF = 10**18
uu = t
mod = 10**9 + 7
def f(sm, lg, v):
if v == "0":
return sm
elif v == "1":
return lg
else:
return sm + lg
while t > 0:
t -= 1
n = fi()
s = si()
q = fi()
c = 0
dp = [([1] * (2**n + 1)) for i in range(n + 1)]
for i in range(n - 1, -1, -1):
for j in range(2**i):
dp[i][j] = f(dp[i + 1][2 * j], dp[i + 1][2 * j + 1], s[c + j])
c += 2**i
for _ in range(q):
ind, v = input().split()
ind = int(ind)
s[ind - 1] = v
for i in range(n - 1, -1, -1):
if 2**i >= ind:
break
ind -= 2**i
j = ind - 1
c = 2**n - 2 ** (i + 1)
while i >= 0:
dp[i][j] = f(dp[i + 1][2 * j], dp[i + 1][2 * j + 1], s[c + j])
c += 2**i
i -= 1
j //= 2
print(dp[0][0]) | IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR STRING STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR STRING RETURN VAR IF VAR STRING RETURN VAR RETURN BIN_OP VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
k = int(input())
n = 1 << k
s = list(input()[:-1][::-1])
d = [1] * (n * 2)
for i in range(n - 2, -1, -1):
if s[i] == "0":
d[i] = d[i * 2 + 2]
elif s[i] == "1":
d[i] = d[i * 2 + 1]
else:
d[i] = d[i * 2 + 1] + d[i * 2 + 2]
q = int(input())
for _ in range(q):
a, c = input().split()
a = n - 1 - int(a)
s[a] = c
if c == "0":
d[a] = d[a * 2 + 2]
elif c == "1":
d[a] = d[a * 2 + 1]
else:
d[a] = d[a * 2 + 1] + d[a * 2 + 2]
while a > 0:
a = (a - 1) // 2
if s[a] == "0":
d[a] = d[a * 2 + 2]
elif s[a] == "1":
d[a] = d[a * 2 + 1]
else:
d[a] = d[a * 2 + 1] + d[a * 2 + 2]
print(d[0]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
k = int(input())
s = input()[:-1]
poss = [0] * (k + 1)
slist = [0] * (k + 1)
cur = 0
for i in range(k + 1):
poss[i] = [1] * 2 ** (k - i)
slist[i] = [1] * 2 ** (k - i)
if i:
for j in range(2 ** (k - i)):
char = s[cur]
slist[i][j] = char
if char == "?":
poss[i][j] = poss[i - 1][2 * j] + poss[i - 1][2 * j + 1]
if char == "0":
poss[i][j] = poss[i - 1][2 * j]
if char == "1":
poss[i][j] = poss[i - 1][2 * j + 1]
cur += 1
for f in range(int(input())):
x, y = input().split()
x = int(x) - 1
lv = 1
while x >= 2 ** (k - lv):
x -= 2 ** (k - lv)
lv += 1
pos = x
slist[lv][pos] = y
while lv <= k:
char = slist[lv][pos]
if char == "?":
poss[lv][pos] = poss[lv - 1][2 * pos] + poss[lv - 1][2 * pos + 1]
if char == "0":
poss[lv][pos] = poss[lv - 1][2 * pos]
if char == "1":
poss[lv][pos] = poss[lv - 1][2 * pos + 1]
lv += 1
pos //= 2
print(poss[-1][0]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP NUMBER BIN_OP VAR VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
maxN = 2**18 + 5
t = [0] * maxN
def build(a, n):
for v in range(n, 0, -1):
if v * 2 > n:
if a[v] == "?":
t[v] = 2
else:
t[v] = 1
elif a[v] == "?":
t[v] = t[v * 2] + t[v * 2 + 1]
elif a[v] == "0":
t[v] = t[v * 2 + 1]
else:
t[v] = t[v * 2]
def query():
return t[1]
def update(a, v, n):
while v > 0:
if v * 2 > n:
if a[v] == "?":
t[v] = 2
else:
t[v] = 1
elif a[v] == "?":
t[v] = t[v * 2] + t[v * 2 + 1]
elif a[v] == "0":
t[v] = t[v * 2 + 1]
else:
t[v] = t[v * 2]
v //= 2
def main():
k = int(input())
a = list(input())
n = 2**k - 1
a.reverse()
a = [None] + a
build(a, n)
allans = []
q = int(input())
for _ in range(q):
idx, c = input().split()
idx = int(idx)
idx = n - idx + 1
a[idx] = c
update(a, idx, n)
allans.append(query())
multiLineArrayPrint(allans)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultValFactory, dimensionArr):
dv = defaultValFactory
da = dimensionArr
if len(da) == 1:
return [dv() for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(i, j):
print("? {} {}".format(i, j))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(" ".join([str(x) for x in ans])))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main() | IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR NUMBER FUNC_DEF WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = lambda: sys.stdin.readline().rstrip()
K = int(input())
S = list(input())
G = 2**K - 1
W = [(2 if c == "?" else 1) for c in S[: 2 ** (K - 1)]]
W += [0] * (G - len(W))
start = 0
for k in range(K - 2, -1, -1):
nstart = start + 2 ** (k + 1)
for i in range(2**k):
a = W[start + 2 * i]
b = W[start + 2 * i + 1]
p = nstart + i
c = S[p]
if c in "0?":
W[p] += a
if c in "1?":
W[p] += b
start = nstart
O = []
for _ in range(int(input())):
ps, c = input().split()
p = int(ps) - 1
S[p] = c
nstart = 0
for k in range(K - 1, -1, -1):
games = 2**k
if p < games:
if k == K - 1:
W[p] = 2 if S[p] == "?" else 1
else:
a = W[start + 2 * p]
b = W[start + 2 * p + 1]
W[nstart + p] = 0
c = S[nstart + p]
if c in "0?":
W[nstart + p] += a
if c in "1?":
W[nstart + p] += b
p //= 2
else:
p -= games
start = nstart
nstart += games
O.append(W[-1])
print("\n".join(map(str, O))) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR VAR VAR IF VAR STRING VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR STRING VAR BIN_OP VAR VAR VAR IF VAR STRING VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
K = int(input())
N = 1 << K
Conv = list(range(N - 1))
i = 0
for j in range(K):
j = 1 << j
Conv[i : i + j] = Conv[i : i + j][::-1]
i += j
Conv.reverse()
func = lambda x: 0 if x == "0" else 1 if x == "1" else 2
Tmp = input()
S = [0] * (N - 1)
for i in range(N - 1):
S[Conv[i]] = func(Tmp[i])
A = [0] * (N * 2)
def make(i):
if i >= N - 1:
A[i] = 1
elif S[i] == 0:
A[i] = make(i * 2 + 1)
make(i * 2 + 2)
elif S[i] == 1:
make(i * 2 + 1)
A[i] = make(i * 2 + 2)
else:
A[i] = make(i * 2 + 1) + make(i * 2 + 2)
return A[i]
make(0)
func2 = lambda x, y: (Conv[int(x) - 1], 0 if y == "0" else 1 if y == "1" else 2)
Q = int(input())
for _ in range(Q):
i, s = func2(*input().split())
S[i] = s
while True:
if S[i] == 0:
A[i] = A[i * 2 + 1]
elif S[i] == 1:
A[i] = A[i * 2 + 2]
else:
A[i] = A[i * 2 + 1] + A[i * 2 + 2]
if i == 0:
break
i = (i - 1) // 2
print(A[0]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING NUMBER VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR WHILE NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
n = int(input())
a = list(input())[::-1]
n = len(a)
ans = [0] * (4 * n)
for i in range(n, 4 * n):
ans[i] = 1
for i in range(n - 1, -1, -1):
left = 2 * i + 1
right = 2 * i + 2
if a[i] == "0":
ans[i] = ans[right]
if a[i] == "1":
ans[i] = ans[left]
if a[i] == "?":
ans[i] = ans[left] + ans[right]
def fill(i, val):
a[i] = val
while True:
left = 2 * i + 1
right = 2 * i + 2
if a[i] == "0":
ans[i] = ans[right]
if a[i] == "1":
ans[i] = ans[left]
if a[i] == "?":
ans[i] = ans[left] + ans[right]
if i == 0:
break
i = (i - 1) // 2
for q in range(int(input())):
ind, val = input().split()
fill(n - int(ind), val)
print(ans[0]) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
def solution(k, res, q, queries):
bij = [(-1) for _ in range(2**k)]
count = 1
end = 2**k
for p in range(0, k):
beg = end - 2**p
for orig in range(beg, end):
bij[orig] = count
count += 1
end = beg
winners = ["1" for _ in range(2**k)]
for pos_them in range(1, 2**k):
val = res[pos_them - 1]
pos_moi = bij[pos_them]
winners[pos_moi] = val
nb_gagnants = [(1) for _ in range(2**k)]
for pos_me in range(2**k - 1, -1, -1):
if pos_me >= 2 ** (k - 1):
if winners[pos_me] == "?":
nb_gagnants[pos_me] = 2
else:
nb_gagnants[pos_me] = 1
elif winners[pos_me] == "?":
nb_gagnants[pos_me] = nb_gagnants[2 * pos_me] + nb_gagnants[2 * pos_me + 1]
elif winners[pos_me] == "1":
nb_gagnants[pos_me] = nb_gagnants[2 * pos_me + 1]
else:
nb_gagnants[pos_me] = nb_gagnants[2 * pos_me]
for [pos_them, elem] in queries:
pos_them = int(pos_them)
pos_me = bij[pos_them]
winners[pos_me] = elem
while pos_me > 0:
if pos_me >= 2 ** (k - 1):
if winners[pos_me] == "?":
nb_gagnants[pos_me] = 2
else:
nb_gagnants[pos_me] = 1
elif winners[pos_me] == "?":
nb_gagnants[pos_me] = (
nb_gagnants[2 * pos_me] + nb_gagnants[2 * pos_me + 1]
)
elif winners[pos_me] == "1":
nb_gagnants[pos_me] = nb_gagnants[2 * pos_me + 1]
else:
nb_gagnants[pos_me] = nb_gagnants[2 * pos_me]
pos_me = pos_me // 2
print(nb_gagnants[1])
k = int(input())
res = input().strip()
q = int(input())
queries = []
for t in range(q):
queries.append(input().split())
solution(k, res, q, queries) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR FOR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
def conv(p):
return (1 << k) - p
k = int(input())
s = [0] + [i for i in input().strip()[::-1]]
can_win = [0] * len(s)
st = [1]
vis = [0] * len(s)
while st:
c = st[-1]
if not vis[c]:
vis[c] = 1
if 2 * c + 1 < len(s):
st.append(2 * c)
st.append(2 * c + 1)
else:
if 2 * c + 1 >= len(s):
can_win[c] = 1 + (s[c] == "?")
elif s[c] == "?":
can_win[c] = can_win[2 * c] + can_win[2 * c + 1]
else:
can_win[c] = can_win[2 * c + (s[c] == "0")]
st.pop()
q = int(input())
for i in range(q):
a = input().split()
p = conv(int(a[0]))
c = a[1]
s[p] = c
c = p
while c:
if 2 * c + 1 >= len(s):
can_win[c] = 1 + (s[c] == "?")
elif s[c] == "?":
can_win[c] = can_win[2 * c] + can_win[2 * c + 1]
else:
can_win[c] = can_win[2 * c + (s[c] == "0")]
c >>= 1
print(can_win[1]) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | def get_total(total, char, left, right, idx):
if left[idx] == -1:
if char[idx] == "?":
return 2
else:
return 1
elif char[idx] == "?":
return total[left[idx]] + total[right[idx]]
elif char[idx] == "0":
return total[left[idx]]
else:
return total[right[idx]]
def main():
k = int(input())
s = list(input())
q = int(input())
l = []
for i in range(q):
p, c = input().split()
l.append((int(p), c))
left = []
right = []
parent = []
total = []
d = 2**k
z = z2 = 0
for i in range(k):
d //= 2
for j in range(d):
parent.append(-1)
if i:
left.append(z2)
right.append(z2 + 1)
parent[z2] = parent[z2 + 1] = z
z2 += 2
else:
left.append(-1)
right.append(-1)
total.append(get_total(total, s, left, right, z))
z += 1
for i in range(q):
idx = l[i][0] - 1
s[idx] = l[i][1]
total[idx] = get_total(total, s, left, right, idx)
while parent[idx] != -1:
idx = parent[idx]
total[idx] = get_total(total, s, left, right, idx)
print(total[-1])
main() | FUNC_DEF IF VAR VAR NUMBER IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR STRING RETURN BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR STRING RETURN VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | def tree(a, k, q):
ans = [0] * pow(2, k + 1)
a = a + ["l"]
a = a[::-1]
for i in range(1 << k, 1 << k + 1):
ans[i] = 1
for i in range((1 << k) - 1, 0, -1):
if a[i] == "0":
ans[i] += ans[2 * i + 1]
elif a[i] == "1":
ans[i] += ans[2 * i]
else:
ans[i] = ans[2 * i] + ans[2 * i + 1]
for p, ch in q:
p = (1 << k) - p
a[p] = ch
while p:
if a[p] == "0":
ans[p] = ans[2 * p + 1]
elif a[p] == "1":
ans[p] = ans[2 * p]
else:
ans[p] = ans[2 * p] + ans[2 * p + 1]
p //= 2
print(ans[1])
k = int(input())
a = list(input())
q = []
for i in range(int(input())):
x, y = map(str, input().strip().split())
x = int(x)
q.append((x, y))
tree(a, k, q) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR WHILE VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | import sys
input = sys.stdin.readline
def im():
return map(int, input().split())
def ii():
return int(input())
def il():
return list(map(int, input().split()))
def ins():
return input()[:-1]
k = ii()
lis = list(ins())
lis.reverse()
n = len(lis)
ldp = [1] * n
rdp = [1] * n
for i in range(n - 1, 0, -1):
parent = (i - 1) // 2
if lis[i] == "?":
if i == parent * 2 + 1:
ldp[parent] = ldp[i] + rdp[i]
else:
rdp[parent] = ldp[i] + rdp[i]
elif lis[i] == "1":
if i == parent * 2 + 1:
ldp[parent] = ldp[i]
else:
rdp[parent] = ldp[i]
elif i == parent * 2 + 1:
ldp[parent] = rdp[i]
else:
rdp[parent] = rdp[i]
for _ in range(ii()):
i, c = map(str, input().split())
i = n - int(i)
lis[i] = c
while i != 0:
parent = (i - 1) // 2
if lis[i] == "?":
if i == parent * 2 + 1:
ldp[parent] = ldp[i] + rdp[i]
else:
rdp[parent] = ldp[i] + rdp[i]
elif lis[i] == "1":
if i == parent * 2 + 1:
ldp[parent] = ldp[i]
else:
rdp[parent] = ldp[i]
elif i == parent * 2 + 1:
ldp[parent] = rdp[i]
else:
rdp[parent] = rdp[i]
i = (i - 1) // 2
if lis[0] == "?":
print(ldp[0] + rdp[0])
elif lis[0] == "0":
print(rdp[0])
else:
print(ldp[0]) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR STRING IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR STRING IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.
For example, this picture describes the chronological order of games with k = 3:
<image>
Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:
* if s_i is 0, then the team with lower index wins the i-th game;
* if s_i is 1, then the team with greater index wins the i-th game;
* if s_i is ?, then the result of the i-th game is unknown (any team could win this game).
Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.
You are given the initial state of the string s. You have to process q queries of the following form:
* p c β replace s_p with character c, and print f(s) as the result of the query.
Input
The first line contains one integer k (1 β€ k β€ 18).
The second line contains a string consisting of 2^k - 1 characters β the initial state of the string s. Each character is either ?, 0, or 1.
The third line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow, the i-th line contains an integer p and a character c (1 β€ p β€ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.
Output
For each query, print one integer β f(s).
Example
Input
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
Output
1
2
3
3
5
4 | from sys import gettrace, stdin
if gettrace():
def inputi():
return input()
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def main():
k = int(input())
ss = [" "] + list(input())
q = int(input())
qr = []
for _ in range(q):
p, c = input().split()
qr.append((int(p), c))
n = len(ss) - 1
vals = [0] * (n + 1)
for i, c in enumerate(ss[1:], 1):
if i <= 2 ** (k - 1):
vals[i] = 2 if c == "?" else 1
else:
hi = vals[i * 2 - n - 1]
lo = vals[i * 2 - n - 2]
vals[i] = hi if c == "1" else lo if c == "0" else hi + lo
for i, c in qr:
ss[i] = c
while True:
if i <= 2 ** (k - 1):
vals[i] = 2 if ss[i] == "?" else 1
else:
hi = vals[i * 2 - n - 1]
lo = vals[i * 2 - n - 2]
vals[i] = hi if ss[i] == "1" else lo if ss[i] == "0" else hi + lo
if i == n:
break
i = (n + i) // 2 + 1
print(vals[n])
main() | IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR WHILE NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR STRING VAR VAR VAR STRING VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
maxi = -float("inf")
p = 0
for i in range(n):
if arr[i] > 0:
continue
p += 1
c = arr.copy()
k = i
r = c[0]
for j in range(1, n):
if j == k:
continue
r = max(0, r + arr[j], arr[j])
maxi = max(r, maxi)
if maxi == -float("inf"):
maxi = sum(arr)
elif p == n or maxi < max(arr):
maxi = max(arr)
return maxi | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
sm = nm = gm = -10000
for i in arr:
sm = max(sm + i, nm)
nm = max(nm + i, i)
gm = max(gm, sm, nm)
return gm | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
m = arr[0]
for i in range(n):
s = 0
neg = arr[i]
for j in range(i, n):
if arr[j] < 0:
if neg < 0:
s += max(neg, arr[j])
neg = min(neg, arr[j])
else:
s += arr[j]
m = max(s, m)
return m | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
Maxsum = -1001
cond = 0
for i in arr:
if i < 0:
cond += 1
if cond == n:
return max(arr)
for i in range(0, n):
for j in range(0, n - i):
Maxsum = max(
Maxsum,
sum(arr[j : j + i + 1]) - min(arr[j : j + i + 1]),
sum(arr[j : j + i + 1]),
)
return Maxsum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
fw = [None] * n
bw = [None] * n
if n == 1:
return arr[0]
bw[0] = arr[0]
for i in range(1, n):
bw[i] = max(arr[i], arr[i] + bw[i - 1])
fw[n - 1] = arr[n - 1]
for i in reversed(range(n - 1)):
fw[i] = max(arr[i], arr[i] + fw[i + 1])
fans = max(bw)
for i in range(1, n - 1):
fans = max(fans, fw[i + 1] + bw[i - 1])
return fans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
dpf = [(0) for _ in range(n)]
dpf[0] = arr[0]
arrb = arr[::-1]
msf = arr[0]
dpb = [(0) for _ in range(n)]
dpb[0] = arrb[0]
for i in range(1, n):
if arr[i] + dpf[i - 1] > arr[i]:
dpf[i] = arr[i] + dpf[i - 1]
else:
dpf[i] = arr[i]
msf = max(dpf[i], msf)
for i in range(1, n):
if arrb[i] + dpb[i - 1] > arrb[i]:
dpb[i] = arrb[i] + dpb[i - 1]
else:
dpb[i] = arrb[i]
msf = max(dpb[i], msf)
for i in range(1, n - 1):
msf = max(msf, dpf[i - 1] + dpb[n - 2 - i])
return msf | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
fk = [0] * n
bk = [0] * n
temp = 0
for i in range(n):
temp = max(temp + arr[i], arr[i])
fk[i] = temp
temp = 0
ans = -(10**9)
for i in range(n - 1, -1, -1):
temp = max(temp + arr[i], arr[i])
ans = max(ans, temp)
bk[i] = temp
for i in range(1, n - 1):
a = fk[i - 1] if i > 0 else 0
b = bk[i + 1] if i < n - 1 else 0
sm = a + b
ans = max(ans, sm)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
fw = [(0) for i in range(n)]
bw = [(0) for i in range(n)]
curmax = arr[0]
maxsofar = arr[0]
fw[0] = curmax
for i in range(1, n):
curmax = max(arr[i], curmax + arr[i])
maxsofar = max(maxsofar, curmax)
fw[i] = curmax
curmax = maxsofar = bw[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
curmax = max(arr[i], curmax + arr[i])
maxsofar = max(maxsofar, curmax)
bw[i] = curmax
final = maxsofar
for i in range(1, n - 1):
final = max(final, fw[i - 1] + bw[i + 1])
return final | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
max_ending_here = [0] * n
max_ending_here[0] = arr[0]
for i in range(1, n):
max_ending_here[i] = max(arr[i], max_ending_here[i - 1] + arr[i])
max_starting_here = [0] * n
max_starting_here[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
max_starting_here[i] = max(arr[i], max_starting_here[i + 1] + arr[i])
max_sum = max(max_ending_here)
for i in range(1, n - 1):
max_sum = max(max_sum, max_ending_here[i - 1] + max_starting_here[i + 1])
return max_sum | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
lis = list()
Sum = -(2**63)
boo = False
cnt = 0
for i in arr:
if i < 0:
cnt += 1
if cnt == n:
return max(arr)
for i in range(n + 1):
s = list()
for j in range(i, n):
s.append(arr[j])
cur_sum = sum(s)
Min = 2**63
for ele in s:
if ele < 0:
Min = min(Min, ele)
if Min < 0:
cur_sum += abs(Min)
Sum = max(Sum, cur_sum)
return Sum | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
if n == 1:
return max(0, arr[0])
inf = -float("inf")
l = [[inf, inf]] * n
csum = arr[0]
l[1] = [csum, csum]
ma = csum
for i in range(2, n):
csum = max(csum + arr[i - 1], arr[i - 1])
ma = max(csum, ma)
l[i] = [csum, ma]
r = [[inf, inf]] * n
csum = arr[-1]
r[-2] = [csum, csum]
ma = csum
for i in range(n - 3, -1, -1):
csum = max(csum + arr[i + 1], arr[i + 1])
ma = max(csum, ma)
r[i] = [csum, ma]
m = -float("inf")
for i in range(n):
m = max(m, l[i][0] + r[i][0], l[i][1], r[i][1])
csum = arr[0]
ma = csum
for i in range(1, n):
csum = max(csum + arr[i], arr[i])
ma = max(ma, csum)
return max(m, ma)
if __name__ == "__main__":
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().strip().split()))
print(Solution().maxSumSubarray(arr, n)) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST LIST VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER LIST VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST LIST VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER LIST VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
left = [0] * n
right = [0] * n
right[-1] = arr[-1]
left[0] = arr[0]
ans = left[0]
for i in range(1, n):
left[i] = max(left[i - 1] + arr[i], arr[i])
ans = max(ans, left[i])
right[n - 1 - i] = max(right[n - i] + arr[n - 1 - i], arr[n - 1 - i])
ans = max(ans, right[1])
for i in range(1, n - 1):
ans = max(ans, left[i - 1] + right[i + 1])
ans = max(ans, left[n - 2])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, A, n):
max_here = A[0]
pref = [0] * n
pref[0] = A[0]
for i in range(1, n):
max_here = max(A[i], max_here + A[i])
pref[i] = max_here
max_here = A[-1]
suff = [0] * n
suff[-1] = A[-1]
max_so_far = A[-1]
for i in range(n - 2, -1, -1):
max_here = max(A[i], max_here + A[i])
max_so_far = max(max_so_far, max_here)
suff[i] = max_here
ans = max_so_far
for i in range(1, n - 1):
ans = max(ans, pref[i - 1] + suff[i + 1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
dpf = [0] * n
dpr = [0] * n
csum = arr[0]
gsum = arr[0]
dpf[0] = arr[0]
for i in range(1, n):
if csum >= 0:
csum += arr[i]
else:
csum = arr[i]
dpf[i] = csum
gsum = max(gsum, csum)
gsum = max(gsum, arr[n - 1])
csum = arr[n - 1]
dpr[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
if csum >= 0:
csum += arr[i]
else:
csum = arr[i]
dpr[i] = csum
gsum = max(gsum, csum)
ans = gsum
for i in range(1, n - 1):
ans = max(dpf[i - 1] + dpr[i + 1], ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
ans = [[max(arr[0], 0), 0]]
t = max(0, ans[0][0], ans[0][1])
for i in range(1, len(arr)):
p = max(0, ans[-1][0] + arr[i], arr[i])
q = max(0, ans[-1][1] + arr[i], ans[-1][0])
t = max(t, p, q)
ans.append([p, q])
if t == 0 and t not in arr:
return max(arr)
return t | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
if n == 1:
return arr[0]
dp = [[(0) for _ in range(2)] for _ in range(n)]
dp[0][0] = arr[0]
dp[0][1] = float("-inf")
res = max(dp[0][0], dp[0][1])
for i in range(1, n):
dp[i][0] = max(dp[i - 1][0] + arr[i], arr[i])
dp[i][1] = max(dp[i - 1][1] + arr[i], arr[i], dp[i - 1][0])
res = max(res, dp[i][0], dp[i][1])
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
prenotd = arr[0]
predel = 0
maxn = arr[0]
for i in range(1, n):
predel = max(prenotd, predel + arr[i])
prenotd = max(arr[i], prenotd + arr[i])
cur = max(prenotd, predel)
maxn = max(cur, maxn)
return maxn | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
lsum = [0] * n
rsum = [0] * n
maxSum = arr[0]
maxSubArraySum = arr[0]
lsum[0] = arr[0]
for i in range(1, n):
maxSubArraySum = max(maxSubArraySum + arr[i], arr[i])
lsum[i] = maxSubArraySum
maxSum = max(maxSum, lsum[i])
rsum[n - 1] = arr[n - 1]
maxSubArraySum = arr[n - 1]
for j in range(n - 2, -1, -1):
maxSubArraySum = max(maxSubArraySum + arr[j], arr[j])
rsum[j] = maxSubArraySum
maxVal = float("-inf")
for i in range(n):
fw = 0
bw = 0
if i - 1 >= 0:
fw = lsum[i - 1]
if i + 1 < n:
bw = rsum[i + 1]
maxVal = max(fw + bw, maxVal)
return max(maxVal, maxSum) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given array A of size n. You need to find the maximum-sum sub-array with the condition that you are allowed to skip at most one element.
Example 1:
Input:
n = 5
A[] = {1,2,3,-4,5}
Output: 11
Explanation: We can get maximum sum
subarray by skipping -4.
Example 2:
Input:
n = 8
A[] = {-2,-3,4,-1,-2,1,5,-3}
Output: 9
Explanation: We can get maximum sum
subarray by skipping -2 as [4,-1,1,5]
sums to 9, which is the maximum
achievable sum.
Your Task:
Your task is to complete the function maxSumSubarray that take array and size as parameters and returns the maximum sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= n <= 100
-10^{3} <= A_{i}<= 10^{3} | class Solution:
def maxSumSubarray(self, arr, n):
cmax = arr[0]
mmax = arr[0]
fw = [0] * n
fw[0] = arr[0]
bw = [0] * n
bw[-1] = arr[-1]
for i in range(1, n):
cmax = max(cmax + arr[i], arr[i])
fw[i] = cmax
mmax = max(mmax, cmax)
cmax = arr[-1]
mmax = arr[-1]
for i in range(n - 2, -1, -1):
cmax = max(cmax + arr[i], arr[i])
bw[i] = cmax
mmax = max(mmax, cmax)
for i in range(1, n - 1):
mmax = max(mmax, fw[i - 1] + bw[i + 1])
mmax = max(mmax, fw[n - 2], bw[1])
return mmax | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
m = [[1, 1], [1, 0]]
t = [[1, 1], [1, 0]]
def multiply(m1, m2):
MOD = 10**9 + 7
n = len(m1)
w = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0]
x = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1]
y = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0]
z = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1]
m1[0][0], m1[0][1], m1[1][0], m1[1][1] = w % MOD, x % MOD, y % MOD, z % MOD
def power(n):
MOD = 10**9 + 7
if n == 1 or n == 0:
return 2
power(n // 2)
multiply(m, m)
if n & 1:
multiply(m, t)
return (m[0][0] + m[0][1]) % MOD
if n <= 1:
return 1
return power(n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | def multiply(a, b):
mul = [[(0) for x in range(2)] for y in range(2)]
for i in range(2):
for j in range(2):
mul[i][j] = 0
for k in range(2):
mul[i][j] += a[i][k] * b[k][j] % 1000000007
for i in range(2):
for j in range(2):
a[i][j] = mul[i][j]
return a
def power(F, n):
M = [[1, 1], [1, 0]]
if n == 1:
return (F[0][0] + F[0][1]) % 1000000007
elif n < 1:
return n + 1
power(F, int(n / 2))
F = multiply(F, F)
if n % 2 != 0:
F = multiply(F, M)
return (F[0][0] + F[0][1]) % 1000000007
class Solution:
def FindNthTerm(self, m):
F = [[1, 1], [1, 0]]
return power(F, m - 1) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def multiply(self, a, b):
mod = 10**9 + 7
mul = [[(0) for x in range(3)] for y in range(3)]
for i in range(3):
for j in range(3):
mul[i][j] = 0
for k in range(3):
mul[i][j] = (mul[i][j] + a[i][k] * b[k][j] % mod) % mod
for i in range(3):
for j in range(3):
a[i][j] = mul[i][j]
return a
def power(self, F, n):
mod = 10**9 + 7
M = [[1, 1, 0], [1, 0, 0], [0, 1, 0]]
if n == 1:
return (F[0][0] * 2 + (F[0][1] + F[0][2]) % mod) % mod
self.power(F, int(n / 2))
F = self.multiply(F, F)
if n % 2 != 0:
F = self.multiply(F, M)
return (F[0][0] * 2 + (F[0][1] + F[0][2]) % mod) % mod
def FindNthTerm(self, n):
if n == 1 or n == 2:
return n
F = [[1, 1, 0], [1, 0, 0], [0, 1, 0]]
try:
return self.power(F, n - 2)
except Exception as e:
print(e) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
mod = 1000000007
def f(dic, n):
if n == 0:
return 0
if n == 1 or n == 2:
return 1
if n in dic:
return dic[n]
t = -1
if n % 2 == 0:
k = n // 2
t = f(dic, k) * (2 * f(dic, k - 1) + f(dic, k))
else:
k = (n + 1) // 2
t = f(dic, k) * f(dic, k) + f(dic, k - 1) * f(dic, k - 1)
t = t % mod
dic[n] = t
return t
dic = dict()
return f(dic, n + 1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
FIBMAT = [[0, 1], [1, 1]]
ID2 = [[1, 0], [0, 1]]
MOD = 10**9 + 7
@staticmethod
def matmul(m1, m2, MOD=MOD):
return [
[
(sum(m1[i][k] * m2[k][j] for k in range(len(m2))) % MOD)
for j in range(len(m2[1]))
]
for i in range(len(m1))
]
@staticmethod
def matpow(base, exp):
bpow = base
bexp = 1
result = Solution.ID2
while bexp <= exp:
if exp & bexp:
result = Solution.matmul(result, bpow)
bpow = Solution.matmul(bpow, bpow)
bexp += bexp
return result
def FindNthTerm(self, n):
mat = Solution.matpow(Solution.FIBMAT, n)
return sum(mat[0]) % Solution.MOD
if __name__ == "__main__":
T = int(input())
for i in range(T):
n = int(input())
ob = Solution()
ans = ob.FindNthTerm(n)
print(ans) | CLASS_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def matmul(self, a, b, mod):
c = [[(0) for j in range(len(b[0]))] for i in range(len(a))]
for i in range(len(a)):
for j in range(len(b[0])):
for k in range(len(b)):
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod
return c
def matexp(self, a, n, mod):
r = [[1, 0], [0, 1]]
while n:
if n & 1:
r = self.matmul(r, a, mod)
a = self.matmul(a, a, mod)
n >>= 1
return r
def FindNthTerm(self, n):
mod = 10**9 + 7
a = [[1, 1], [1, 0]]
result = self.matexp(a, n - 1, mod)
return (result[0][0] + result[0][1]) % mod | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | MOD = 1000000007
class Solution:
def FindNthTerm(self, n):
def FastDoubling(n):
if n == 0:
return [0, 1]
res = FastDoubling(n // 2)
c = (2 * res[0] * res[1] % MOD - res[0] * res[0] % MOD + MOD) % MOD
d = (res[0] * res[0] % MOD + res[1] * res[1] % MOD) % MOD
if n & 1:
return [d, (c + d) % MOD]
else:
return [c, d]
return FastDoubling(n + 1)[0]
return res[0] | ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER RETURN LIST VAR BIN_OP BIN_OP VAR VAR VAR RETURN LIST VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
if n == 1:
return 1
power = n
M = [[1, 1], [1, 0]]
res = [[1, 1], [1, 0]]
res = self.power(res, power)
return res[0][0]
def power(self, res, power):
if power == 1:
return [[1, 1], [1, 0]]
temp = self.power(res, power // 2)
temp2 = self.mul(temp, temp)
if power & 1:
temp2 = self.mul(temp2, [[1, 1], [1, 0]])
return temp2
def mul(self, t1, t2):
mod = 1000000007
res = [[1, 1], [1, 0]]
res[0][0] = (t1[0][0] * t2[0][0] + t1[0][1] * t2[1][0]) % mod
res[0][1] = (t1[0][0] * t2[1][0] + t1[0][1] * t2[1][1]) % mod
res[1][0] = (t1[1][0] * t2[0][0] + t1[1][1] * t2[1][0]) % mod
res[1][1] = (t1[1][0] * t2[1][0] + t1[1][1] * t2[1][1]) % mod
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR RETURN VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, N):
def multiply(F, M):
mod = 10**9 + 7
x = F[0][0] * M[0][0] + F[0][1] * M[1][0]
y = F[0][0] * M[0][1] + F[0][1] * M[1][1]
z = F[1][0] * M[0][0] + F[1][1] * M[1][0]
w = F[1][0] * M[0][1] + F[1][1] * M[1][1]
F[0][0] = x % mod
F[0][1] = y % mod
F[1][0] = z % mod
F[1][1] = w % mod
def power(F, n):
if n == 0 or n == 1:
return
M = [[1, 1], [1, 0]]
power(F, n // 2)
multiply(F, F)
if n % 2 != 0:
multiply(F, M)
mod = 10**9 + 7
if n == 0 or n == 1:
return 1
F = [[1, 1], [1, 0]]
power(F, n - 1)
return (F[0][0] % mod + F[0][1] % mod) % mod | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
mod = 1000000007
def mulmat(a, b):
x = [[0, 0], [0, 0]]
for i in range(len(a)):
for j in range(len(b)):
for k in range(len(b)):
x[i][j] += a[i][k] * b[k][j] % mod
return x
def matexp(pb, y):
if y == 1:
return pb
half = y >> 1
x = [[0, 0], [0, 0]]
x = matexp(pb, half)
x = mulmat(x, x)
if y % 2 == 0:
pass
else:
x = mulmat(x, pb)
return x
if n <= 1:
return 1
base = [[1, 1], [1, 0]]
ini = [[1, 1], [1, 0]]
pb = matexp(base, n - 1)
res = mulmat(pb, ini)
return int(res[0][0] % mod) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
m = 10**9 + 7
def multiply(F, M):
x = (F[0][0] * M[0][0] + F[0][1] * M[1][0]) % m
y = (F[0][0] * M[0][1] + F[0][1] * M[1][1]) % m
z = (F[1][0] * M[0][0] + F[1][1] * M[1][0]) % m
w = (F[1][0] * M[0][1] + F[1][1] * M[1][1]) % m
F[0][0] = x
F[0][1] = y
F[1][0] = z
F[1][1] = w
def power(F, n):
if n < 2:
return
power(F, n // 2)
multiply(F, F)
if n % 2 != 0:
multiply(F, [[1, 1], [1, 0]])
F = [[1, 1], [1, 0]]
power(F, n)
return F[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def Multiply(self, A, B, n):
new_mat = [[(0) for i in range(n)] for j in range(n)]
for i in range(n):
for j in range(n):
for k in range(n):
new_mat[i][j] += A[i][k] * B[k][j]
new_mat[i][j] %= 1000000007
return new_mat
def FindNthTerm(self, n):
if n <= 1:
return 1
dp = []
mat = [[1, 1], [1, 0]]
base_case = [1, 1]
dp.append(mat)
a = 2
while a <= n - 1:
dp.append(self.Multiply(dp[-1], dp[-1], 2))
a *= 2
n -= 1
ans = [[1, 0], [0, 1]]
i = 0
while n:
if n % 2 == 1:
ans = self.Multiply(ans, dp[i], 2)
n //= 2
i += 1
return (ans[0][0] + ans[0][1]) % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def multiply(self, a, b):
ans1 = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
ans1[i][j] += (
a[i][k] % 1000000007 * b[k][j] % 1000000007 % 1000000007
)
return ans1
def matrix(self, n, mat):
if n == 1 or n == 0:
return mat
temp = self.matrix(n // 2, mat)
ans = self.multiply(temp, temp)
if n % 2 != 0:
ans = self.multiply(ans, mat)
return ans
def FindNthTerm(self, n):
mat = [[1, 1], [1, 0]]
res = self.matrix(n, mat)
return res[0][0] % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def multiply(self, F, M):
x = F[0][0] * M[0][0] + F[0][1] * M[1][0]
y = F[0][0] * M[0][1] + F[0][1] * M[1][1]
z = F[1][0] * M[0][0] + F[1][1] * M[1][0]
w = F[1][0] * M[0][1] + F[1][1] * M[1][1]
F[0][0] = x % 1000000007
F[0][1] = y % 1000000007
F[1][0] = z % 1000000007
F[1][1] = w % 1000000007
def FindNthTerm(self, n):
F = [[1, 1], [1, 0]]
if n == 0:
return 0
self.power(F, n)
return F[0][0]
def power(self, F, n):
if n == 0 or n == 1:
return
M = [[1, 1], [1, 0]]
self.power(F, n // 2)
self.multiply(F, F)
if n % 2 != 0:
self.multiply(F, M) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def matrix_mul(self, a, b):
mod = 10**9 + 7
ans = [[(0) for _ in range(len(a))] for _ in range(len(a))]
for i in range(len(a)):
for j in range(len(a)):
for k in range(len(a)):
ans[i][j] += a[i][k] % mod * (b[k][j] % mod) % mod
return ans
def matrix_exponentiation(self, a, power):
if power == 1:
return a
root = self.matrix_exponentiation(a, power // 2)
if power % 2 == 0:
return self.matrix_mul(root, root)
else:
inter = self.matrix_mul(root, root)
return self.matrix_mul(inter, a)
def FindNthTerm(self, n):
if n == 0:
return 1
if n == 1:
return 1
matrix = [[1, 1], [1, 0]]
ans_matrix = self.matrix_exponentiation(matrix, n - 1)
return sum(ans_matrix[0]) % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | MOD = 1000000007
class Solution:
def multiply_matrix(self, matrix1, matrix2):
res = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
res[i][j] += matrix1[i][k] * matrix2[k][j]
res[i][j] %= MOD
return res
def power(self, exponent):
res = [[1, 0], [0, 1]]
c_matrix = [[1, 1], [1, 0]]
while exponent:
if exponent % 2:
res = self.multiply_matrix(res, c_matrix)
exponent = exponent // 2
c_matrix = self.multiply_matrix(c_matrix, c_matrix)
return res
def f(self, n):
res = self.power(n)
return res[0][0]
def FindNthTerm(self, n):
return self.f(n) | ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
a = [[1, 1], [1, 0]]
def multiply(mat1, mat2):
ans = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
ans[i][j] = ans[i][j] + mat1[i][k] * mat2[k][j] % 1000000007
return ans
def check(a, n):
if n == 1:
return a
temp = check(a, n // 2)
if n % 2 == 0:
return multiply(temp, temp)
return multiply(temp, multiply(temp, a))
m = check(a, n)
return m[0][0] % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
if n == 1:
return 1
if n == 0:
return 1
intial = [[1], [1]]
A = [[1, 1], [1, 0]]
def mul_matrix(a, b):
res = [[0, 0], [0, 0]]
for i in range(len(a)):
for j in range(len(b[0])):
for k in range(len(b)):
res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % (10**9 + 7)
return res
def mat_exp(n, A):
if n == 1:
return A
ans = mat_exp(n // 2, A)
ans = mul_matrix(ans, ans)
if n % 2 == 0:
return ans
else:
ans = mul_matrix(ans, A)
return ans
x = mat_exp(n - 1, A)
final = mul_matrix(x, intial)
return final[0][0] | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER LIST NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
m = 1000000007
if n < 2:
return 1
mt = [[1, 1], [1, 0]]
f = [[1], [1]]
def mul(A, B):
res = [[0, 0], [0, 0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
res[i][j] += A[i][k] * B[k][j]
res[i][j] %= m
return res
def exp(n):
if n == 1:
return mt
h = n // 2
ans = exp(h)
ans = mul(ans, ans)
if n % 2 == 0:
return ans
else:
ans = mul(ans, mt)
return ans
s = exp(n - 1)
mxl = mul(s, f)
return mxl[0][0] % m
if __name__ == "__main__":
T = int(input())
for i in range(T):
n = int(input())
ob = Solution()
ans = ob.FindNthTerm(n)
print(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER LIST NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | def matMult(mat1, mat2):
return [
[
(mat1[0][0] * mat2[0][0] + mat1[0][1] * mat2[1][0]) % 1000000007,
(mat1[0][0] * mat2[0][1] + mat1[0][1] * mat2[1][1]) % 1000000007,
],
[
(mat1[1][0] * mat2[0][0] + mat1[1][1] * mat2[1][0]) % 1000000007,
(mat1[1][0] * mat2[0][1] + mat1[1][1] * mat2[1][1]) % 1000000007,
],
]
def solver(n, dct):
if n == 0:
return [[1, 1], [1, 0]]
elif n == 1:
return [[1, 1], [1, 0]]
elif n == 2:
return [[2, 1], [1, 1]]
if n in dct:
return dct[n]
mat = [[1, 1], [1, 0]]
if n % 2 == 0:
mat = matMult(solver(n // 2, dct), solver(n // 2, dct))
else:
mat = matMult(solver(n // 2, dct), solver(n // 2 + 1, dct))
dct[n] = mat
return mat
class Solution:
def FindNthTerm(self, n):
if n == 1:
return 1
elif n == 2:
return 2
dct = dict()
mat = solver(n, dct)
return mat[0][0] | FUNC_DEF RETURN LIST LIST BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER LIST BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def multiply(self, a, b):
c = []
for i in range(2):
d = []
for j in range(2):
g = 0
for k in range(2):
g += a[i][k] * b[k][j] % 1000000007
d.append(g)
c.append(d)
return c
def mat_expo(self, a, n):
if n == 0:
return [[1, 0], [0, 1]]
if n == 1:
return [[1, 1], [1, 0]]
temp = self.mat_expo(a, n // 2)
ans = self.multiply(temp[:], temp[:])
if n % 2 == 1:
ans = self.multiply(ans[:], [[1, 1], [1, 0]])
return ans
def FindNthTerm(self, n):
if n <= 2:
return n
a = [[1, 1], [1, 0]]
b = self.mat_expo(a, n - 1)
g = b[0][0] + b[0][1]
return g % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
mod = 10**9 + 7
def power(M, n1):
temp = [[1, 1], [1, 0]]
if n1 == 1:
return M
M1 = power(M, int(n1 / 2))
if n1 % 2 == 0:
return multiply(M1, M1)
return multiply(multiply(M1, M1), temp)
def multiply(a, b):
mul = [[(0) for x in range(2)] for y in range(2)]
for i in range(2):
for j in range(2):
mul[i][j] = 0
for k in range(2):
mul[i][j] += a[i][k] * b[k][j]
mul[i][j] = mul[i][j] % mod
for i in range(2):
for j in range(2):
a[i][j] = mul[i][j]
return a
matrix = [[1, 1], [1, 0]]
arr = power(matrix, n)
return arr[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | MOD = 10**9 + 7
class Solution:
T = [[0, 1], [1, 1]]
def multiply(self, A, B):
C = [[0, 0], [0, 0]]
C[0][0] = (A[0][0] * B[0][0] + A[0][1] * B[1][0]) % MOD
C[0][1] = (A[0][0] * B[0][1] + A[0][1] * B[1][1]) % MOD
C[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % MOD
C[1][1] = (A[1][0] * B[0][1] + A[1][1] * B[1][1]) % MOD
return C
def calcule(self, X, n):
if n == 1:
return X
elif n % 2 == 0:
X = self.calcule(X, n / 2)
return self.multiply(X, X)
else:
return self.multiply(X, self.calcule(X, n - 1))
def FindNthTerm(self, n):
if n < 2:
return 1
T = self.calcule(self.T, n - 1)
Fn = [0, 0]
Fn[0] = T[0][0] + T[0][1]
Fn[1] = T[1][0] + T[1][1]
return Fn[1] % MOD | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def Multiply(self, a, b, mod):
multiply = [[0, 0], [0, 0]]
multiply[0][0] = (a[0][0] * b[0][0] % mod + a[0][1] * b[1][0] % mod) % mod
multiply[0][1] = (a[0][0] * b[0][1] % mod + a[0][1] * b[1][1] % mod) % mod
multiply[1][0] = (a[1][0] * b[0][0] % mod + a[1][1] * b[1][0] % mod) % mod
multiply[1][1] = (a[1][0] * b[0][1] % mod + a[1][1] * b[1][1] % mod) % mod
return multiply
def GetAnswer(self, matrix, n, mod):
if n == 0:
ans = [[1, 0], [0, 1]]
return ans
elif n % 2 == 0:
ans = self.GetAnswer(matrix, n // 2, mod)
return self.Multiply(ans, ans, mod)
else:
ans = self.GetAnswer(matrix, n - 1, mod)
return self.Multiply(matrix, ans, mod)
def FindNthTerm(self, n):
mod = 1000000007
matrix = [[0, 1], [1, 1]]
ans = self.GetAnswer(matrix, n, mod)
return ans[1][1] % mod | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
m = 10**9 + 7
def multi(mat, ans):
res = [[0, 0], [0, 0]]
for i in range(len(mat)):
for j in range(len(ans)):
for k in range(len(mat)):
res[i][j] += mat[i][k] % m * (ans[k][j] % m)
return res
mat = [[1, 1], [1, 0]]
ans = [[1, 1], [1, 0]]
while n:
if n & 1:
ans = multi(mat, ans)
n -= 1
else:
mat = multi(mat, mat)
n >>= 1
return ans[0][1] % m | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | def multiplymatrix(mat, mat1):
h = 10**9 + 7
temp = [[0, 0], [0, 0]]
temp[0][0] = mat[0][0] * mat1[0][0] % h + mat[0][1] * mat1[1][0] % h
temp[0][0] %= h
temp[0][1] = mat[0][0] * mat1[0][1] % h + mat[0][1] * mat1[1][1] % h
temp[0][1] %= h
temp[1][0] = mat[1][0] * mat1[0][0] % h + mat[1][1] * mat1[1][0] % h
temp[1][0] %= h
temp[1][1] = mat[1][0] * mat1[0][1] % h + mat[1][1] * mat1[1][1] % h
temp[1][1] %= h
mat[0][0] = temp[0][0]
mat[0][1] = temp[0][1]
mat[1][0] = temp[1][0]
mat[1][1] = temp[1][1]
def matrix(n, mat):
if n == 1:
return
matrix(n // 2, mat)
multiplymatrix(mat, mat)
m = [[1, 1], [1, 0]]
if n % 2 != 0:
multiplymatrix(mat, m)
class Solution:
def FindNthTerm(self, n):
if n <= 1:
return 1
mat = [[1, 1], [1, 0]]
matrix(n, mat)
return mat[0][0] | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | mod = 10**9 + 7
class Solution:
def Mult(self, A, B):
n = 2
C = [[(0) for i in range(n)] for j in range(n)]
for i in range(n):
for j in range(n):
C[i][j] = sum([(A[i][k] * B[k][j] % mod) for k in range(n)]) % mod
return C
def Expo(self, A, b):
if b == 0:
return [[1, 0], [0, 1]]
if b % 2 == 0:
p = self.Expo(A, b // 2)
return self.Mult(p, p)
return self.Mult(A, self.Expo(A, b - 1))
def FindNthTerm(self, n):
if n == 0:
return 1
if n == 1:
return 1
A = [[0, 1], [1, 1]]
P = self.Expo(A, n - 1)
return (P[1][0] + P[1][1]) % mod | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | mod = 10**9 + 7
class Solution:
def multiply_matrix(self, mat1, mat2):
a = (mat1[0][0] * mat2[0][0] + mat1[0][1] * mat2[1][0]) % mod
b = (mat1[0][0] * mat2[0][1] + mat1[0][1] * mat2[1][1]) % mod
c = (mat1[1][0] * mat2[0][0] + mat1[1][1] * mat2[1][0]) % mod
d = (mat1[1][0] * mat2[0][1] + mat1[1][1] * mat2[1][1]) % mod
return [[a, b], [c, d]]
def multiply(self, mat, n):
if n == 1:
return mat
temp = self.multiply(mat, n // 2)
value = self.multiply_matrix(temp, temp)
if n % 2 == 0:
return value
else:
return self.multiply_matrix(value, self.multiply(mat, 1))
def FindNthTerm(self, n):
mat = [[1, 1], [1, 0]]
return self.multiply(mat, n)[0][0] % mod | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR RETURN LIST LIST VAR VAR LIST VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
def mul(mat1, mat2):
ans = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
ans[i][j] += mat1[i][k] * mat2[k][j] % 1000000007
return ans
def find_nth(a, n):
if n == 1:
return a
ans = find_nth(a, n // 2)
if n % 2 == 0:
return mul(ans, ans)
else:
return mul(ans, mul(ans, a))
a = [[1, 1], [1, 0]]
ans = find_nth(a, n)
return ans[0][0] % 1000000007
if __name__ == "__main__":
T = int(input())
for i in range(T):
n = int(input())
ob = Solution()
ans = ob.FindNthTerm(n)
print(ans) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, ni):
mati = [[1, 0], [0, 1]]
mo = int(1000000000.0) + 7
def multiply(a, b):
w = (a[0][0] * b[0][0] % mo + a[0][1] * b[1][0] % mo) % mo
x = (a[0][0] * b[0][1] % mo + a[0][1] * b[1][1] % mo) % mo
y = (a[1][0] * b[0][0] % mo + a[1][1] * b[1][0] % mo) % mo
z = (a[1][0] * b[0][1] % mo + a[1][1] * b[1][1] % mo) % mo
return [[w, x], [y, z]]
def power(t, n):
if n == 0:
return mati
k = power(t, n // 2)
if n % 2 != 0:
return multiply(multiply(k, k), t)
return multiply(k, k)
mat = [[1, 1], [1, 0]]
if ni == 0 or ni == 1:
return 1
q = power(mat, ni)
return q[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR RETURN LIST LIST VAR VAR LIST VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
mat = [[1, 1], [1, 0]]
return self.find_nth(mat, n)[0][0] % 1000000007
def find_nth(self, mat, n):
if n == 1:
return mat
temp = self.find_nth(mat, n // 2)
if n & 1:
return self.multiply_mat(temp, self.multiply_mat(temp, mat))
else:
return self.multiply_mat(temp, temp)
def multiply_mat(self, a, b):
new_mat = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
new_mat[i][j] += a[i][k] * b[k][j] % 1000000007
return new_mat | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
mod = int(1000000000.0 + 7)
def multiply(a, b):
arr = [[0, 0], [0, 0]]
arr[0][0] = (a[0][0] * b[0][0] + a[0][1] * b[1][0]) % mod
arr[0][1] = (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % mod
arr[1][0] = (a[1][0] * b[0][0] + a[1][1] * b[1][0]) % mod
arr[1][1] = (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % mod
return arr
def power(arr, p):
if p == 1:
return arr
temp = power(arr, p // 2)
if p % 2 == 0:
return multiply(temp, temp)
return multiply(multiply(temp, temp), arr)
arr = [[1, 1], [1, 0]]
arr = power(arr, n)
return int(arr[0][0]) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
def mul(mat1, mat2):
ans = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
ans[i][j] += mat1[i][k] * mat2[k][j] % 1000000007
return ans
def find_nth(a, n):
if n == 1:
return a
ans = find_nth(a, n // 2)
if n % 2 == 0:
return mul(ans, ans)
else:
return mul(ans, mul(ans, a))
a = [[1, 1], [1, 0]]
ans = find_nth(a, n)
return ans[0][0] % 1000000007 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def multiply(self, a, b):
mul = [[(0) for x in range(3)] for y in range(3)]
for i in range(3):
for j in range(3):
mul[i][j] = 0
for k in range(3):
mul[i][j] += a[i][k] * b[k][j]
for i in range(3):
for j in range(3):
a[i][j] = mul[i][j] % int(1000000000.0 + 7)
return a
def power(self, F, n):
M = [[1, 1, 0], [1, 0, 0], [0, 1, 0]]
if n == 1:
return F[0][0] + F[0][1]
self.power(F, int(n / 2))
F = self.multiply(F, F)
if n % 2 != 0:
F = self.multiply(F, M)
return F[0][0] + F[0][1]
def FindNthTerm(self, n):
if n == 1:
return 1
if n == 2:
return 2
F = [[1, 1, 0], [1, 0, 0], [0, 1, 0]]
k = self.power(F, n - 1)
return k % int(1000000000.0 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
n += 1
mod = 10**9 + 7
if n == 1:
return 1
if n == 2:
return 1
I = [[0, 0, 0], [0, 1, 0], [0, 0, 1]]
T = [[0, 0, 0], [0, 0, 1], [0, 1, 1]]
n -= 1
def mul(A, B, dim):
res = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for i in range(1, dim + 1):
for j in range(1, dim + 1):
for k in range(1, dim + 1):
temp = A[i][k] * B[k][j] % mod
res[i][j] = (res[i][j] + temp) % mod
for i in range(dim + 1):
for j in range(dim + 1):
A[i][j] = res[i][j]
while n:
if n % 2:
mul(I, T, 2)
n -= 1
else:
mul(T, T, 2)
n //= 2
ans = (1 * I[1][1] + 1 * I[2][1]) % mod
return ans | CLASS_DEF FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER NUMBER VAR RETURN VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def mul(self, m1, m2):
a = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
a[i][j] += m1[i][k] * m2[k][j]
a[i][j] = a[i][j] % (10**9 + 7)
return a
def recur(self, n):
if n == 1:
return [[1, 1], [1, 0]]
temp = self.recur(n // 2)
ans = self.mul(temp, temp)
if n % 2 != 0:
ans = self.mul(ans, [[1, 1], [1, 0]])
return ans
def FindNthTerm(self, n):
mod = 10**9 + 7
c = self.recur(n)
return c[0][0] % mod | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER NUMBER VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
def prod(x, y, m):
z = [[], []]
z[0].append(x[0][0] * y[0][0] + x[1][0] * y[0][1])
z[1].append(x[0][0] * y[1][0] + x[1][0] * y[1][1])
z[0].append(x[0][1] * y[0][0] + x[1][1] * y[0][1])
z[1].append(x[0][1] * y[1][0] + x[1][1] * y[1][1])
z[0][0] = z[0][0] % m
z[0][1] = z[0][1] % m
z[1][0] = z[1][0] % m
z[1][1] = z[1][1] % m
return z
x = [[1, 1], [1, 0]]
def power(x, y, m):
if y == 1:
result = x
else:
z = y
w = []
while z > 0:
if z % 2 == 0:
w.append(0)
else:
w.append(1)
z = z // 2
p = [[1, 0], [0, 1]]
a = x[:]
for i in range(0, len(w)):
if w[i] == 1:
p = prod(p, a, m)
a = prod(a, a, m)
result = p[:]
return result
m = 10**9 + 7
c = power(x, n + 1, m)
res = c[0][1]
return res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST LIST LIST EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR RETURN VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER RETURN VAR |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | mod = pow(10, 9) + 7
class Solution:
def multiply(self, a, b):
x = a[0][0] * b[0][0] + a[0][1] * b[1][0]
y = a[0][0] * b[0][1] + a[0][1] * b[1][1]
x1 = a[1][0] * b[0][0] + a[1][1] * b[1][0]
y1 = a[1][0] * b[0][1] + a[1][1] * b[1][1]
a[0][0] = x % mod
a[0][1] = y % mod
a[1][0] = x1 % mod
a[1][1] = y1 % mod
def power(self, f, n):
if n == 1 or n == 0:
return
self.power(f, n // 2)
self.multiply(f, f)
if n % 2 != 0:
m = [[1, 1], [1, 0]]
self.multiply(f, m)
def FindNthTerm(self, n):
if n == 0 or n == 1:
return n
f = [[1, 1], [1, 0]]
self.power(f, n)
return f[0][0] | ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given an equation of the form f(n) = f(n-1) + f(n-2) where f(0) = 1, f(1) = 1 , the task is to find the n^{th} term of this sequence.
Example 1:
Input: n = 3
Output: 3
Explanation: f(3) = f(2) + f(1) = 3
Example 2:
Input: n = 2
Output: 2
Explanation: f(2) = f(1) + f(0) = 2
Yout Task:
You don't need to read or print anything. Your task is to complete the function FindNthTerm() which takes n as input parameter and returns n^{th} term mod 10^9+7 .
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(K) where K is constant.
Constraints:
1 <= n <= 10^{9} | class Solution:
def FindNthTerm(self, n):
def multiply(a, b):
m = [[0, 0], [0, 0]]
m[0][0] = (a[0][0] * b[0][0] + a[0][1] * b[1][0]) % 1000000007
m[0][1] = (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % 1000000007
m[1][0] = (a[1][0] * b[0][0] + a[1][1] * b[1][0]) % 1000000007
m[1][1] = (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % 1000000007
return m
def power(a, n):
if n == 0:
return [[1, 0], [0, 1]]
elif n == 1:
return a
p = power(a, n // 2)
if n % 2 == 0:
return multiply(p, p)
else:
p = multiply(p, p)
return multiply(a, p)
def function(n):
if n == 1:
return 1
elif n == 2:
return 2
m = power([[0, 1], [1, 1]], n)
z = [[0], [1]]
return m[1][0] * z[0][0] + m[1][1] * z[1][0]
return function(n) % 1000000007 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER VAR ASSIGN VAR LIST LIST NUMBER LIST NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, nums, n):
max_prod, min_prod, ans = nums[0], nums[0], nums[0]
for i in range(1, len(nums)):
x = max(nums[i], max_prod * nums[i], min_prod * nums[i])
y = min(nums[i], max_prod * nums[i], min_prod * nums[i])
max_prod, min_prod = x, y
ans = max(max_prod, ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
if n == 0:
return 0
max_product = arr[0]
min_product = arr[0]
max_so_far = arr[0]
for i in range(1, n):
curr = arr[i]
temp_max = max(curr, max_product * curr, min_product * curr)
min_product = min(curr, max_product * curr, min_product * curr)
max_product = temp_max
max_so_far = max(max_so_far, max_product)
return max_so_far | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
max = arr[0]
for i in range(n):
c = arr[i]
if arr[i] == 0:
continue
else:
for j in range(i + 1, n):
if arr[j] == 0:
break
else:
c = c * arr[j]
if max < c:
max = c
if max < c:
max = c
return max | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
maxi = -1000.0
cur = 1
for i in range(n):
cur *= arr[i]
if cur > maxi:
maxi = cur
if cur == 0:
cur = 1
cur = 1
for i in range(n - 1, -1, -1):
cur *= arr[i]
if cur > maxi:
maxi = cur
if cur == 0:
cur = 1
return maxi | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, size):
curMax = curMin = 1
res = arr[0]
for n in arr:
vals = n, n * curMax, n * curMin
curMax = max(vals)
curMin = min(vals)
res = max(res, curMax)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, nums, n):
ans = arr[0]
maxx, minn = ans, ans
for i in range(1, n):
if arr[i] < 0:
var = maxx
maxx = minn
minn = var
maxx = max(arr[i], maxx * arr[i])
minn = min(arr[i], minn * arr[i])
ans = max(ans, maxx)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
ma = arr[0]
mi = arr[0]
res = arr[0]
for e in arr[1:]:
if e < 0:
ma, mi = mi, ma
ma = max(e, e * ma)
mi = min(e, e * mi)
res = max(res, ma)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
maxxi = 0
a = 0
value = 0
pehla = 0
last = 0
if len(arr) == 1:
maxxi = arr[0]
else:
for i in range(0, len(arr)):
if value == 0 and arr[i] < 0 and i == len(arr) - 1:
value = 0
elif value == 0 and arr[i] < 0 and arr[i + 1] == 0:
value = 0
else:
if i == len(arr) - 1 and arr[i] != 0:
if arr[i] > 0:
if value == 0:
value = 1
value *= arr[i]
else:
value *= arr[i]
last = last * arr[i]
elif arr[i] < 0:
if value == 0:
value = 1
value *= arr[i]
else:
value *= arr[i]
if pehla == 0:
pehla = value
last = arr[i]
if value > 0:
if value > maxxi:
maxxi = value
elif value < 0:
ww = max(pehla, last)
if value // ww > maxxi:
maxxi = value // ww
if arr[i] > 0:
if value == 0:
value = 1
value *= arr[i]
else:
value *= arr[i]
last = last * arr[i]
elif arr[i] < 0:
if value == 0:
value = 1
value *= arr[i]
else:
value *= arr[i]
if pehla == 0:
pehla = value
last = arr[i]
else:
if value > 0:
if value > maxxi:
maxxi = value
elif value < 0:
ww = max(pehla, last)
if value // ww > maxxi:
maxxi = value // ww
value = 0
pehla = 0
last = 0
return maxxi | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
if n == 0:
return 0
currentMin, currentMax, maxProduct = arr[0], arr[0], arr[0]
for i in range(1, n):
if arr[i] < 0:
currentMax, currentMin = currentMin, currentMax
currentMax = max(arr[i], currentMax * arr[i])
currentMin = min(arr[i], currentMin * arr[i])
maxProduct = max(maxProduct, currentMax)
return maxProduct | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
res = max(arr)
cmin, cmax = 1, 1
for i in arr:
if i == 0:
cmin, cmax = 1, 1
continue
temp = cmax * i
cmax = max(cmax * i, cmin * i, i)
cmin = min(temp, cmin * i, i)
res = max(cmax, res)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
max_product = 1
min_product = 1
res = max(arr)
for i in arr:
if i == 0:
max_product = 1
min_product = 1
continue
temp = i * max_product
max_product = max(temp, i, min_product * i)
min_product = min(i, min_product * i, temp)
res = max(res, max_product, min_product)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
pre = 1
suff = 1
ans = float("-inf")
for i in range(n):
if pre == 0:
pre = 1
if suff == 0:
suff = 1
pre = pre * arr[i]
suff = suff * arr[n - i - 1]
ans = max(ans, max(pre, suff))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
max_element = 1
min_element = 1
result = -float("inf")
for i in range(n):
x = max_element * arr[i]
y = min_element * arr[i]
max_element = max(x, y, arr[i])
min_element = min(x, y, arr[i])
result = max(max_element, result)
if arr[i] == 0:
min_element = 1
max_element = 1
return result | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
max_product = -1000000000.0
product = 1
for i in range(n):
if arr[i] == 0:
product = 1
max_product = max(0, max_product)
else:
product *= arr[i]
max_product = max(product, max_product)
product = 1
for i in range(n - 1, -1, -1):
if arr[i] == 0:
product = 1
max_product = max(0, max_product)
else:
product *= arr[i]
max_product = max(product, max_product)
return max_product | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | import sys
class Solution:
def maxProduct(self, arr, n):
frontprod = 1
endprod = 1
res = -sys.maxsize - 1
for i in range(n):
frontprod = frontprod * arr[i]
if frontprod > res:
res = frontprod
if frontprod == 0:
frontprod = 1
endprod = endprod * arr[n - i - 1]
if endprod > res:
res = endprod
if endprod == 0:
endprod = 1
return res | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
dic = {}
max1 = -10000000
pr = 1
for i in range(len(arr)):
pr = pr * arr[i]
if max1 < pr:
max1 = pr
if pr == 0:
pr = 1
dic.clear()
continue
for j in dic:
pr1 = pr // dic[j]
if max1 < pr1:
max1 = pr1
dic[i] = pr
return max1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
max_so_far = arr[0]
max_ending_here = arr[0]
min_ending_here = arr[0]
for i in range(1, len(arr)):
temp = max_ending_here
max_ending_here = max(
arr[i], max_ending_here * arr[i], min_ending_here * arr[i]
)
min_ending_here = min(temp * arr[i], min_ending_here * arr[i], arr[i])
max_so_far = max(max_so_far, max_ending_here)
return max_so_far | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.
Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 500
-10^{2} β€ Arr_{i} β€ 10^{2} | class Solution:
def maxProduct(self, arr, n):
b = arr[::-1]
a = arr
for i in range(1, n):
if a[i - 1] != 0:
a[i] *= a[i - 1]
if b[i - 1] != 0:
b[i] *= b[i - 1]
return max(a + b) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.