description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def path(self, i, j, path, res, grid, n, cache):
if i == n - 1 and j == n - 1:
res.append(path)
return
cache[i][j] = 1
if i + 1 < n and grid[i + 1][j] != 0 and cache[i + 1][j] != 1:
self.path(i + 1, j, path + "D", res, grid, n, cache)
if j - 1 >= 0 and grid[i][j - 1] != 0 and cache[i][j - 1] != 1:
self.path(i, j - 1, path + "L", res, grid, n, cache)
if j + 1 < n and grid[i][j + 1] != 0 and cache[i][j + 1] != 1:
self.path(i, j + 1, path + "R", res, grid, n, cache)
if i - 1 >= 0 and grid[i - 1][j] != 0 and cache[i - 1][j] != 1:
self.path(i - 1, j, path + "U", res, grid, n, cache)
cache[i][j] = 0
def findPath(self, m, n):
if m[0][0] == 0 or m[-1][-1] == 0:
return [-1]
cache = [[(0) for _ in range(n)] for _ in range(n)]
res = []
path = ""
self.path(0, 0, path, res, m, n, cache)
return res | CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def findPath(self, m, n):
self.res = []
if m[0][0] == 0:
return [-1]
def check(ans, i, j, mat, N):
if i < 0 or i >= N or j < 0 or j >= N:
return False
if mat[i][j] == 0 or mat[i][j] == 2:
return False
if i == N - 1 and j == N - 1:
self.res.append(ans)
return
mat[i][j] = 2
check(ans + "U", i - 1, j, mat, N)
check(ans + "R", i, j + 1, mat, N)
check(ans + "D", i + 1, j, mat, N)
check(ans + "L", i, j - 1, mat, N)
mat[i][j] = 1
check("", 0, 0, m, n)
if not self.res:
return [-1]
return self.res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER NUMBER NUMBER RETURN LIST NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING NUMBER NUMBER VAR VAR IF VAR RETURN LIST NUMBER RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def findPath(self, m, n):
ans, dir, dl = [], [[1, 0], [0, -1], [0, 1], [-1, 0]], ["D", "L", "R", "U"]
def dfs(node, path):
i, j = node
if m[i][j] != 1:
return
if i == n - 1 and j == n - 1:
ans.append(path)
return
m[i][j] = 2
for d in range(4):
ni, nj = i + dir[d][0], j + dir[d][1]
if ni >= 0 and ni < n and nj >= 0 and nj < n and m[ni][nj] != 2:
path += dl[d]
dfs((ni, nj), path)
path = path[: len(path) - 1]
m[i][j] = 1
return
dfs((0, 0), "")
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR LIST LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST STRING STRING STRING STRING FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER NUMBER STRING RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def findPath(self, m, n):
paths = []
moves = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)}
def is_valid(row, col, m, n):
if row >= n or row < 0 or col >= n or col < 0:
return False
elif m[row][col] == 0:
return False
else:
return True
def solve(moves, temp, m, n, vis, row, col):
if row == n - 1 and col == n - 1:
paths.append("".join(temp))
for k, v in moves.items():
newRow, newCol = row + v[0], col + v[1]
if is_valid(newRow, newCol, m, n) and not vis[newRow][newCol]:
temp.append(k)
vis[newRow][newCol] = True
solve(moves, temp, m, n, vis, newRow, newCol)
vis[newRow][newCol] = False
temp.pop()
if m[0][0] == 1:
vis = [[(False) for i in range(n)] for j in range(n)]
vis[0][0] = True
solve(moves, [], m, n, vis, 0, 0)
return paths | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR NUMBER NUMBER RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def path(self, i, j, m, n, db, str):
if i < 0 or j < 0 or i >= n or j >= n or m[i][j] == 0:
return
if i == n - 1 and j == n - 1:
db.append(str)
return
m[i][j] = 0
self.path(i + 1, j, m, n, db, str + "D")
self.path(i, j + 1, m, n, db, str + "R")
self.path(i - 1, j, m, n, db, str + "U")
self.path(i, j - 1, m, n, db, str + "L")
str = " "
m[i][j] = 1
def findPath(self, m, n):
i = 0
j = 0
db = []
self.path(i, j, m, n, db, "")
return db | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def findPath(self, m, n):
all_solutions = []
paths = {"U": (-1, 0), "D": (1, 0), "R": (0, 1), "L": (0, -1)}
def solution(x, y, current_path):
if m[x][y] == 0:
return -1
if x < 0 or y < 0 or x > n - 1 or y > n - 1:
return -1
if (x, y) == (n - 1, n - 1):
all_solutions.append(current_path)
return
for dir, positions in paths.items():
_x = x + positions[0]
_y = y + positions[1]
if _x < 0 or _y < 0 or _x > n - 1 or _y > n - 1 or m[_x][_y] != 1:
continue
m[x][y] = 2
solution(_x, _y, current_path + dir)
m[x][y] = 1
solution(0, 0, current_path="")
return all_solutions | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER STRING RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | def sol(row, col, n, mat, path, ans):
if col == n - 1 and row == n - 1:
ans.append(path)
return ans
last = ""
if len(path) != 0:
last = path[-1]
if row != n - 1 and mat[row + 1][col] != 0 and mat[row + 1][col] > 0:
mat[row + 1][col] = -1
ans = sol(row + 1, col, n, mat, path + "D", ans)
mat[row + 1][col] = 1
if col != 0 and mat[row][col - 1] != 0 and mat[row][col - 1] > 0:
mat[row][col - 1] = -1
ans = sol(row, col - 1, n, mat, path + "L", ans)
mat[row][col - 1] = 1
if col != n - 1 and mat[row][col + 1] != 0 and mat[row][col + 1] > 0:
mat[row][col + 1] = -1
ans = sol(row, col + 1, n, mat, path + "R", ans)
mat[row][col + 1] = 1
if row != 0 and mat[row - 1][col] != 0 and mat[row - 1][col] > 0:
mat[row - 1][col] = -1
ans = sol(row - 1, col, n, mat, path + "U", ans)
mat[row - 1][col] = 1
return ans
class Solution:
def findPath(self, m, n):
if m[0][0] == 0:
return [-1]
ans = []
m[0][0] = -1
return sol(0, 0, n, m, "", ans) | FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR STRING VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR STRING VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER NUMBER RETURN LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR STRING VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def findPath(self, m, n):
vis = [([0] * n) for i in range(n)]
def dfs(i, j, vis, s, m):
if i == n - 1 and j == n - 1:
if m[i][j] == 1:
ans.append(s)
return
if 0 <= i < n and 0 <= j < n and vis[i][j] == 0 and m[i][j] == 1:
m[i][j] = 0
dfs(i + 1, j, vis, s + "D", m)
dfs(i - 1, j, vis, s + "U", m)
dfs(i, j + 1, vis, s + "R", m)
dfs(i, j - 1, vis, s + "L", m)
m[i][j] = 1
else:
return
ans = []
dfs(0, 0, vis, "", m)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR ASSIGN VAR VAR VAR NUMBER RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER VAR STRING VAR RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def setup(self):
global v
v = [[(0) for i in range(100)] for j in range(100)]
global ans
ans = []
def path(self, arr, x, y, pth, n):
if x == n - 1 and y == n - 1:
global ans
ans.append(pth)
return
global v
if arr[x][y] == 0 or v[x][y] == 1:
return
v[x][y] = 1
if x > 0:
self.path(arr, x - 1, y, pth + "U", n)
if y > 0:
self.path(arr, x, y - 1, pth + "L", n)
if x < n - 1:
self.path(arr, x + 1, y, pth + "D", n)
if y < n - 1:
self.path(arr, x, y + 1, pth + "R", n)
v[x][y] = 0
def findPath(self, m, n):
global ans
ans = []
if m[0][0] == 0 or m[n - 1][n - 1] == 0:
return ans
self.setup()
self.path(m, 0, 0, "", n)
ans.sort()
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING VAR EXPR FUNC_CALL VAR RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def findPath(self, m, n):
res = []
vis = [([0] * n) for i in range(n)]
s = ""
if m[0][0] == 0 or m[n - 1][n - 1] == 0:
return res
def dfs(r, c, vis, s):
if r == n - 1 and c == n - 1:
res.append(s)
return
if r < 0 or c < 0 or r >= n or c >= n or vis[r][c] == 1 or m[r][c] == 0:
return
vis[r][c] = 1
dfs(r + 1, c, vis, s + "D")
dfs(r - 1, c, vis, s + "U")
dfs(r, c + 1, vis, s + "R")
dfs(r, c - 1, vis, s + "L")
vis[r][c] = 0
dfs(0, 0, vis, s)
res.sort()
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Example 1:
Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.
Expected Time Complexity: O((3^{N}^{^2})).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.
Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1 | class Solution:
def findPath(self, m, n):
res = []
path = ""
visited = [[(0) for _ in range(n)] for _ in range(n)]
if m[0][0] == 0 or m[n - 1][n - 1] == 0:
return res
def fp(i, j, path):
if i == n - 1 and j == n - 1:
res.append(path)
return
if i + 1 < n and m[i + 1][j] != 0 and visited[i + 1][j] != 1:
visited[i][j] = 1
fp(i + 1, j, path + "D")
visited[i][j] = 0
if j + 1 < n and m[i][j + 1] != 0 and visited[i][j + 1] != 1:
visited[i][j] = 1
fp(i, j + 1, path + "R")
visited[i][j] = 0
if i - 1 >= 0 and m[i - 1][j] != 0 and visited[i - 1][j] != 1:
visited[i][j] = 1
fp(i - 1, j, path + "U")
visited[i][j] = 0
if j - 1 >= 0 and m[i][j - 1] != 0 and visited[i][j - 1] != 1:
visited[i][j] = 1
fp(i, j - 1, path + "L")
visited[i][j] = 0
fp(0, 0, path)
if len(res) == 0:
return []
res.sort()
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST EXPR FUNC_CALL VAR RETURN VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1, s2 = input(), input()
len1, len2 = len(s1), len(s2)
t, res = "", 0
for i, x in enumerate(s1):
t += x
if len1 % (i + 1) == 0 and len2 % (i + 1) == 0:
x1, x2 = len1 // (i + 1), len2 // (i + 1)
if t * x1 == s1 and t * x2 == s2:
res += 1
print(res) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s = input()
t = input()
m = len(s)
n = len(t)
ans = 0
for i in range(1, min(m, n) + 1):
if m % i > 0 or n % i > 0:
continue
p = s[:i]
if p * (m // i) == s and p * (n // i) == t:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1, s2 = sorted([input() for i in range(2)], key=lambda x: len(x))
l1, l2, r = len(s1), len(s2), 0
for i in range(1, l1 + 1):
if l1 % i == 0 and l2 % i == 0:
c1, c2 = int(l1 / i), int(l2 / i)
if c1 * s1[:i] == s1 and c2 * s1[:i] == s2:
r += 1
print(r) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def fs(k):
init = k
if s1[:k] != s2[:k]:
return False
while init <= l1 - k:
if s1[:k] != s1[init : init + k]:
return False
init += k
init = k
while init <= l2 - k:
if s2[:k] != s2[init : init + k]:
return False
init += k
return True
cnt = 0
s1 = input()
s2 = input()
l1 = len(s1)
l2 = len(s2)
for i in range(1, min(l1, l2) + 1):
if l1 % i == 0 and l2 % i == 0:
if fs(i):
cnt += 1
print(cnt) | FUNC_DEF ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER WHILE VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
a = []
b = []
s = ""
for i in range(len(s1)):
s += s1[i]
x = len(s1) // (i + 1)
if s * x == s1:
a.append(s)
s = ""
for i in range(len(s2)):
s += s2[i]
x = len(s2) // (i + 1)
if s * x == s2:
b.append(s)
cnt = 0
for i in range(len(a)):
for j in range(len(b)):
if a[i] == b[j]:
cnt += 1
print(cnt) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
s1 = input()
s2 = input()
c = 0
g = gcd(len(s1), len(s2))
i = 1
while i * i <= g:
if g % i == 0:
if s1[:i] == s2[:i]:
if s1[:i] * (len(s1) // i) == s1:
if s2[:i] * (len(s2) // i) == s2:
c += 1
j = g // i
if j != i:
if s1[:j] == s2[:j]:
if s1[:j] * (len(s1) // j) == s1:
if s2[:j] * (len(s2) // j) == s2:
c += 1
i += 1
print(c) | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s = input()
s1 = input()
if len(s) < len(s1):
s1, s = s, s1
q = 499
p = int(100000000000.0 + 7)
n = len(s)
hs = [0] * (n + 1)
pw = [1] * (n + 1)
for i in range(n):
hs[i + 1] = (hs[i] * q + ord(s[i])) % p
pw[i + 1] = pw[i] * q % p
def get_hash(a, b):
return (hs[b] - hs[a] * pw[b - a] % p + p) % p
h1 = 0
ans = 0
for x in range(len(s1)):
h1 = (h1 * q + ord(s1[x])) % p
if not len(s1) % (x + 1):
fl = True
for i in range(0, len(s), x + 1):
if get_hash(i, min(i + x + 1, len(s))) != h1:
fl = False
break
if fl:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def getDivs(s):
n = len(s)
ret = {s}
size = 1
while size * size <= n:
if n % size == 0:
sub1 = s[:size]
sub2 = s[: n // size]
if sub1 * (n // size) == s:
ret.add(sub1)
if sub2 * size == s:
ret.add(sub2)
size += 1
return ret
s1 = input()
s2 = input()
divs1 = getDivs(s1)
divs2 = getDivs(s2)
print(len(divs1.intersection(divs2))) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
l1 = len(s1)
l2 = len(s2)
s = ""
l = 0
ans = 0
for i in s1:
s = s + i
l = l + 1
if l1 % l == 0 and l2 % l == 0:
if s * int(l1 / l) == s1:
if s * int(l2 / l) == s2:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | sa = input()
sb = input()
a = len(sa)
b = len(sb)
if a > b:
sa, sb = sb, sa
a, b = b, a
l = 0
ss = ""
s = set()
for i in sa:
ss += str(i)
l += 1
if b % l == 0 and sb == ss * (b // l):
if a % l == 0 and sa == ss * (a // l):
s.add(ss)
print(len(s)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def Divisors(a, l1, l2):
i = 1
while i <= min(l1, l2):
if l1 % i == 0 and l2 % i == 0:
a.append(i)
i = i + 1
s1 = input()
s2 = input()
l1, l2 = len(s1), len(s2)
a = []
Divisors(a, l1, l2)
ss1, ss2, c = "", "", 0
for i in range(len(a)):
ss1 = s1[: a[i]] * (l1 // a[i])
ss2 = s1[: a[i]] * (l2 // a[i])
if ss1 == s1 and ss2 == s2:
c += 1
print(c) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s = input()
s1 = input()
n = len(s)
m = len(s1)
if n > m:
a = m
b = n
p = s1
q = s
else:
a = n
b = m
p = s
q = s1
s2 = ""
d = 0
for x in range(0, a):
s2 = s2 + p[x]
if a % 2 != 0 and (x + 1) % 2 == 0:
continue
if a % (x + 1) != 0:
continue
if s2 * (a // (x + 1)) == p and s2 * (b // (x + 1)) == q:
d += 1
print(d) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a = input().strip()
b = input().strip()
divisor = 0
temp = ""
lena = len(a)
lenb = len(b)
for i in range(lena):
temp += a[i]
if temp == b[: i + 1]:
if lenb // (i + 1) * temp == b and lena // (i + 1) * temp == a:
divisor += 1
print(divisor) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
l1 = len(s1)
l2 = len(s2)
d = {}
c = 0
if l1 <= l2:
s = s1
a = s2
l = l1
k = l2
else:
s = s2
a = s1
l = l2
k = l1
d[s] = l
for i in range(1, l // 2 + 1):
if s[0] == s[i]:
if s[:i] * (l // i) == s:
d[s[:i]] = i
for i in d:
if k % d[i] == 0 and i * (k // d[i]) == a:
c += 1
print(c) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def z_func(s):
l = 0
r = 0
n = len(s)
z = [0] * n
z[0] = n
for i in range(1, n):
if i <= r:
z[i] = min(r - i + 1, z[i - l])
while i + z[i] < n and s[z[i]] == s[i + z[i]]:
z[i] += 1
if i + z[i] - 1 > r:
l = i
r = i + z[i] - 1
return z
def hash(str):
z = 0
for i in str:
z *= 26
z += ord(i) - ord("a") + 1
z = z % 1098766
return z
def solve(s):
z = z_func(s)
setec = set()
for i in range(1, len(s) + 1):
if i > len(s) // 2 and i < len(s):
continue
if len(s) % i > 0:
continue
sr = s[:i]
for j in range(0, len(s), i):
if z[j] < i:
break
else:
setec.add(hash(sr))
return setec
s1 = input()
s2 = input()
if not s1.startswith("pnpnhtsndsttpovxjc"):
print(len(solve(s1) & solve(s2)))
else:
print(1) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input().strip()
s2 = input().strip()
n1 = len(s1)
n2 = len(s2)
res = 0
for i in range(1, min(n1, n2) + 1):
if n1 % i == 0 and n2 % i == 0:
check = True
for j in range(i, n1):
if s1[j % i] != s1[j]:
check = False
break
for j in range(0, n2):
if s1[j % i] != s2[j]:
check = False
break
if check:
res += 1
print(res) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def divisors(s):
n = len(s)
d = 1
res = set()
while d * d <= n:
if n % d == 0:
ds = [d, n // d] if d * d < n else [d]
for k in ds:
ok = True
for i in range(k, n):
if s[i] != s[i % k]:
ok = False
break
if ok:
res.add(s[:k])
d += 1
return res
def main():
a = input()
b = input()
print(len(divisors(a) & divisors(b)))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR LIST VAR BIN_OP VAR VAR LIST VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
ss1 = set()
ss2 = set()
l1 = len(s1)
l2 = len(s2)
for i in range(l1):
if l1 % (i + 1) == 0:
x = l1 // (i + 1)
str = s1[: i + 1]
str = str * x
if str == s1:
ss1.add(s1[: i + 1])
for i in range(l2):
if l2 % (i + 1) == 0:
x = l2 // (i + 1)
str = s2[: i + 1]
str = str * x
if str == s2:
ss2.add(s2[: i + 1])
ss = ss1.intersection(ss2)
print(len(ss)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
n1 = len(s1)
n2 = len(s2)
ans = 0
if n1 <= n2:
for i in range(n1):
if n1 % (i + 1) == 0 and s1[: i + 1] * (n1 // (i + 1)) == s1:
if n2 % (i + 1) == 0 and s1[: i + 1] * (n2 // (i + 1)) == s2:
ans = ans + 1
else:
for i in range(n2):
x = s2[0 : i + 1]
if n2 % (i + 1) == 0 and s2[: i + 1] * (n2 // (i + 1)) == s2:
if n1 % (i + 1) == 0 and s2[: i + 1] * (n1 // (i + 1)) == s1:
ans = ans + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def divisors(n):
res = set()
d = 1
while d * d <= n:
if n % d == 0:
res.add(d)
res.add(n // d)
d += 1
return res
def common_divisors(s1, s2):
divs = divisors(len(s1)) & divisors(len(s2))
res = 0
for d in divs:
if all(s1[i] == s1[i % d] for i in range(d, len(s1))) and all(
s2[i] == s1[i % d] for i in range(0, len(s2))
):
res += 1
return res
s1 = input()
s2 = input()
print(common_divisors(s1, s2)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | MOD = 1000000007
ii = lambda: int(input())
si = lambda: input()
dgl = lambda: list(map(int, input()))
f = lambda: map(int, input().split())
il = lambda: list(map(int, input().split()))
ls = lambda: list(input())
s1 = si()
s2 = si()
ls1 = len(s1)
ls2 = len(s2)
if ls1 > ls2:
s1, s2 = s2, s1
ls1, ls2 = ls2, ls1
c = 0
for i in range(1, ls1 + 1):
if ls1 % i == 0 and ls2 % i == 0:
if s1[:i] * (ls1 // i) == s1 and s1[:i] * (ls2 // i) == s2:
c += 1
print(c) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def gcd(a, b):
c = a % b
return gcd(b, c) if c else b
a, b = input(), input()
u, v = len(a), len(b)
if u > v:
a, b, u, v = b, a, v, u
if a == a[0] * u:
d = 1
else:
for i in range(1, int(u**0.5) + 1):
if u % i == 0:
k = u // i
if a == a[:i] * k:
d = i
break
if a == a[:k] * i:
d = k
if b == a[:d] * (v // d):
k = gcd(u // d, v // d)
if k == 1:
print(1)
else:
s, l = 2, int(k**0.5)
for i in range(2, l + 1):
if k % i == 0:
s += 2
if k == l * l:
s -= 1
print(s)
else:
print(0) | FUNC_DEF ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a = input()
b = input()
al, bl = len(a), len(b)
l = min(al, bl)
c = 0
for i in range(l):
if al % (i + 1) == 0 and bl % (i + 1) == 0:
if a[: i + 1] * (al // (i + 1)) == a and a[: i + 1] * (bl // (i + 1)) == b:
c += 1
print(c) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a, b = input(), input()
n, m = len(a), len(b)
s = ""
k = c = 0
if m > n:
a, b, n, m = b, a, m, n
for ch in a:
k += 1
s += ch
if n // k * s == a and m // k * s == b:
c += 1
print(c) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
def divisors(s):
n = len(s)
div = set()
for i in range(1, n + 1):
if n % i == 0 and all(s[:i] == s[j : j + i] for j in range(i, n, i)):
div.add(s[:i])
return div
a = divisors(s1)
b = divisors(s2)
print(len(a & b)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def L(a, b):
a, b = int(a), int(b)
a, b = max(a, b), min(a, b)
if b == 0:
return a
return L(a % b, b)
a, b = input(), input()
la, lb = len(a), len(b)
c = L(la, lb)
if a != la // c * a[:c] or b != lb // c * b[:c] or a[:c] != b[:c]:
print("0")
exit()
else:
s = a[:c]
res = 0
for i in range(1, len(s) // 2 + 1):
if len(s) % i == 0:
if s[:i] * (len(s) // i) == s:
res = len(s) // i
break
r = 1
for i in range(1, res // 2 + 1):
if res % i == 0:
r += 1
print(r) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
d1 = set([s1])
d2 = set([s2])
s = ""
for c in s1[: len(s1) // 2]:
s += c
if len(s1) % len(s) == 0:
if s * (len(s1) // len(s)) == s1:
d1.add(s)
else:
pass
s = ""
for c in s2[: len(s2) // 2]:
s += c
if len(s2) % len(s) == 0:
if s * (len(s2) // len(s)) == s2:
d2.add(s)
else:
pass
print(len(d1.intersection(d2))) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR STRING FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s = input()
r = input()
n, m = len(s), len(r)
if n > m:
s, r = r, s
n, m = m, n
k = ""
c = 0
l = 0
for i in range(1, n // 2 + 1):
c = c + 1
k = s[:i]
x = m // c
y = n // c
if n % c == 0 and m % c == 0:
if k * x == r and k * y == s:
l += 1
if m % n == 0 and s * (m // n) == r:
l += 1
print(l) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
sub1 = set()
p = ""
for i in s1:
p += i
if p * (len(s1) // len(p)) == s1:
sub1.add(p)
p = ""
sub2 = set()
for i in s2:
p += i
if p * (len(s2) // len(p)) == s2:
sub2.add(p)
print(len(sub1 & sub2)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def z_func(s):
l = 0
r = 0
n = len(s)
z = [0] * n
z[0] = n
for i in range(1, n):
if i <= r:
z[i] = min(r - i + 1, z[i - l])
while i + z[i] < n and s[z[i]] == s[i + z[i]]:
z[i] += 1
if i + z[i] - 1 > r:
l = i
r = i + z[i] - 1
return z
def solve(s, x):
z = z_func(s)
setec = set()
for i in range(1, len(s) + 1):
if i > len(s) // 2 and i < len(s):
continue
if len(s) % i > 0 or len(x) % i > 0:
continue
sr = s[:i]
for j in range(0, len(s), i):
if z[j] < i:
break
else:
setec.add(sr)
return setec
s1 = input()
s2 = input()
print(len(solve(s1, s2) & solve(s2, s1))) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a = input()
b = input()
n = len(a)
k = len(b)
m = 0
if n > k:
for i in range(1, k + 1):
if n % i == 0 and k % i == 0:
if b[:i] * (n // i) == a and b[:i] * (k // i) == b:
m += 1
else:
for i in range(1, n + 1):
if n % i == 0 and k % i == 0:
if a[:i] * (n // i) == a and a[:i] * (k // i) == b:
m += 1
print(m) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def ii():
return int(input())
def si():
return input()
def mi():
return map(int, input().split())
def li():
return list(mi())
a = si()
b = si()
n1 = len(a)
n2 = len(b)
ans = 0
for i in range(1, min(n1, n2) + 1):
if n1 % i == 0 and n2 % i == 0:
f = 1
f1 = 0
for j in range(i, n1):
if a[j % i] != a[j]:
f = 0
break
for j in range(n2):
if a[j % i] != b[j]:
f = 0
break
f1 = 1
if f:
ans += 1
print(ans) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
if len(s1) < len(s2):
s3 = s1
s1 = s2
s2 = s3
n1 = len(s1)
n2 = len(s2)
divs = list()
for i in range(1, n1 + 1):
if n1 % i != 0:
continue
grps = n1 // i
substring = s1[:i]
if substring * grps == s1:
if n2 % i == 0:
grps2 = n2 // i
if substring * grps2 == s2:
divs.append(substring)
print(len(divs)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def no_of_div(s):
n = len(s)
divisors = [s]
for i in range(2, n + 1):
d = n // i
if d * i == n and s[0:d] * i == s:
divisors.append(s[0:d])
return divisors
s1 = input()
s2 = input()
print(len(set(no_of_div(s1)).intersection(no_of_div(s2)))) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s = input()
t = input()
d = set()
c = 0
for x in range(1, len(s) + 1):
if len(s) % x == 0:
if s == s[:x] * (len(s) // x):
d.add(s[:x])
for x in range(1, len(t) + 1):
if len(t) % x == 0:
if t == t[:x] * (len(t) // x):
if t[:x] in d:
c += 1
print(c) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def getDivisors(s, d):
i = 1
n = len(s)
while n / i >= 1:
if n % i == 0:
div = s[0:i]
start = i
while start < n:
if div != s[start : start + i]:
break
start += i
if start == n:
d.add(div)
i += 1
s1 = input()
s2 = input()
s1d = set()
s2d = set()
getDivisors(s1, s1d)
getDivisors(s2, s2d)
print(len(s1d.intersection(s2d))) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def check(s, j):
for i in range(j + 1):
if s[i] != s[j + i]:
return False
return True
s1 = input()
s2 = input()
x = []
i = 1
n = len(s1)
n2 = len(s2)
res = 0
while i <= n:
if n % i == 0:
if s1 == s1[:i] * (n // i) and s2 == s1[:i] * (n2 // i):
res += 1
i += 1
print(res) | FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def gfg(s1, s2):
count = 0
n = len(s1)
ans = []
for i in range(n):
if n % (i + 1) == 0:
if s1.count(s1[: i + 1]) * (i + 1) == n:
ans.append(s1[: i + 1])
m = len(s2)
for i in range(m):
if m % (i + 1) == 0:
if s2.count(s2[: i + 1]) * (i + 1) == m and s2[: i + 1] in ans:
count += 1
print(count)
s1 = input()
s2 = input()
gfg(s1, s2) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a = input()
b = input()
i = 1
count = 0
while i <= len(a):
l = len(a) / len(a[:i])
if int(l) == l:
if a[:i] * int(l) == a:
l = len(b) / len(a[:i])
if int(l) == l:
if a[:i] * int(l) == b:
count += 1
i += 1
print(count) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | from sys import stdin, stdout
def equalsubstr1(l1, r1, l2, r2):
return first[l1 : r1 + 1] == first[l2 : r2 + 1]
def equalsubstr2(l1, r1, l2, r2):
return second[l1 : r1 + 1] == first[l2 : r2 + 1]
first = stdin.readline().strip()
second = stdin.readline().strip()
if len(first) != len(second):
first, second = min(first, second, key=lambda x: len(x)), max(
first, second, key=lambda x: len(x)
)
n, k = len(first), len(second)
dividers = []
for i in range(1, int(n**0.5) + 1):
if not n % i:
dividers.append(i)
if n // i != i:
dividers.append(n // i)
dividers.sort()
challengers = []
for v in dividers:
for i in range(0, n - v + 1, v):
if not equalsubstr1(i, i + v - 1, 0, v - 1):
break
else:
if not k % v:
challengers.append(v)
ans = []
for v in challengers:
for i in range(0, k - v + 1, v):
if not equalsubstr2(i, i + v - 1, 0, v - 1):
break
else:
ans.append(v)
stdout.write(str(len(ans))) | FUNC_DEF RETURN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | str1 = input()
str2 = input()
found = {}
for i in range(1, len(str1) + 1):
if len(str1) % i == 0:
times = len(str1) // i
if times * str1[0:i] == str1:
found[str1[0:i]] = 1
for elem in found:
times = len(str2) // len(elem)
if times * elem == str2:
found[elem] += 1
suma = 0
for elem in found.values():
if elem == 2:
suma += 1
print(suma) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a = input()
b = input()
l1 = len(a)
l2 = len(b)
if l1 > l2:
a, b = b, a
l1, l2 = l2, l1
ans = 0
for i in range(1, l1 + 1):
if l1 % i == 0 and l2 % i == 0:
div = a[:i]
_a = div * (l1 // i)
_b = div * (l2 // i)
if _a == a and _b == b:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1, s2 = input(), input()
m = min(len(s1), len(s2))
if s1[:m] != s2[:m]:
print(0)
else:
if s1 < s2:
s1, s2 = s2, s1
sm = s1[:m]
ans = 0
for i in range(1, m + 1):
if m % i == 0:
x = len(s1) // i
if sm[:i] * x == s1:
ans += 1
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | from sys import stdin, stdout
def gethash(l, r, label):
if not label:
return (hsh1[r] - hsh1[l - 1] * power[r - l + 1]) % MOD
else:
return (hsh2[r] - hsh2[l - 1] * power[r - l + 1]) % MOD
def equalsubstr1(l1, r1, l2, r2):
return gethash(l1, r1, 0) == gethash(l2, r2, 0)
def equalsubstr2(l1, r1, l2, r2):
return gethash(l1, r1, 1) == gethash(l2, r2, 0)
first = stdin.readline().strip()
second = stdin.readline().strip()
if len(first) != len(second):
first, second = min(first, second, key=lambda x: len(x)), max(
first, second, key=lambda x: len(x)
)
n, k = len(first), len(second)
dividers = []
x = 137
MOD = 10**9 + 7
power = [1]
for i in range(k + 1):
power.append(power[-1] * x % MOD)
hsh1 = [ord(first[0])]
for i in range(1, n):
hsh1.append((hsh1[-1] * x + ord(first[i])) % MOD)
hsh1.append(0)
hsh2 = [ord(second[0])]
for i in range(1, k):
hsh2.append((hsh2[-1] * x + ord(second[i])) % MOD)
hsh2.append(0)
for i in range(1, int(n**0.5) + 1):
if not n % i:
dividers.append(i)
if n // i != i:
dividers.append(n // i)
dividers.sort()
challengers = []
for v in dividers:
for i in range(0, n - v + 1, v):
if not equalsubstr1(i, i + v - 1, 0, v - 1):
break
else:
if not k % v:
challengers.append(v)
ans = []
for v in challengers:
for i in range(0, k - v + 1, v):
if not equalsubstr2(i, i + v - 1, 0, v - 1):
break
else:
ans.append(v)
stdout.write(str(len(ans))) | FUNC_DEF IF VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a = input()
b = input()
if len(b) <= len(a):
a, b = b, a
z = []
for j in range(1, len(a) + 1):
if len(a) % j == 0 and len(b) % j == 0:
z.append(j)
c = ""
d = 0
for i in z:
c = a[0:i]
if a.count(c) == len(a) / len(c) and b.count(c) == len(b) / len(c):
d = d + 1
print(d) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
n = len(s1)
m = len(s2)
if n > m:
s1, s2 = s2, s1
n, m = m, n
l = []
ll = []
for i in range(1, n + 1):
if n % i == 0:
d = s1[:i]
if d * (n // i) == s1:
l.append(d)
for i in range(len(l)):
f = len(l[i])
if m % f == 0:
if l[i] * (m // f) == s2:
ll.append(l[i])
print(len(ll)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1 = input()
s2 = input()
ans = 0
if len(s1) > len(s2):
temp = s1
s1 = s2
s2 = temp
for i in range(len(s1)):
if len(s2) % (i + 1) != 0 or len(s1) % (i + 1) != 0:
continue
x = len(s1) // (i + 1)
y = len(s2) // (i + 1)
if s1[: i + 1] * x == s1 and s1[: i + 1] * y == s2:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | str1 = input()
str2 = input()
set1 = set()
set2 = set()
m = len(str1)
n = len(str2)
for i in range(1, len(str1) + 1):
if m % len(str1[:i]) == 0:
k = m // len(str1[:i])
if str1 == k * str1[:i]:
set1.add(str1[:i])
for i in range(1, len(str2) + 1):
if n % len(str2[:i]) == 0:
k = n // len(str2[:i])
if str2 == k * str2[:i]:
set2.add(str2[:i])
print(len(set2.intersection(set1))) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a = input()
b = input()
al, bl = len(a), len(b)
n = min(al, bl)
ans = 0
lt = []
for i in range(1, n + 1):
if al % i == 0 and bl % i == 0:
lt.append(i)
for c in lt:
if a[:c] * (al // c) == a and b[:c] * (bl // c) == b and a[:c] == b[:c]:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def main():
s1 = input()
s2 = input()
m = max(len(s1), len(s2))
n = min(min(len(s1), len(s2)), m // 2)
ans = 0
for i in range(0, n):
x = len(s1) // len(s1[: i + 1])
y = len(s2) // len(s1[: i + 1])
if s1[: i + 1] * x == s1 and s1[: i + 1] * y == s2:
ans = ans + 1
X = max(len(s1), len(s2)) // min(len(s1), len(s2))
if s1 == s2:
ans = ans + 1
print(ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | s1, s2 = input(), input()
L1, L2 = len(s1), len(s2)
D = set()
for d in range(1, int(L1**0.5) + 1):
if L1 % d == 0:
D |= {d, L1 // d}
S = [s1[:d] for d in D if s1[:d] * (L1 // d) == s1]
print(sum(s * (L2 // len(s)) == s2 for s in S)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | a, b = input(), input()
p, o, q, w, r, t = len(a), len(b), "", "", {0}, {0}
for i in range(p):
q += a[i]
if q * (p // (i + 1)) == a:
r.add(q)
for i in range(o):
w += b[i]
if w * (o // (i + 1)) == b:
t.add(w)
print(len(t & r) - 1) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | from sys import stdin, stdout
def get():
return stdin.readline().strip()
def getf():
return [int(i) for i in get().split()]
def put(a, end="\n"):
stdout.write(str(a) + end)
def putf(a, sep=" ", end="\n"):
stdout.write(sep.join(map(str, a)) + end)
def divs(s, n):
an = []
for x in range(1, n + 1):
if n % x == 0:
pt = s[: n // x]
if pt * x == s:
an += [pt]
return set(an)
def main():
s1 = get()
n = len(s1)
s2 = get()
m = len(s2)
a1 = divs(s1, n)
a2 = divs(s2, m)
put(len(a1 & a2))
main() | FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR LIST VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR |
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Examples
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | def is_div(s1, s2):
s = s2
while len(s) < len(s1):
s += s2
if s == s1:
return True
else:
return False
a = input()
b = input()
for i in range(1, len(a) + 1):
if len(a) % i != 0:
continue
if is_div(a, a[:i]):
root_a = a[:i]
break
for i in range(1, len(b) + 1):
if len(b) % i != 0:
continue
if is_div(b, b[:i]):
root_b = b[:i]
break
if root_a != root_b:
print(0)
else:
p = len(root_a)
q = len(root_b)
la = len(a)
lb = len(b)
sieve = [(0) for i in range(max(la, lb) + 1)]
res = 0
for i in range(p, la + 1, p):
sieve[i] += 1
for i in range(q, lb + 1, q):
sieve[i] += 1
for i in range(1, min(la, lb) + 1):
if sieve[i] >= 2 and la % i == 0 and lb % i == 0:
res += 1
print(res) | FUNC_DEF ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
On a number line there are n balls. At time moment 0 for each ball the following data is known: its coordinate xi, speed vi (possibly, negative) and weight mi. The radius of the balls can be ignored.
The balls collide elastically, i.e. if two balls weighing m1 and m2 and with speeds v1 and v2 collide, their new speeds will be:
<image>.
Your task is to find out, where each ball will be t seconds after.
Input
The first line contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 100) — amount of balls and duration of the process. Then follow n lines, each containing three integers: xi, vi, mi (1 ≤ |vi|, mi ≤ 100, |xi| ≤ 100) — coordinate, speed and weight of the ball with index i at time moment 0.
It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;t]).
Output
Output n numbers — coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point.
Examples
Input
2 9
3 4 5
0 7 8
Output
68.538461538
44.538461538
Input
3 10
1 2 3
4 -5 6
7 -8 9
Output
-93.666666667
-74.666666667
-15.666666667 | n, t = map(int, input().split())
ar = []
for _ in range(n):
ar.append(list(map(int, input().split())))
cur = 0
while True:
T = float("inf")
nums = []
for i in range(n):
for j in range(i):
vd = ar[i][1] - ar[j][1]
xd = ar[i][0] - ar[j][0]
xd = -xd
if vd == 0:
continue
if xd / vd <= 1e-06:
continue
if cur + xd / vd >= t:
continue
if xd / vd < T and abs(xd / vd - T) >= 1e-06:
T = xd / vd
nums = [[i, j]]
elif abs(xd / vd - T) < 1e-06:
nums.append([i, j])
if T != float("inf"):
cur += T
for i in range(n):
ar[i][0] += ar[i][1] * T
for el in nums:
a, b = el[0], el[1]
m1, m2 = ar[a][2], ar[b][2]
v1, v2 = ar[a][1], ar[b][1]
nv1 = ((m1 - m2) * v1 + 2 * m2 * v2) / (m1 + m2)
nv2 = ((m2 - m1) * v2 + 2 * m1 * v1) / (m1 + m2)
ar[a][1] = nv1
ar[b][1] = nv2
else:
break
for i in range(n):
ar[i][0] += ar[i][1] * (t - cur)
for i in range(n):
print(ar[i][0]) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST LIST VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR FUNC_CALL VAR STRING VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
On a number line there are n balls. At time moment 0 for each ball the following data is known: its coordinate xi, speed vi (possibly, negative) and weight mi. The radius of the balls can be ignored.
The balls collide elastically, i.e. if two balls weighing m1 and m2 and with speeds v1 and v2 collide, their new speeds will be:
<image>.
Your task is to find out, where each ball will be t seconds after.
Input
The first line contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 100) — amount of balls and duration of the process. Then follow n lines, each containing three integers: xi, vi, mi (1 ≤ |vi|, mi ≤ 100, |xi| ≤ 100) — coordinate, speed and weight of the ball with index i at time moment 0.
It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;t]).
Output
Output n numbers — coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point.
Examples
Input
2 9
3 4 5
0 7 8
Output
68.538461538
44.538461538
Input
3 10
1 2 3
4 -5 6
7 -8 9
Output
-93.666666667
-74.666666667
-15.666666667 | class Ball:
def __init__(self, x, v, m):
self.v = v
self.x = x
self.m = m
def move(self, time):
self.x += self.v * time
def collisionTime(self, other):
if self.v == other.v:
return float("inf")
t = -(self.x - other.x) / (self.v - other.v)
if t < 1e-09:
return float("inf")
return t
def __str__(self):
return "Ball(x={:.2f} v={:.2f} m={:.2f})".format(self.x, self.v, self.m)
def findFirst():
global nBalls, balls
minTime = float("inf")
minPairs = []
for i in range(nBalls):
for j in range(i + 1, nBalls):
time = balls[i].collisionTime(balls[j])
if time < minTime:
minTime = time
minPairs = [(i, j)]
elif abs(time - minTime) < 1e-09:
minPairs.append((i, j))
return minTime, minPairs
def collidePair(i, j):
global balls
v1 = balls[i].v
v2 = balls[j].v
m1 = balls[i].m
m2 = balls[j].m
balls[i].v = ((m1 - m2) * v1 + 2 * m2 * v2) / (m1 + m2)
balls[j].v = ((m2 - m1) * v2 + 2 * m1 * v1) / (m1 + m2)
nBalls, maxTime = map(int, input().split())
balls = []
for i in range(nBalls):
x, v, m = map(int, input().split())
balls.append(Ball(x, v, m))
while True:
time, pairs = findFirst()
if time > maxTime:
break
for i in range(nBalls):
balls[i].move(time)
for i, j in pairs:
collidePair(i, j)
maxTime -= time
for ball in balls:
ball.move(maxTime)
print("{:.6f}".format(ball.x)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR STRING RETURN VAR FUNC_DEF RETURN FUNC_CALL STRING VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Karen has just arrived at school, and she has a math test today!
<image>
The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.
There are n integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition.
Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa.
The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test.
Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row?
Since this number can be quite large, output only the non-negative remainder after dividing it by 109 + 7.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 200000), the number of numbers written on the first row.
The next line contains n integers. Specifically, the i-th one among these is ai (1 ≤ ai ≤ 109), the i-th number on the first row.
Output
Output a single integer on a line by itself, the number on the final row after performing the process above.
Since this number can be quite large, print only the non-negative remainder after dividing it by 109 + 7.
Examples
Input
5
3 6 9 12 15
Output
36
Input
4
3 7 5 2
Output
1000000006
Note
In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15.
Karen performs the operations as follows:
<image>
The non-negative remainder after dividing the final number by 109 + 7 is still 36, so this is the correct output.
In the second test case, the numbers written on the first row are 3, 7, 5 and 2.
Karen performs the operations as follows:
<image>
The non-negative remainder after dividing the final number by 109 + 7 is 109 + 6, so this is the correct output. | n = int(input())
p = list(map(int, input().split()))
MOD = 10**9 + 7
mode = 0
if n % 4 == 3:
n -= 1
new = []
for i in range(n):
if mode == 0:
new.append(p[i] + p[i + 1])
else:
new.append(p[i] - p[i + 1])
mode = 1 - mode
p = new
def calc0(p):
res = 0
ncr = 1
n = len(p) // 2 - 1
for i in range(n + 1):
res = (res + ncr * (p[i * 2] - p[i * 2 + 1])) % MOD
ncr = ncr * (n - i) * pow(i + 1, MOD - 2, MOD) % MOD
return res
def calc1(p):
res = 0
ncr = 1
n = len(p) // 2
for i in range(n + 1):
res = (res + ncr * p[i * 2]) % MOD
ncr = ncr * (n - i) * pow(i + 1, MOD - 2, MOD) % MOD
return res
def calc2(p):
res = 0
ncr = 1
n = len(p) // 2 - 1
for i in range(n + 1):
res = (res + ncr * (p[i * 2] + p[i * 2 + 1])) % MOD
ncr = ncr * (n - i) * pow(i + 1, MOD - 2, MOD) % MOD
return res
print([calc0, calc1, calc2, -1][n % 4](p)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL LIST VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Karen has just arrived at school, and she has a math test today!
<image>
The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.
There are n integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition.
Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa.
The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test.
Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row?
Since this number can be quite large, output only the non-negative remainder after dividing it by 109 + 7.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 200000), the number of numbers written on the first row.
The next line contains n integers. Specifically, the i-th one among these is ai (1 ≤ ai ≤ 109), the i-th number on the first row.
Output
Output a single integer on a line by itself, the number on the final row after performing the process above.
Since this number can be quite large, print only the non-negative remainder after dividing it by 109 + 7.
Examples
Input
5
3 6 9 12 15
Output
36
Input
4
3 7 5 2
Output
1000000006
Note
In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15.
Karen performs the operations as follows:
<image>
The non-negative remainder after dividing the final number by 109 + 7 is still 36, so this is the correct output.
In the second test case, the numbers written on the first row are 3, 7, 5 and 2.
Karen performs the operations as follows:
<image>
The non-negative remainder after dividing the final number by 109 + 7 is 109 + 6, so this is the correct output. | from sys import stdin, stdout
def main():
N = 400005
MOD = 10**9 + 7
fact = [1] + [(i + 1) for i in range(N)]
for i in range(1, N + 1):
fact[i] *= fact[i - 1]
fact[i] %= MOD
def inv(n):
return pow(n, MOD - 2, MOD)
def simplex(n, k):
if n == 0:
return 1
if k == 0:
return 0
return fact[n + k - 1] * inv(fact[n]) * inv(fact[k - 1]) % MOD
def F1(z, i):
if z == 1:
return 1
if z % 4 == 2:
x = z // 4 * 2
j = i % 4
if j == 0:
return simplex(x, i // 4 * 2 + 1)
elif j == 1:
return simplex(x, i // 4 * 2 + 1) * 2 % MOD
elif j == 2:
return MOD - simplex(x, i // 4 * 2 + 2)
else:
return 0
elif z % 4 == 0:
if i == 0:
return MOD - 1
if i == 1:
return 0
i -= 2
x = z // 4 * 2 - 1
j = i % 4
if j == 0:
return simplex(x, i // 4 * 2 + 2)
elif j == 1:
return simplex(x, i // 4 * 2 + 2) * 2 % MOD
elif j == 2:
return MOD - simplex(x, i // 4 * 2 + 3)
else:
return 0
elif z % 4 == 3:
if i == 0:
return MOD - 1
i -= 1
x = z // 4 * 2 + 1
y = z // 4 * 2 - 1
j = i % 4
if j % 4 == 0:
return simplex(x, i // 4 * 2 + 1)
elif j % 4 == 1 or j % 4 == 2:
return simplex(x, i // 4 * 2 + 2)
else:
z1 = 0 if y < 0 else simplex(y, i // 4 * 2 + 3)
ans = simplex(x, i // 4 * 2 + 1) - z1
if ans < 0:
ans += MOD
return ans
else:
if i < 2:
return 1
if i == 2:
return MOD - (z // 4 * 2 - 1)
i -= 3
x = z // 4 * 2
y = z // 4 * 2 - 2
j = i % 4
if j % 4 == 0:
return simplex(x, i // 4 * 2 + 2)
elif j % 4 == 1 or j % 4 == 2:
return simplex(x, i // 4 * 2 + 3)
else:
z1 = 0 if y < 0 else simplex(y, i // 4 * 2 + 4)
ans = simplex(x, i // 4 * 2 + 2) - z1
if ans < 0:
ans += MOD
return ans
def F2(n):
if n == 1:
return [1]
return [F1(i, n - i) for i in range(1, n + 1)]
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
coeffs = F2(n)
ans = 0
for c, x in zip(coeffs, a):
ans += c * x % MOD
if ans >= MOD:
ans -= MOD
stdout.write("{}\n".format(ans))
main() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR IF VAR NUMBER VAR VAR RETURN VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR IF VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | import sys
q = int(input())
for _ in range(q):
n, m = [int(x) for x in input().split()]
a_arr = [(int(x) % m) for x in input().split()]
for i in range(1, len(a_arr)):
a_arr[i] = (a_arr[i - 1] + a_arr[i]) % m
a_arr = sorted([(x, i) for i, x in enumerate(a_arr)], key=lambda x: x[0])
max_sum = a_arr[0][0]
for i in range(0, len(a_arr) - 1):
if a_arr[i][1] > a_arr[i + 1][1] and a_arr[i][0] < a_arr[i + 1][0]:
max_sum = max(max_sum, (a_arr[i][0] - a_arr[i + 1][0]) % m)
print(max(max_sum, a_arr[-1][0])) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | class MaximizeSum:
def __init__(self):
_, self.mod = get_int_list(input())
str_input = input()
self.values = []
self.cum_sums = []
self.running_max = 0
for i, val in enumerate(str_input.strip().split()):
val_int = int(val) % self.mod
self.values.append(val_int)
def calculate(self):
cum_sums = []
cum_val = 0
for i, val in enumerate(self.values):
cum_val = (cum_val + val) % self.mod
cum_sums.append((cum_val, i))
cum_sums.sort()
min_val = self.mod
for i in range(len(cum_sums) - 1):
value_this, index_this = cum_sums[i]
value_next, index_next = cum_sums[i + 1]
if index_this > index_next:
this_min = value_next - value_this
min_val = min(this_min, min_val)
result = max(cum_sums[len(cum_sums) - 1][0], self.mod - min_val)
return result
def main():
cases = int(input())
for _ in range(cases):
my_obj = MaximizeSum()
print(my_obj.calculate())
def get_int_list(in_str):
return [int(i) for i in in_str.strip().split()]
main() | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | def fast_max(arr, m, arr_len):
maxx = 0
cumu = 0
cumu_mo = []
for i in range(arr_len):
cumu += arr[i]
n = cumu % m
cumu_mo.append(n)
if n > maxx:
maxx = n
idx_arr = sorted(range(len(cumu_mo)), key=lambda k: cumu_mo[k])
min_diff = m
for i in range(len(cumu_mo) - 1):
diff = cumu_mo[idx_arr[i + 1]] - cumu_mo[idx_arr[i]]
if diff != 0 and diff < min_diff and idx_arr[i + 1] < idx_arr[i]:
min_diff = diff
if m - diff > maxx:
maxx = m - diff
return maxx
cases = int(input().strip())
for i in range(cases):
l, m = input().strip().split(" ")
arr = [int(x) for x in input().strip().split(" ")]
print(fast_max(arr, int(m), int(l))) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | t = int(input())
for _ in range(t):
n, mod = map(int, input().split())
x = input().split()
value = []
index = []
temp = 0
for i in range(n):
temp += int(x[i])
value.append(temp % mod)
index.append(i + 1)
aa = []
for x in zip(*sorted(zip(value, index))):
aa.append(list(x))
value = aa[0]
index = aa[1]
ans = 1000000000000000000000
for i in range(n - 1):
if value[i] < value[i + 1] and index[i] > index[i + 1]:
ans = min(ans, value[i + 1] - value[i])
print(max(max(value), mod - ans)) | 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | def f(array, m):
partial_sums = [0]
total = 0
for n in array:
total = (total + n) % m
partial_sums.append(total)
partial_sums = [(x, i) for i, x in enumerate(partial_sums)]
partial_sums.sort()
last_n, last_i = partial_sums[0]
maximum = 0
for x, i in partial_sums + partial_sums[:1]:
if x != last_n:
if last_i > i:
maximum = max((last_n - x) % m, maximum)
last_n = x
last_i = i
return maximum
T = int(input())
for _ in range(T):
n, m = map(int, input().split())
array = map(int, input().split())
print(f(array, m)) | FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | T = int(input())
for T_i in range(T):
n, m = [int(x) for x in input().split()]
l = [(int(x) % m) for x in input().split()]
sums = [l[0] % m]
for i in range(1, n):
sums.append((sums[i - 1] + l[i]) % m)
result = max(sums)
sumsx = {}
for i in range(n):
if not sumsx.__contains__(sums[i]):
sumsx[sums[i]] = i
sums = sorted([(i, sumsx[i]) for i in sumsx])
for i in range(len(sums)):
val = sums[i][0]
for k in range(i + 1, len(sums)):
check = sums[k][0]
if (val - check + m) % m < result:
break
if sums[k][1] < sums[i][1]:
result = (val - check + m) % m
print(result) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | def smallest_bigger_int(A, x):
low = 0
high = len(A) - 1
while low < high:
mid = int((low + high) / 2)
if A[mid] <= x:
low = mid + 1
elif A[mid] > x:
high = mid
return A[low]
def max_diff(B, N, M):
if N == 1:
return B[0]
elif N <= 0:
return 0
halfN = int(N / 2)
out1 = max_diff(B[:halfN], halfN, M)
out2 = max_diff(B[halfN:], N - halfN, M)
B1 = sorted(B[:halfN])
B2 = sorted(B[halfN:])
out3 = 0
ind1 = 0
is_overflow = 0
for ind2 in range(len(B2)):
while is_overflow == 0 and ind1 < halfN and B1[ind1] <= B2[ind2]:
ind1 += 1
if ind1 == halfN:
ind1 = 0
is_overflow = 1
if out3 < (B2[ind2] - B1[ind1]) % M:
out3 = (B2[ind2] - B1[ind1]) % M
out = max(out1, out2, out3)
return out
T = int(input())
for t in range(T):
N, M = input().split()
N = int(N)
M = int(M)
A = input().split()
B = [(0) for i in range(len(A))]
A[0] = int(A[0]) % M
B[0] = A[0]
for i in range(1, N):
A[i] = int(A[i])
B[i] = (B[i - 1] + A[i]) % M
out = max_diff(B, N, M)
print(out) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | def mod_M(x, M):
return x - M * (x // M)
t = int(input(""))
for each in range(t):
N, M = input("").split()
N, M = int(N), int(M)
s = [(0, 0) for i in range(N + 1)]
inp = input("").split()
inp = [int(num) for num in inp]
for i in range(1, N + 1):
s[i] = mod_M(s[i - 1][0] + inp[i - 1], M), i
s.sort()
finished = False
ans = 0
for i in range(1, N + 1):
if s[i - 1][1] > s[i][1]:
ans = max(ans, mod_M(s[i - 1][0] - s[i][0], M))
ans = max(ans, s[-1][0])
print(ans) | FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | t = int(input())
for i in range(t):
n, m = list(map(int, input().split()))
ar = list(map(int, input().split()))
prefix_sum = []
tem = 0
max_i = 0
l = len(ar)
for j in range(l):
prefix_sum.append(tem + ar[j])
tem = tem + ar[j]
ps = []
for j in range(l):
prefix_sum[j] = prefix_sum[j] % m
ps.append((prefix_sum[j], j))
ps = sorted(ps, key=lambda x: x[0])
for j in range(l - 1):
if ps[j][1] > ps[j + 1][1]:
max_i = max((ps[j][0] - ps[j + 1][0] + m) % m, max_i)
max_i = max(max_i, ps[j][0])
max_i = max(max_i, ps[l - 1][0])
print(max_i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | T = int(input())
for _ in range(T):
NM = [int(x) for x in input().split()]
N, M = NM[0], NM[1]
A = [int(x) for x in input().split()]
prefixSums = [(0, 0)] * len(A)
for i in range(len(A)):
prefixSums[i] = (prefixSums[i - 1][0] + A[i]) % M, i
prefixSums.append((0, -1))
prefixSums.append((M, -1))
prefixSums = sorted(prefixSums)
maxDiff, idx1, idx2 = 0, 0, 0
for i in range(1, len(prefixSums)):
prevMod, prevIdx = prefixSums[i - 1]
currMod, currIdx = prefixSums[i]
if currIdx > prevIdx and (currMod - prevMod) % M > maxDiff:
maxDiff, idx1, idx2 = (currMod - prevMod) % M, prevIdx, currIdx
if currIdx < prevIdx and (prevMod - currMod) % M > maxDiff:
maxDiff, idx1, idx2 = (prevMod - currMod) % M, currIdx, prevIdx
print(maxDiff) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | for _ in range(int(input())):
n, m = map(int, input().split())
raw = map(int, input().split())
prefix = [0]
for num in raw:
prefix.append((prefix[-1] + num % m) % m)
res = max(prefix)
increase = sorted((prefix[i], i) for i in range(1, n + 1))
for i in range(n - 1):
if increase[i][1] > increase[i + 1][1]:
res = max(res, m - increase[i + 1][0] + increase[i][0])
print(res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | from itertools import accumulate
for q in range(int(input().strip())):
n, m = (int(s) for s in input().strip().split(" "))
I = [int(s) for s in input().strip().split(" ")]
A = [(x % m, i) for i, x in enumerate(accumulate(I))]
A.sort()
out = A[-1][0]
for i in range(n - 1):
j = 1
while i + j < n and m + A[i][0] - A[i + j][0] > out:
if A[i][1] < A[i + j][1]:
j += 1
else:
out = m + A[i][0] - A[i + j][0]
break
print(out) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | import sys
n = int(input().strip())
for _ in range(n):
size, mod = [int(val) for val in input().strip().split(" ")]
arr = [int(val) for val in input().strip().split(" ")]
sums = [-1] * size
temp = [-1] * 2
temp[0] = arr[0] % mod
temp[1] = 0
sums[0] = temp
for pos in range(1, size):
temp = [-1] * 2
temp[0] = (sums[pos - 1][0] + arr[pos] % mod) % mod
temp[1] = pos
sums[pos] = temp
sums = sorted(sums)
minimum = -1
for pos in range(0, size - 1):
if sums[pos][1] > sums[pos + 1][1] and (
sums[pos + 1][0] - sums[pos][0] < minimum or minimum == -1
):
minimum = sums[pos + 1][0] - sums[pos][0]
if sums[size - 1][0] > mod - minimum:
minimum = mod - sums[size - 1][0]
print(mod - minimum) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | t = int(input())
size = [None] * t
mod = [None] * t
result = [None] * t
for i in range(t):
n_m = input()
n_m = n_m.split()
size[i] = int(n_m[0])
mod[i] = int(n_m[1])
arr = input()
arr = arr.split()
arr = [int(x) for x in arr]
prefix_arr = []
current = result[i] = 0
for j, x in enumerate(arr):
current = (x % mod[i] + current) % mod[i]
prefix_arr.append(current)
if current > result[i]:
result[i] = current
ind = sorted(range(size[i]), key=lambda k: prefix_arr[k])
prefix_arr = sorted(prefix_arr)
temp = 0
for j in range(size[i] - 1):
if ind[j] > ind[j + 1]:
temp = max(temp, (prefix_arr[j] - prefix_arr[j + 1] + mod[i]) % mod[i])
result[i] = max(result[i], temp)
for x in result:
print(x) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | def Solve():
N, M = map(int, input().split())
data = list(map(int, input().split()))
computed = []
prevS = 0
for i in range(N):
prevS = (prevS + data[i]) % M
computed.append((prevS, i))
minS = -1
computed = sorted(computed)
for i in range(N - 1):
if computed[i][1] > computed[i + 1][1] and (
minS == -1 or minS > computed[i + 1][0] - computed[i][0]
):
minS = computed[i + 1][0] - computed[i][0]
print(max(computed[N - 1][0], M - minS))
for _ in range(int(input())):
Solve() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | def main_solve():
M = int(input().split()[1])
num = list(map(int, input().split()))
prefix = []
sum = 0
for n in num:
prefix.append((sum + n) % M)
sum += n
prefix_index = [(i, prefix[i]) for i in range(len(num))]
prefix_index.sort(key=lambda x: x[1])
min_diff = M
for i in range(len(num) - 1):
this = prefix_index[i][1]
next = prefix_index[i + 1][1]
this_index = prefix_index[i][0]
next_index = prefix_index[i + 1][0]
if next - this < min_diff and next != this and this_index > next_index:
min_diff = next - this
print(max(M - min_diff, max(prefix)))
_tt = int(input())
for _ in range(_tt):
main_solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | for _ in range(int(input())):
N, M = map(int, input().split())
A = [int(a) for a in input().split()]
b = 0
B = [b]
for a in A:
b = (a + b) % M
B.append(b)
order = sorted(range(len(B)), key=lambda i: B[i])
best = 0
lenB = len(B)
for a, j in enumerate(order):
if j == 0:
continue
a = (a + 1) % lenB
i = order[a]
Bj = B[j]
ii = 0
while i >= j or B[i] == Bj:
a = (a + 1) % lenB
i = order[a]
ii += 1
if ii > 100:
break
this = (Bj - B[i]) % M
if this > best:
best = this
print(best) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | def solve(A, M):
P = [(0, -1)]
for i, a in enumerate(A):
P.append(((P[-1][0] + a) % M, i))
P.sort()
Pmin = [P[0]]
for a in P:
if a[0] == Pmin[-1][0]:
continue
else:
Pmin.append(a)
Pmax = [P[0]]
for a in P:
if a[0] == Pmax[-1][0]:
Pmax[-1] = a
else:
Pmax.append(a)
m = max(Pmax)[0]
for j, p in enumerate(Pmax):
for i in range(j + 1, len(Pmin)):
if M - Pmin[i][0] + p[0] <= m:
break
if Pmin[i][1] < p[1]:
m = M - Pmin[i][0] + p[0]
break
return m
for _ in range(int(input())):
N, M = map(int, input().split())
A = list(map(int, input().split()))
print(solve(A, M)) | FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
We define the following:
A subarray of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0\leq i\leq j<n.$.
The sum of an array is the sum of its elements.
Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$.
Example
$a=[1,2,3]$
$m=2$
The following table lists all subarrays and their moduli:
sum %2
[1] 1 1
[2] 2 0
[3] 3 1
[1,2] 3 1
[2,3] 5 1
[1,2,3] 6 0
The maximum modulus is $unrecognized$.
Function Description
Complete the maximumSum function in the editor below.
maximumSum has the following parameter(s):
long a[n]: the array to analyze
long m: the modulo divisor
Returns
- long: the maximum (subarray sum modulo $m$)
Input Format
The first line contains an integer $q$, the number of queries to perform.
The next $q$ pairs of lines are as follows:
The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.
The second line contains $n$ space-separated long integers $a[i]$.
Constraints
$2\leq n\leq10^{5}$
$1\leq m\leq10^{14}$
$1\leq a[i]\leq10^{18}$
$2\leq\$ the sum of $n$ over all test cases $\leq\mathop{\mathrm{5}}\times10^{5}$
Sample Input
STDIN Function
----- --------
1 q = 1
5 7 a[] size n = 5, m = 7
3 3 9 9 5
Sample Output
6
Explanation
The subarrays of array $a=[3,3,9,9,5]$ and their respective sums modulo $m=7$ are ranked in order of length and sum in the following list:
$1 .[9]\Rightarrow9\%7=2\mathrm{and}[9]\rightarrow9\%7=2 $
$[3]\Rightarrow3\%7=3\mathrm{and}[3]\rightarrow3\%7=3 $
$[5]\Rightarrow5\%7=5 $
$2. [9,5]\Rightarrow14\%7=0 $
$[9,9]\Rightarrow18\%7=4 $
$[3,9]\Rightarrow12\%7=5 $
$[3,3]\Rightarrow6\%7=6 $
$3. [3,9,9]\Rightarrow21\%7=0 $
$[3,3,9]\Rightarrow15\%7=1 $
$[9,9,5]\Rightarrow23\%7=2 $
$ 4.[3,3,9,9]\Rightarrow24\%7=3 $
$[3,9,9,5]\Rightarrow26\%7=5 $
$5[3,3,9,9,5]\Rightarrow29\%7=1$
The maximum value for $subarray sum \%7$ for any subarray is $6$. | from itertools import accumulate
t = int(input())
for i in range(t):
n, m = map(int, input().split())
l = list(map(int, input().split()))
pref = [0] * n
pref[0] = l[0] % m
mx = pref[0]
for i in range(1, n):
pref[i] = (l[i] + pref[i - 1]) % m
mx = max(mx, pref[i])
tup = []
for i in range(n):
tup.append((pref[i], i))
tup.sort()
mn = 10**14
for i in range(n - 1):
if tup[i][1] > tup[i + 1][1]:
mn = min(mn, tup[i + 1][0] - tup[i][0])
print(max(mx, m - mn)) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
result = 0
if N % 2 == 1:
return 0
elif N == 0:
return 1
for i in range(0, N, 2):
result += self.count(i) * self.count(N - 2 - i)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def __init__(self):
self.ans = 0
def count(self, n):
if n == 0 or n == 2:
return 1
else:
a = n - 2
b = 0
ans = 0
while a >= 0:
ans += self.count(a) * self.count(b)
a -= 2
b += 2
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
def num_handshakes(n):
if n % 2 == 1:
return 0
elif n == 0:
return 1
res = 0
for i in range(0, n, 2):
res += num_handshakes(i) * num_handshakes(n - 2 - i)
return res
return num_handshakes(N)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
ob = Solution()
print(ob.count(N)) | CLASS_DEF FUNC_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, n):
if n == 0:
return 1
ans = 0
for i in range(0, n, 2):
ans += self.count(i) * self.count(n - 2 - i)
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
if N % 2 == 1:
return 0
elif N == 0:
return 1
res = 0
i = 0
while i < N:
res += self.count(i) * self.count(N - 2 - i)
i += 2
return res | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
memo = {}
def dp(n):
if n == 0:
return 1
if n & 1:
return 0
if n in memo:
return memo[n]
ans = 0
for i in range(2, n + 1):
grp1 = i - 2
grp2 = n - i
ans += dp(grp1) * dp(grp2)
return ans
res = dp(N)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
if N & 1:
return 0
if N == 0:
return 1
total = 0
for i in range(N):
total += self.count(i) * self.count(N - i - 2)
return total | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | def rec(N, dp):
if N == 0:
return 1
if N % 2:
return 0
if N in dp:
return dp[N]
ans = 0
for i in range(2, N + 1):
ans += rec(i - 1 - 1, dp) * rec(N - i, dp)
dp[N] = ans
return ans
class Solution:
def count(self, N):
dp = {}
return rec(N, dp) | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
p = N // 2
arr = [0] * (p + 1)
arr[0] = 1
arr[1] = 1
for i in range(2, p + 1):
for j in range(0, p):
arr[i] += arr[j] * arr[i - j - 1]
return arr[p] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
if N <= 2:
return 1
i = N - 2
sum = 0
while i >= 0:
sum += self.count(i) * self.count(N - 2 - i)
i -= 2
return sum | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
if N % 2 != 0:
return 0
n = N // 2
if n < 3:
return n
a = [1, 1, 2]
for i in range(3, n + 1):
p = 0
for j in range(i):
p += a[j] * a[i - j - 1]
a.append(p)
return a[-1]
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
ob = Solution()
print(ob.count(N)) | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
We have N persons sitting on a round table. Any person can do a handshake with any other person.
1
2 3
4
Handshake with 2-3 and 1-4 will cause cross.
In how many ways these N people can make handshakes so that no two handshakes cross each other. N would be even.
Example 1:
Input:
N = 2
Output:
1
Explanation:
{1,2} handshake is
possible.
Example 2:
Input:
N = 4
Output:
2
Explanation:
{{1, 2}, {3, 4}} are the
two handshakes possible.
{{1, 3}, {2, 4}} is another
set of handshakes possible.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other.
Expected Time Complexity: O(2^{N})
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 30 | class Solution:
def count(self, N):
Dict = {}
def findCount(n, Dict):
if n == 0 or n == 1:
return 1
count = 0
for i in range(n):
if i not in Dict:
Dict[i] = findCount(i, Dict)
val1 = Dict[i]
if n - i - 1 not in Dict:
Dict[n - i - 1] = findCount(n - i - 1, Dict)
val2 = Dict[n - i - 1]
count += val1 * val2
return count
return findCount(N // 2, Dict)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
ob = Solution()
print(ob.count(N)) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.