description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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 solution(n): catalan = [1] * (n + 1) for i in range(2, n + 1): catalan[i] = 0 for j in range(i): catalan[i] += catalan[j] * catalan[i - j - 1] return catalan[n] if N % 2 != 0: return False else: return solution(N // 2)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER
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 0 if N == 2: return 1 c = 0 for i in range(0, N - 1, 2): left = self.count(i) right = self.count(N - 2 - i) if left and right: c += left * right else: c += left + right return c
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP 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
def handShake(p): dp = [(0) for _ in range(p + 1)] dp[0] = dp[1] = 1 for i in range(2, p + 1, 2): for j in range(0, i - 1, 2): dp[i] += dp[j] * dp[i - 2 - j] return dp[-1] class Solution: def count(self, N): return handShake(N)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR NUMBER CLASS_DEF FUNC_DEF RETURN 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, dic={}): if N == 0: return 1 if N in dic: return dic[N] res = 0 for i in range(0, N, 2): res += self.count(i, dic) * self.count(N - i - 2, dic) dic[N] = res return dic[N]
CLASS_DEF FUNC_DEF DICT IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR 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
d = {(0): 1, (2): 1, (4): 2} class Solution: def count(self, N): if N % 2 != 0: N -= 1 if N in d: return d[N] ans = 0 for j in range(1, N, 2): ans += self.count(j - 1) * self.count(N - j - 1) return ans
ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER 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
class Solution: def count(self, N): arr = [0] * (N + 1) arr[0] = 1 if N == 0: return arr[0] for i in range(2, N + 1, 2): temp = 0 for j in range(0, i, 2): temp = temp + arr[j] * arr[i - 2 - j] arr[i] = temp return arr[N]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR 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 helper(self, x): if x == 0 or x == 1: return 1 res = 0 for i in range(x): a = self.helper(i) b = self.helper(x - i - 1) res += a * b return res def count(self, N): res = self.helper(N // 2) return res
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP 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 handshakes(min, max): total_handshakes = 0 if min >= max: return 1 for partner in range(min + 1, max + 1, 2): above_handshakes = handshakes(min + 1, partner - 1) below_handshakes = handshakes(partner + 1, max) total_handshakes += above_handshakes * below_handshakes return total_handshakes return handshakes(1, N)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER 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): sum1 = 0 if N == 0 or N == 2: return 1 else: for i in range(0, N, 2): sum1 = sum1 + self.count(i) * self.count(N - i - 2) return sum1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP 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
class Solution: def count(self, N): if N % 2 == 1: return 0 dp = [1] * (N + 1) for i in range(4, N + 1): tmp, M = 0, i - 2 for j in range(0, M // 2, 2): tmp += dp[j] * dp[M - j] tmp *= 2 if M % 4 == 0: tmp += dp[M // 2] * dp[M // 2] dp[i] = tmp return dp[N]
CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR 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 solve(self, N, dict_hand): if N <= 2: return 1 if N in dict_hand: return dict_hand[N] ans = 0 for i in range(0, N, 2): ans += self.solve(i, dict_hand) * self.solve(N - i - 2, dict_hand) dict_hand[N] = ans return ans def count(self, N): dict_hand = {(2): 1} self.solve(N, dict_hand) return dict_hand[N]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR 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): dp = [0] * (N + 1) dp[0] = 1 dp[1] = 1 for i in range(2, N + 1): for j in range(i): dp[i] += dp[j] * dp[i - j - 1] return dp[N // 2] 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 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 VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP 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): d = {} def helper(x): if x == 0 or x == 1: return 1 if x in d: return d[x] ans = 0 for i in range(x): ans += helper(i) * helper(x - i - 1) d[x] = ans return d[x] return helper(N // 2)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR 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 ASSIGN VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER
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): dp = [0] * (N // 2 + 1) dp[0] = 1 for i in range(1, len(dp)): for j in range(i): dp[i] += dp[j] * dp[i - 1 - j] return dp[N // 2]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER
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
from sys import stdin input = stdin.readline inp = lambda: list(map(int, input().split())) mod = 10**9 + 7 def add(a, b): return (a % mod + b % mod) % mod def mul(a, b): return a % mod * (b % mod) % mod dp = [(-1) for i in range(501)] def solve(n): if n == 0: return 1 if dp[n] != -1: return dp[n] ans = 0 for i in range(n): left = solve(i) right = solve(n - i - 1) ans = add(ans, mul(left, right)) dp[n] = ans return ans def answer(): ans = solve(n) return ans for T in range(int(input())): n = int(input()) if n & 1: continue n //= 2 print(answer()) exit()
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL 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 hand(N): if N <= 2: return 1 ans = 0 for i in range(0, N, 2): ans += hand(i) * hand(N - i - 2) return ans class Solution: def count(self, N): ans = hand(N) return ans
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 VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF 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 == 0: return 0 else: return int(self.fact(N) / (self.fact(N / 2 + 1) * self.fact(N / 2))) def fact(self, n): if n == 1: return 1 else: return n * self.fact(n - 1)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER
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 kMod = 1000000007 dp = [0] * (N + 1) dp[0] = 1 for i in range(2, N + 1): for j in range(2, i + 1, 2): dp[i] += dp[j - 2] * dp[i - j] % kMod return dp[-1] % kMod
CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER 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): a = [-1] * (N + 1) ans = 0 for i in range(0, N + 1): if i % 2 != 0: a[i] = 0 a[0] = 0 if N == 1 or N == 0: return 0 a[2] = 1 if N == 2: return 1 a[4] = 2 if N < 6: return a[N] for i in range(6, N + 1): if a[i] == -1: for k in range(2, i + 1): if a[k - 2] == 0 or a[i - k] == 0: ans += a[k - 2] + a[i - k] else: ans += a[k - 2] * a[i - k] a[i] = ans ans = 0 return a[N] 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER RETURN 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): def catalan(n): dp = [0] * (n + 1) dp[0] = 1 for i in range(1, n + 1): dp[i] = 0 for j in range(i): dp[i] += dp[j] * dp[i - j - 1] return dp[n] return catalan(N // 2)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER
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 findCatalan(x): if x == 0 or x == 1: return 1 result = 0 for i in range(0, x): result += findCatalan(i) * findCatalan(x - i - 1) return result return findCatalan(N // 2)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER
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 if N == 0: return 1 ans = 0 left = 0 right = N - 2 while left <= N - 2: ans += self.count(left) * self.count(right) left += 2 right -= 2 return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
def count_up_array(array, n_min, n_max): count_array = [(0) for i in range(n_min, n_max + 1)] min_val = n_max + 1 max_val = n_min - 1 for i in range(0, len(array)): count_array[array[i]] += 1 for j in range(n_min, n_max + 1): if count_array[j] != 0 and min_val == n_max + 1: min_val = j if count_array[-1 * j] != 0 and max_val == n_min - 1: max_val = -1 * j return count_array, min_val, max_val def compute_shifted_abs_sum(counts, shift, n_min, n_max): total = 0 for i in range(n_min, n_max + 1): total += abs(counts[i] * (i + shift)) return total N = int(input()) A = [int(a) for a in input().split()] Q = int(input()) queries_list = [int(a) for a in input().split()] counts, min_value, max_value = count_up_array(A, -2000, 2000) positive_computation = compute_shifted_abs_sum(counts, -1 * min_value, -2000, 2000) negative_computation = compute_shifted_abs_sum(counts, -1 * max_value, -2000, 2000) total_shift = 0 for i in range(0, len(queries_list)): total_shift += queries_list[i] if total_shift >= -1 * min_value: print(positive_computation + (total_shift + min_value) * N) elif total_shift <= -1 * max_value: print(negative_computation - (total_shift + max_value) * N) else: print(compute_shifted_abs_sum(counts, total_shift, -2000, 2000))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
n = int(input()) arr = list(map(int, input().split())) q = int(input()) queries = list(map(int, input().split())) count = [0] * 4001 for i in arr: count[2000 + i] += 1 sum_num_right = [] sum_num_right.append(n) for i in range(4000): sum_num_right.append(sum_num_right[i] - count[i]) sum_right = [0] * 4001 for i in range(4001): sum_right[0] += count[i] * i for i in range(1, 4001): sum_right[i] = sum_right[i - 1] - sum_num_right[i] sum_left = [0] * 4001 for i in range(4000, -1, -1): sum_left[4000] += count[i] * (4000 - i) for i in range(3999, -1, -1): sum_left[i] = sum_left[i + 1] - (n - sum_num_right[i + 1]) acc = 0 for i in queries: acc += i mid = 2000 - acc if mid < 4001 and mid >= 0: print(sum_right[mid] + sum_left[mid]) elif mid < 0: print(sum_right[0] + n * abs(mid)) else: print(sum_left[4000] + n * (mid - 4000))
ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
n = int(input()) A = sorted([int(x) for x in input().split()]) B = [a for a in A if a < 0] A = [a for a in A if a >= 0] glob = 0 n = len(A) cumA = [0] * (n + 1) for i in range(1, n + 1): cumA[i] = cumA[i - 1] + A[i - 1] m = len(B) cumB = [0] * (m + 1) for i in range(1, m + 1): cumB[i] = cumB[i - 1] + B[i - 1] q = int(input()) for x in [int(x) for x in input().split()]: glob += x summa = 0 a = 0 b = n while a < b: c = (a + b) // 2 if A[c] + glob < 0: a = c + 1 else: b = c t11 = cumA[n] - cumA[a] + glob * (n - a) t12 = -(cumA[a] + glob * a) a = 0 b = m while a < b: c = (a + b) // 2 if B[c] + glob < 0: a = c + 1 else: b = c t21 = cumB[m] - cumB[b] + glob * (m - b) t22 = -(cumB[b] + glob * b) summa = t11 + t12 + t21 + t22 print(summa)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
class RangeTree: def __init__(self, rl, rh, nums): self.rl = rl self.rh = rh if rh <= rl: self.num_sum = 0 elif rh - rl == 1: self.num_sum = nums[rl] else: mid = (rl + rh) // 2 self.left_child = RangeTree(rl, mid, nums) self.right_child = RangeTree(mid, rh, nums) self.num_sum = self.left_child.num_sum + self.right_child.num_sum def query_range(self, l, h): if self.rh <= self.rl or h <= self.rl or l >= self.rh: result = 0 elif l <= self.rl < self.rh <= h: result = self.num_sum else: l = max(l, self.rl) h = min(h, self.rh) mid = (l + h) // 2 result = self.left_child.query_range(l, h) + self.right_child.query_range( l, h ) return result class Maintainer: def __init__(self, nums): self.nums = sorted(nums) self.cur_added_sum = 0 self.cur_sum = 0 self.rt = RangeTree(0, len(self.nums), self.nums) self.least_larger = self.__least_larger() self.largest_less = self.__largest_less() self.cur_sum = sum([abs(x) for x in self.nums]) def __least_larger(self): target = -self.cur_added_sum if target < self.nums[0]: return 0 if target > self.nums[-1]: return len(self.nums) head, tail = 0, len(self.nums) - 1 while head < tail - 1: mid = (head + tail) // 2 if self.nums[mid] <= target: head = mid elif self.nums[mid] > target: tail = mid if self.nums[tail] == target: return len(self.nums) else: return tail def __largest_less(self): target = -self.cur_added_sum if target > self.nums[-1]: return len(self.nums) - 1 if target < self.nums[0]: return -1 head, tail = 0, len(self.nums) - 1 while head < tail - 1: mid = (head + tail) // 2 if self.nums[mid] < target: head = mid if self.nums[mid] >= target: tail = mid if self.nums[head] == target: return -1 else: return head def query(self, x): if x == 0: return self.cur_sum self.cur_added_sum += x new_least_larger = self.__least_larger() new_largest_less = self.__largest_less() if x > 0: self.cur_sum += x * (len(self.nums) - self.largest_less - 1) self.cur_sum -= x * (new_largest_less + 1) after_change = self.rt.query_range( new_largest_less + 1, self.largest_less + 1 ) + self.cur_added_sum * (self.largest_less - new_largest_less) self.cur_sum += abs(after_change) - abs( after_change - x * (self.largest_less - new_largest_less) ) else: x = -x self.cur_sum += x * self.least_larger self.cur_sum -= x * (len(self.nums) - new_least_larger) after_change = self.rt.query_range( self.least_larger, new_least_larger ) + self.cur_added_sum * (new_least_larger - self.least_larger) self.cur_sum += abs(after_change) - abs( after_change + x * (new_least_larger - self.least_larger) ) self.least_larger = new_least_larger self.largest_less = new_largest_less return self.cur_sum N = int(input()) maintainer = Maintainer([int(x) for x in input().split()]) Q = int(input()) for x in [int(x) for x in input().split()]: print(maintainer.query(x))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR IF VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
import sys def findFirstGE(ar, v): N = len(ar) if ar[0] >= v: return 0 if ar[-1] < v: return -1 sBegin = 0 sEnd = N - 1 while sBegin + 1 != sEnd: iMid = (sBegin + sEnd) // 2 if ar[iMid] >= v: sEnd = iMid else: sBegin = iMid return sEnd def getSumLT(arSum, i): if i < 0: return arSum[-1] if i == 0: return 0 return arSum[i - 1] def getSumGE(arSum, i): if i < 0: return 0 if i == 0: return arSum[-1] return arSum[-1] - arSum[i - 1] N = int(input().strip()) ar = list(map(int, input().strip().split(" "))) ar.sort() Q = int(input().strip()) qr = list(map(int, input().strip().split(" "))) cumSum = [0] * N cumSum[0] = ar[0] for i in range(1, N): cumSum[i] = cumSum[i - 1] + ar[i] totalAdded = 0 result = [] for q in qr: totalAdded += q i = findFirstGE(ar, -totalAdded) r = 0 if i < 0: r = -(cumSum[-1] + N * totalAdded) else: r = -(getSumLT(cumSum, i) + i * totalAdded) + ( getSumGE(cumSum, i) + (N - i) * totalAdded ) result.append(r) print(r)
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER RETURN BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
import sys def locate(val, lst): lo, hi = 0, len(lst) while lo < hi: mid = lo + hi >> 1 if val > lst[mid]: lo = mid + 1 else: hi = mid return hi def absolute_elements_sum(line): N = int(next(line)) arr = sorted(map(int, next(line).split())) z = [0] for c in arr: z.append(z[-1] + c) next(line) sol = [] p = 0 for c in map(int, next(line).split()): p += c i1 = locate(-p, arr) sol.append(z[-1] + p * (N - 2 * i1) - 2 * z[i1]) print("\n".join(map(str, sol))) absolute_elements_sum(sys.stdin)
IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
def binarySearch(a, total): l = 0 r = len(a) while r - l > 1: m = (l + r) // 2 if a[m] + total >= 0: r = m else: l = m return l n = int(input().strip()) a = sorted(list(map(int, input().strip().split()))) partial = [a[0]] for i in range(1, n): partial.append(a[i] + partial[-1]) Q = int(input().strip()) q = list(map(int, input().strip().split())) total = 0 for i in range(Q): total += q[i] index = binarySearch(a, total) leftSum = partial[index] + total * (index + 1) rightSum = partial[n - 1] - partial[index] + total * (n - index - 1) print(abs(leftSum) + rightSum)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
def playingWithNumbers(arr, queries): if arr[0] > 0: arr.insert(0, 0) pv = 0 total = 0 piv = search(0) sum_arr = arr[:] sm = 0 a = arr[piv] for i in range(piv + 1, len(arr)): a += arr[i] sum_arr[i] = a a = arr[piv - 1] for i in range(piv - 2, -1, -1): a += arr[i] sum_arr[i] = a for i in range(len(queries)): sm += queries[i] if sm >= -arr[0]: total = sum_arr[-1] + sum_arr[0] + len(arr) * sm print(total) total = 0 continue pv = search(-sm) if sm >= 0: if sum_arr[pv] > 0: gar = 0 else: gar = sum_arr[pv] total += sum_arr[-1] + gar + (len(arr) - pv) * sm total += -(sum_arr[0] - gar) - pv * sm elif sm < 0: pv -= 1 if sum_arr[pv] < 0: gar = 0 else: gar = sum_arr[pv] total += sum_arr[-1] - gar + (len(arr) - pv - 1) * sm total += -(sum_arr[0] + gar) - (pv + 1) * sm print(total) total = 0 def search(x): l = 0 r = len(arr) while l <= r: mid = l + (r - l) // 2 if r - l <= 1: return r if arr[mid] == x: return mid elif arr[mid] < x: l = mid else: r = mid n = int(input()) arr = list(map(int, input().rstrip().split())) arr.sort() q = int(input()) queries = list(map(int, input().rstrip().split())) playingWithNumbers(arr, queries)
FUNC_DEF IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
class TheThing: def __init__(self, array): self.array = array self.array.sort() self.array.reverse() self.num = len(self.array) self.leftSums = [0] for elem in array: self.leftSums.append(self.leftSums[-1] + elem) self.totalSum = self.leftSums[-1] self.rightSums = [ (self.totalSum - self.leftSums[i]) for i in range(len(self.leftSums)) ] self.querySum = 0 def AddQuery(self, query): i = self.FindIndex(query) self.querySum = self.querySum + query return self.leftSums[i] - self.rightSums[i] + (2 * i - self.num) * self.querySum def FindIndex(self, query): accumulatedQuery = self.querySum + query start = 0 end = len(self.array) return self.BinaryFindIndex(start, end, accumulatedQuery) def BinaryFindIndex(self, start, end, test): if start == end: return start if start == end - 1: if self.array[end - 1] + test >= 0: return end else: return start candidate = (start + end) // 2 if self.array[candidate - 1] + test >= 0: return self.BinaryFindIndex(candidate, end, test) return self.BinaryFindIndex(start, candidate, test) def main(): n = input().strip().split() array = [int(x) for x in input().strip().split()] num_queries = input().strip().split() queries = [int(x) for x in input().strip().split()] thing = TheThing(array) for q in queries: print(thing.AddQuery(q)) main()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
import sys from itertools import accumulate def playingWithNumbers(n, arr, queries): from itertools import accumulate def find(val, ln, arr): if arr[0] >= val: return 0 if arr[-1] < val: return ln st, i, end = 0, ln // 2, ln while i != 0 and arr[i - 1] >= val or arr[i] < val: if arr[i] < val: st, i = i, (i + end) // 2 else: end, i = i, (i + st) // 2 return i arr.sort() qtot = 0 tot = sum(abs(i) for i in arr) zeroSt = find(0, n, arr) zeroEn = find(1, n, arr) out = [] for q in queries: qtot += q adj = 0 if qtot < 0: st, end = zeroEn, find(-qtot + 1, n, arr) adj = ( qtot * (n - end) - qtot * st + sum(abs(i + qtot) - abs(i) for i in arr[st:end]) ) if qtot > 0: st, end = find(-qtot, n, arr), zeroSt adj = ( qtot * (n - end) - qtot * st + sum(abs(i + qtot) - abs(i) for i in arr[st:end]) ) out.append(tot + adj) return out n = int(input()) arr = list(map(int, input().split())) q = int(input()) queries = list(map(int, input().split())) count = [0] * 4001 for i in arr: count[2000 + i] += 1 sum_num_right = [] sum_num_right.append(n) for i in range(4000): sum_num_right.append(sum_num_right[i] - count[i]) sum_right = [0] * 4001 for i in range(4001): sum_right[0] += count[i] * i for i in range(1, 4001): sum_right[i] = sum_right[i - 1] - sum_num_right[i] sum_left = [0] * 4001 for i in range(4000, -1, -1): sum_left[4000] += count[i] * (4000 - i) for i in range(3999, -1, -1): sum_left[i] = sum_left[i + 1] - (n - sum_num_right[i + 1]) acc = 0 for i in queries: acc += i mid = 2000 - acc if mid < 4001 and mid >= 0: print(sum_right[mid] + sum_left[mid]) elif mid < 0: print(sum_right[0] + n * abs(mid)) else: print(sum_left[4000] + n * (mid - 4000))
IMPORT FUNC_DEF FUNC_DEF IF VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
n = int(input().strip()) a = [int(x) for x in input().strip().split(" ")] q = int(input().strip()) queries = [int(x) for x in input().strip().split(" ")] for j in range(q - 1): queries[j + 1] = queries[j] + queries[j + 1] counts = [(0) for _ in range(4001)] for entry in a: counts[entry + 2000] += 1 partsmsum = [(0) for _ in range(4001)] partsmct = [(0) for _ in range(4001)] partlgsum = [(0) for _ in range(4001)] partlgct = [(0) for _ in range(4001)] partsmsum[0] = -2000 * counts[0] partsmct[0] = counts[0] partlgsum[4000] = 0 partlgct[4000] = 0 for j in range(1, 4001): partsmsum[j] = partsmsum[j - 1] + (j - 2000) * counts[j] partsmct[j] = partsmct[j - 1] + counts[j] partlgsum[4000 - j] = ( partlgsum[4000 - j + 1] + (2000 - j + 1) * counts[4000 - j + 1] ) partlgct[4000 - j] = partlgct[4000 - j + 1] + counts[4000 - j + 1] for j in queries: if j <= -2000: print(-partsmsum[4000] - j * n) if j >= 2000: print(partsmsum[4000] + j * n) if j > -2000 and j < 2000: print( -partsmsum[2000 - j] - j * partsmct[2000 - j] + partlgsum[2000 - j] + j * partlgct[2000 - j] )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
n = int(input()) a = [int(x) for x in input().split()] m = int(input()) b = [int(x) for x in input().split()] a.sort() s = [0] for x in a: s.append(s[-1] + x) def find(x): l, r = 0, len(a) while l < r: m = (l + r) // 2 if x > a[m]: l = m + 1 else: r = m return l def solve(x): l = find(0) r = find(-x) l, r = min(l, r), max(l, r) result = ( abs(s[l] + x * l) + abs(s[r] - s[l] + x * (r - l)) + abs(s[-1] - s[r] + x * (n - r)) ) return result xsum = 0 for x in b: xsum += x print(solve(xsum))
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
import sys low, high = -2000, 2000 def count(arr): counts = [0] * (high - low + 1) for a in arr: counts[a - low] += 1 return counts def agg_sum(arr): arr_agg = arr[:] for i in range(1, len(arr)): arr_agg[i] = arr_agg[i - 1] + arr[i] return arr_agg def count_leq(arr): arr_agg = arr[:] for i in reversed(range(0, len(arr) - 1)): arr_agg[i] = arr_agg[i + 1] + arr[i] return arr_agg def count_gr(arr): arr_agg = [0] * len(arr) for i in range(1, len(arr)): arr_agg[i] = arr_agg[i - 1] + arr[i - 1] return arr_agg def query(arr, q): result = 0 for a in arr: result += abs(q + a) return result def playingWithNumbers(arr, queries): c_neq = count([(-a) for a in arr]) c_leq = count_leq(c_neq) c_gr = count_gr(c_neq) N = len(arr) Q = [0] * (high - low + 1) Q[low - low] = query(arr, low) for v in range(low + 1, high + 1): Q[v - low] = Q[v - 1 - low] - c_leq[v - low] + c_gr[v - low] q_agg = agg_sum(queries) for q in q_agg: if q < low: yield Q[low - low] + (low - q) * N elif q > high: yield Q[high - low] + (q - high) * N else: yield Q[q - low] n = int(input().strip()) arr = list(map(int, input().strip().split(" "))) q = int(input().strip()) queries = list(map(int, input().strip().split(" "))) result = playingWithNumbers(arr, queries) print("\n".join(map(str, result)))
IMPORT ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
def prepareBook(arr): book = { x: {"n": 0, "leftSum": 0, "leftNums": 0, "rightSum": 0, "rightNums": 0} for x in range(-2001, 2002) } for num in arr: book[num]["n"] += 1 leftSum = 0 leftNums = 0 for i in range(-2001, 2002): book[i]["leftSum"] = leftSum book[i]["leftNums"] = leftNums leftSum += book[i]["n"] * (i + 2000) leftNums += book[i]["n"] rightSum = 0 rightNums = 0 for i in range(2001, -2002, -1): book[i]["rightSum"] = rightSum book[i]["rightNums"] = rightNums rightSum += book[i]["n"] * (2000 - i) rightNums += book[i]["n"] return book def calcSum(queries, book): center = 0 for query in queries: center -= query if center < -2000: preRightSum = book[2001]["leftSum"] rightSum = preRightSum + book[2001]["leftNums"] * (-center - 2000) print(rightSum) elif center > 2000: preLeftSum = book[-2001]["rightSum"] leftSum = preLeftSum + book[-2001]["rightNums"] * (center - 2000) print(leftSum) else: preRightSum = book[2001]["leftSum"] - book[center + 1]["leftSum"] rightSum = preRightSum - book[center]["rightNums"] * (2000 + center) preLeftSum = book[-2001]["rightSum"] - book[center - 1]["rightSum"] leftSum = preLeftSum - book[center]["leftNums"] * (2000 - center) print(leftSum + rightSum) n = int(input()) arr = list(map(int, input().strip().split(" "))) book = prepareBook(arr) numQueries = int(input()) queries = list(map(int, input().strip().split(" "))) calcSum(queries, book)
FUNC_DEF ASSIGN VAR VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR VAR VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR STRING VAR ASSIGN VAR VAR STRING VAR VAR BIN_OP VAR VAR STRING BIN_OP VAR NUMBER VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR STRING VAR ASSIGN VAR VAR STRING VAR VAR BIN_OP VAR VAR STRING BIN_OP NUMBER VAR VAR VAR VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR STRING BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR STRING BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
n = int(input()) A = sorted(int(x) for x in input().split()) C = [0] for a in A: C.append(C[-1] + a) nq = int(input()) Q = [int(x) for x in input().split()] def bsearch(L, bias, x): a = 0 b = len(L) - 1 if x >= L[b] + bias: return len(L) if x < L[a] + bias: return 0 while b - a > 1: c = (a + b) // 2 if L[c] + bias <= x: a = c else: b = c return b bias = 0 for q in Q: bias += q ix = bsearch(A, bias, 0) pos = C[-1] - C[ix] + (n - ix) * bias neg = C[ix] - C[0] + ix * bias print(pos - neg)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR RETURN NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given an array of integers, you must answer a number of queries. Each query consists of a single integer, $\boldsymbol{x}$, and is performed as follows: Add $\boldsymbol{x}$ to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. Tip: The Input/Output for this challenge is very large, so you'll have to be creative in your approach to pass all test cases. Function Description Complete the playingWithNumbers function in the editor below. It should return an array of integers that represent the responses to each query. playingWithNumbers has the following parameter(s): arr: an array of integers queries: an array of integers Input Format The first line contains an integer $n$ the number of elements in $\textbf{arr}$. The second line contains $n$ space-separated integers $arr\left[i\right]$. The third line contains an integer $\textit{q}$, the number of queries. The fourth line contains $\textit{q}$ space-separated integers $\boldsymbol{x}$ where $qumeries[j]=x$. Constraints $1\leq n\leq5\times10^5$ $1\leq q\leq5\times10^5$ $-2000\leq arr[i]\leq2000$, where $0\leq i<n$. $-2000\leq\textit{queries}[j]\leq2000$, where $0\leq j<q$ Output Format For each query, print the sum of the absolute values of all the array's elements on a new line. Sample Input 3 -1 2 -3 3 1 -2 3 Sample Output 5 7 6 Explanation Query 0: $x=1$ Array: $[-1,2,-3]\rightarrow[0,3,-2]$ The sum of the absolute values of the updated array's elements is $|0|+|3|+|-2|=0+3+2=5$. Query 1: $x=-2$ Array: $[0,3,-2]\rightarrow[-2,1,-4]$ The sum of the absolute values of the updated array's elements is $|-2|+|1|+|-4|=2+1+4=7$. Query 2: $x=3$ Array: $[-2,1,-4]\rightarrow[1,4,-1]$ The sum of the absolute values of the updated array's elements is $|1|+|4|+|-1|=1+4+1=6$.
n = int(input().strip()) a = list(map(int, input().strip().split())) q = int(input().strip()) qv = map(int, input().strip().split()) qrsumv = [] rsum = 0 for x in qv: rsum += x qrsumv.append(rsum) a.sort() arsumv = [] rsum = 0 for ai in a: rsum += ai arsumv.append(rsum) for xri in qrsumv: low = 0 high = n - 1 while high >= low: mid = (low + high) // 2 if a[mid] > -xri: high = mid - 1 elif a[mid] < -xri: low = mid + 1 else: break if high == mid - 1: mid -= 1 arsumneg = arsumv[mid] if mid >= 0 else 0 arsumpos = arsumv[-1] - arsumneg print(-arsumneg - (mid + 1) * xri + arsumpos + (n - mid - 1) * xri)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $\frac{p}{s} = k$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 10^5), where n is the length of the array and k is the constant described above. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^8) — the elements of the array. -----Output----- In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k. -----Examples----- Input 1 1 1 Output 1 Input 4 2 6 3 8 1 Output 2 -----Note----- In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $\frac{1}{1} = 1$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $\frac{18}{9} = 2$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $\frac{24}{12} = 2$.
n, k = map(int, input().split()) a = list(map(int, input().split())) max_ = 10**13 + 1 def get(l, r, x): min_ = min(l, r) max_ = max(l, r) if x <= min_: return x + 1 if x <= max_: return min_ + 1 return l + r + 1 - x def solve(a, k): cnt = 0 arr = [i for i, x in enumerate(a) if x > 1] n_ = len(arr) for i in range(n_ - 1): curS = 0 curP = 1 l = arr[i] if i == 0 else arr[i] - arr[i - 1] - 1 for j in range(i, n_): ind = arr[j] curS += a[ind] curP *= a[ind] if curP > max_: break if curP % curS == 0: if curP // curS == k: cnt += 1 r = arr[j + 1] - arr[j] - 1 x = curP // k - curS if curP % k == 0 and x > 0 and x <= l + r: cnt += get(l, r, x) curS += r if k == 1: cnt += len(a) - n_ return cnt ans = 0 a.append(max_) ans += solve(a, k) print(ans)
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 BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $\frac{p}{s} = k$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 10^5), where n is the length of the array and k is the constant described above. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^8) — the elements of the array. -----Output----- In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k. -----Examples----- Input 1 1 1 Output 1 Input 4 2 6 3 8 1 Output 2 -----Note----- In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $\frac{1}{1} = 1$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $\frac{18}{9} = 2$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $\frac{24}{12} = 2$.
n, k = list(map(int, input().split())) a = list(map(int, input().split())) ones_ser_ie = [(0) for _ in range(n)] cur_ones_ser_ie = 0 ones_in_tail_i = 0 for i in reversed(list(range(n))): if a[i] == 1: ones_in_tail_i += 1 cur_ones_ser_ie += 1 else: cur_ones_ser_ie = 0 ones_ser_ie[i] = cur_ones_ser_ie res = 0 for i in range(n): p = 1 s = 0 j = i ones_in_tail_j = ones_in_tail_i while j < n: if a[j] == 1: cur_ones_ser_ie = ones_ser_ie[j] if p % k == 0 and 1 <= p // k - s <= cur_ones_ser_ie: res += 1 ones_in_tail_j -= cur_ones_ser_ie s += cur_ones_ser_ie j += cur_ones_ser_ie else: p *= a[j] s += a[j] if p == s * k: res += 1 elif p > (s + ones_in_tail_j) * k: break j += 1 if a[i] == 1: ones_in_tail_i -= 1 print(res)
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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $\frac{p}{s} = k$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 10^5), where n is the length of the array and k is the constant described above. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^8) — the elements of the array. -----Output----- In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k. -----Examples----- Input 1 1 1 Output 1 Input 4 2 6 3 8 1 Output 2 -----Note----- In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $\frac{1}{1} = 1$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $\frac{18}{9} = 2$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $\frac{24}{12} = 2$.
n, k = map(int, input().split()) v = list(map(int, input().split())) pos, pref = [-1], [] ans = 0 for i in range(n): if v[i] != 1: pos.append(i) if v[i] == 1 and k == 1: ans += 1 if i: pref.append(pref[-1] + v[i]) else: pref.append(v[i]) pos.append(n) m = len(pos) inf = int(2e18 + 10) for i in range(1, m - 1): p = 1 for j in range(i, m - 1): p = p * v[pos[j]] if p > inf: break s = pref[pos[j]] if pos[i]: s -= pref[pos[i] - 1] if s * k == p: ans += 1 d = p - s * k if d > 0 and d % k == 0: w = d // k f = min(pos[i] - pos[i - 1] - 1, w) b = min(pos[j + 1] - pos[j] - 1, w) if f + b < w: continue ans += f + b - w + 1 print(ans)
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 VAR LIST NUMBER LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $\frac{p}{s} = k$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 10^5), where n is the length of the array and k is the constant described above. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^8) — the elements of the array. -----Output----- In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k. -----Examples----- Input 1 1 1 Output 1 Input 4 2 6 3 8 1 Output 2 -----Note----- In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $\frac{1}{1} = 1$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $\frac{18}{9} = 2$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $\frac{24}{12} = 2$.
n, k = map(int, input().split()) A = list(map(int, input().split())) A = [0] + A x = 0 prev = [(0) for i in range(n + 1)] sm = [(0) for i in range(n + 1)] for i in range(1, n + 1): prev[i] = x if A[i] > 1: x = i sm[i] = A[i] + sm[i - 1] lim = int(2 * 10**18) ans = 0 for i in range(1, n + 1): p = 1 j = i while j: if lim // A[j] > p: s = sm[i] - sm[j - 1] p *= A[j] sx = s + j - 1 - prev[j] if p % k == 0 and p >= k * s and p <= k * sx: ans += 1 else: break j = prev[j] print(ans)
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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $\frac{p}{s} = k$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 10^5), where n is the length of the array and k is the constant described above. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^8) — the elements of the array. -----Output----- In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k. -----Examples----- Input 1 1 1 Output 1 Input 4 2 6 3 8 1 Output 2 -----Note----- In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $\frac{1}{1} = 1$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $\frac{18}{9} = 2$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $\frac{24}{12} = 2$.
from itertools import accumulate n, k = map(int, input().split()) A = list(map(int, input().split())) C = [0] + A C = list(accumulate(C)) A = [0] + A P = [0] * (n + 1) x = 0 for i in range(1, n + 1): P[i] = x if A[i] > 1: x = i INF = 2 * 10**18 + 1 ans = 0 for i in range(1, n + 1): p = 1 j = i while j: if p * A[j] < INF: s = C[i] - C[j - 1] p *= A[j] if p % k == 0: d = p // k - s if 0 <= d <= j - P[j] - 1: ans += 1 else: break j = P[j] print(ans)
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 FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $\frac{p}{s} = k$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 10^5), where n is the length of the array and k is the constant described above. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^8) — the elements of the array. -----Output----- In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k. -----Examples----- Input 1 1 1 Output 1 Input 4 2 6 3 8 1 Output 2 -----Note----- In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $\frac{1}{1} = 1$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $\frac{18}{9} = 2$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $\frac{24}{12} = 2$.
n, k = map(int, input().split()) flag = [(0) for i in range(0, n)] a = list(map(int, input().split())) r = [(-1) for i in range(0, n)] i = 0 while i < n: if a[i] != 1: i = i + 1 continue j = i while j < n and a[j] == 1: j = j + 1 while i < j: r[i] = j - 1 i = i + 1 ans = 0 maxn = 2**63 - 1 for i in range(0, n): p = 1 s = 0 j = i while j < n: if a[j] != 1: p = p * a[j] s = s + a[j] if p % s == 0 and p // s == k: ans = ans + 1 j = j + 1 else: if p % k == 0 and (p // k - s > 0 and p // k - s <= r[j] - j + 1): ans = ans + 1 s = s + r[j] - j + 1 j = r[j] + 1 if p >= maxn: break print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $\frac{p}{s} = k$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 10^5), where n is the length of the array and k is the constant described above. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^8) — the elements of the array. -----Output----- In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k. -----Examples----- Input 1 1 1 Output 1 Input 4 2 6 3 8 1 Output 2 -----Note----- In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $\frac{1}{1} = 1$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $\frac{18}{9} = 2$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $\frac{24}{12} = 2$.
[n, k] = map(int, input().strip().split()) ais = list(map(int, input().strip().split())) n1 = ais.count(1) one_serie = [(0) for _ in range(n)] for i in reversed(range(n)): if ais[i] == 1: one_serie[i] = (0 if i == n - 1 else one_serie[i + 1]) + 1 n1_head = 0 count = 0 for i in range(n): p = 1 s = 0 if i > 0 and ais[i - 1] == 1: n1_head += 1 n1_tail = n1 - n1_head j = i while j < n: if ais[j] == 1: if p % k == 0 and 1 <= p // k - s <= one_serie[j]: count += 1 n1_tail -= one_serie[j] s += one_serie[j] j += one_serie[j] else: p *= ais[j] s += ais[j] if p == s * k: count += 1 elif p > (s + n1_tail) * k: break j += 1 print(count)
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR 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 10^9 + 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 a_{i} (1 ≤ a_{i} ≤ 10^9), 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 10^9 + 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 10^9 + 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 10^9 + 7 is 10^9 + 6, so this is the correct output.
from sys import exit, stdin, stdout n = int(stdin.readline()) a = [int(i) for i in stdin.readline().split()] if n == 1: print(a[0]) exit(0) mod = 1000000007 f = [0] * (n + 1) f[0] = 1 for i in range(1, n + 1): f[i] = f[i - 1] * i % mod def f_pow(a, k): if k == 0: return 1 if k % 2 == 1: return f_pow(a, k - 1) * a % mod else: return f_pow(a * a % mod, k // 2) % mod def c(n, k): d = f[k] * f[n - k] % mod return f[n] * f_pow(d, mod - 2) % mod oper = 1 while not (oper and n % 2 == 0): for i in range(n - 1): a[i] = a[i] + oper * a[i + 1] oper *= -1 n -= 1 oper *= 1 if n // 2 % 2 != 0 else -1 sm1 = 0 sm2 = 0 for i in range(n): if i % 2 == 0: sm1 = (sm1 + c(n // 2 - 1, i // 2) * a[i]) % mod else: sm2 = (sm2 + c(n // 2 - 1, i // 2) * a[i]) % mod stdout.write(str((sm1 + oper * sm2) % mod))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR 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 10^9 + 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 a_{i} (1 ≤ a_{i} ≤ 10^9), 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 10^9 + 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 10^9 + 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 10^9 + 7 is 10^9 + 6, so this is the correct output.
import sys def CreateFact(n, mod): cant = n + 1 factorials = [1] * cant for x in range(1, cant): val = factorials[x - 1] * x % mod factorials[x] = val inv_factorial = [1] * cant inv_factorial[n] = pow(factorials[-1], mod - 2, mod) for x in reversed(range(0, n)): val = inv_factorial[x + 1] * (x + 1) % mod inv_factorial[x] = val return factorials, inv_factorial def CalculateNPairsCoef(n, mod): factorial, inv_factorial = CreateFact(n, mod) coef = [1] * n middle = int((n + 1) / 2) last = n - 1 for x in range(1, middle): o = factorial[n - 1] * inv_factorial[n - 1 - x] % mod * inv_factorial[x] % mod coef[x] = o coef[last - x] = o return coef def KarenAdTest(): n = int(sys.stdin.readline()) line = sys.stdin.readline().split() i = 0 while i < n: x = int(line[i]) line[i] = x i += 1 mod = 1000000007 if n == 1: val = line[0] % mod print(val) return if n == 2: val = (line[0] + line[1]) % mod print(val) return if n == 3: val = (line[0] + 2 * line[1] - line[2]) % mod print(val) return if n == 4: val = (line[0] - line[1] + line[2] - line[3]) % mod print(val) return if n == 5: val = (line[0] + 2 * line[2] + line[4]) % mod print(val) return coefi = [1] * n if n % 2 == 0: m = int(n / 2) c = CalculateNPairsCoef(m, mod) pos = 0 if n % 4 == 0: for x in range(0, m): coefi[pos] = c[x] pos += 1 coefi[pos] = -c[x] pos += 1 else: for x in range(0, m): coefi[pos] = c[x] pos += 1 coefi[pos] = c[x] pos += 1 else: sr = n - 1 m = int(sr / 2) c = CalculateNPairsCoef(m, mod) co = [1] * sr pos = 2 for x in range(1, m): co[pos] = c[x] pos += 1 co[pos] = c[x] pos += 1 if n % 4 == 1: coefi = [0] * n coefi[0] = coefi[n - 1] = 1 for x in range(2, sr, 2): coefi[x] = co[x - 1] + co[x] else: for x in range(2, sr, 2): coefi[x] = -co[x - 1] + co[x] for x in range(1, sr, 2): coefi[x] = co[x - 1] + co[x] coefi[sr] = -1 res = 0 for x in range(0, n): res += coefi[x] * line[x] % mod res = int(res % mod) print(res) KarenAdTest()
IMPORT FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that xi < yi, and for any j (1 ≤ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≤ k ≤ 105). Output Print the string Anna and Maria need — the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
ch = input() K = int(input()) if len(ch) * (len(ch) + 1) / 2 < K: print("No such line.") else: nn = len(ch) pos = [i for i in range(nn)] ich = [ord(i) for i in ch] ans = "" while True: num = [0] * 128 for i in pos: num[ich[i]] += nn - i addChar = 0 for c in range(ord("a"), ord("z") + 1): if num[c] >= K: addChar = c break K -= num[c] pos = [i for i in pos if ich[i] == addChar] ans += chr(addChar) if len(pos) >= K: break K -= len(pos) pos = [(i + 1) for i in pos if i + 1 < nn] print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING WHILE NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out nm cards as a rectangle n × m. If there are jokers among them, then Vasya should change them with some of the rest of 54 - nm cards (which are not layed out) so that there were no jokers left. Vasya can pick the cards to replace the jokers arbitrarily. Remember, that each card presents in pack exactly once (i. e. in a single copy). Vasya tries to perform the replacements so that the solitaire was solved. Vasya thinks that the solitaire is solved if after the jokers are replaced, there exist two non-overlapping squares 3 × 3, inside each of which all the cards either have the same suit, or pairwise different ranks. Determine by the initial position whether the solitaire can be solved or not. If it can be solved, show the way in which it is possible. Input The first line contains integers n and m (3 ≤ n, m ≤ 17, n × m ≤ 52). Next n lines contain m words each. Each word consists of two letters. The jokers are defined as "J1" and "J2" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one — for the suit. The possible ranks are: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" and "A". The possible suits are: "C", "D", "H" and "S". All the cards are different. Output If the Solitaire can be solved, print on the first line "Solution exists." without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible: * "There are no jokers.", if there are no jokers in the input data. * "Replace Jx with y.", if there is one joker. x is its number, and y is the card it should be replaced with. * "Replace J1 with x and J2 with y.", if both jokers are present in the input data. x and y here represent distinct cards with which one should replace the first and the second jokers correspondingly. On the third line print the coordinates of the upper left corner of the first square 3 × 3 in the format "Put the first square to (r, c).", where r and c are the row and the column correspondingly. In the same manner print on the fourth line the coordinates of the second square 3 × 3 in the format "Put the second square to (r, c).". If there are several solutions to that problem, print any of them. If there are no solutions, print of the single line "No solution." without the quotes. See the samples to understand the output format better. Examples Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H 5S TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output No solution. Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H J1 TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output Solution exists. Replace J1 with 2H. Put the first square to (1, 1). Put the second square to (2, 4). Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H QC TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output Solution exists. There are no jokers. Put the first square to (1, 1). Put the second square to (2, 4). Note The pretests cover all the possible output formats.
ranks = "23456789TJQKA" suits = "CDHS" n, m = [int(i) for i in input().split()] b = [input().split() for _ in range(n)] p = [(r + s) for r in ranks for s in suits] j1, j2 = False, False for r in b: for c in r: if c == "J1": j1 = True elif c == "J2": j2 = True else: p.remove(c) def valid(n, m): r = set() s = set() for ni in range(n, n + 3): for mi in range(m, m + 3): c = b[ni][mi] if c == "J1": c = j1v if c == "J2": c = j2v r.add(c[0]) s.add(c[1]) return len(r) == 9 or len(s) == 1 def solve(): global j1v, j2v, n0, m0, n1, m1 for j1v in p: for j2v in p: if j1v == j2v: continue for n0 in range(n - 2): for m0 in range(m - 2): if not valid(n0, m0): continue for n1 in range(n - 2): for m1 in range(m - 2): if n0 + 2 < n1 or n1 + 2 < n0 or m0 + 2 < m1 or m1 + 2 < m0: if valid(n1, m1): return True return False if solve(): print("Solution exists.") if j1 and j2: print("Replace J1 with {} and J2 with {}.".format(j1v, j2v)) elif j1: print("Replace J1 with {}.".format(j1v)) elif j2: print("Replace J2 with {}.".format(j2v)) else: print("There are no jokers.") print("Put the first square to ({}, {}).".format(n0 + 1, m0 + 1)) print("Put the second square to ({}, {}).".format(n1 + 1, m1 + 1)) else: print("No solution.")
ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR VAR FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out nm cards as a rectangle n × m. If there are jokers among them, then Vasya should change them with some of the rest of 54 - nm cards (which are not layed out) so that there were no jokers left. Vasya can pick the cards to replace the jokers arbitrarily. Remember, that each card presents in pack exactly once (i. e. in a single copy). Vasya tries to perform the replacements so that the solitaire was solved. Vasya thinks that the solitaire is solved if after the jokers are replaced, there exist two non-overlapping squares 3 × 3, inside each of which all the cards either have the same suit, or pairwise different ranks. Determine by the initial position whether the solitaire can be solved or not. If it can be solved, show the way in which it is possible. Input The first line contains integers n and m (3 ≤ n, m ≤ 17, n × m ≤ 52). Next n lines contain m words each. Each word consists of two letters. The jokers are defined as "J1" and "J2" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one — for the suit. The possible ranks are: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" and "A". The possible suits are: "C", "D", "H" and "S". All the cards are different. Output If the Solitaire can be solved, print on the first line "Solution exists." without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible: * "There are no jokers.", if there are no jokers in the input data. * "Replace Jx with y.", if there is one joker. x is its number, and y is the card it should be replaced with. * "Replace J1 with x and J2 with y.", if both jokers are present in the input data. x and y here represent distinct cards with which one should replace the first and the second jokers correspondingly. On the third line print the coordinates of the upper left corner of the first square 3 × 3 in the format "Put the first square to (r, c).", where r and c are the row and the column correspondingly. In the same manner print on the fourth line the coordinates of the second square 3 × 3 in the format "Put the second square to (r, c).". If there are several solutions to that problem, print any of them. If there are no solutions, print of the single line "No solution." without the quotes. See the samples to understand the output format better. Examples Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H 5S TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output No solution. Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H J1 TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output Solution exists. Replace J1 with 2H. Put the first square to (1, 1). Put the second square to (2, 4). Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H QC TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output Solution exists. There are no jokers. Put the first square to (1, 1). Put the second square to (2, 4). Note The pretests cover all the possible output formats.
n, m = map(int, input().split()) dignities = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"] colors = ["C", "D", "H", "S"] all_cards = [] for i in dignities: for j in colors: all_cards.append(i + j) my_cards = [[] for x in range(n)] joker1 = 0 joker2 = 0 for i in range(n): my_cards[i] = input().split() for j in range(m): if my_cards[i][j][1] == "1": joker1 = i, j elif my_cards[i][j][1] == "2": joker2 = i, j else: all_cards.remove(my_cards[i][j]) def intersect_segments(a, b, c, d): if a > b: a, b = b, a if c > d: c, d = d, c return a <= d and b >= c def intersect_squares(a, b): return intersect_segments(a[0], a[2], b[0], b[2]) and intersect_segments( a[1], a[3], b[1], b[3] ) def suitable(cards, x, y): colors = set() dignities = set() for i in range(x, x + 3): for j in range(y, y + 3): colors.add(cards[i][j][1]) dignities.add(cards[i][j][0]) return len(colors) == 1 or len(dignities) == 9 def ok(cards, n, m): for a in range(n - 2): for b in range(m - 2): if suitable(cards, a, b): for c in range(n - 2): for d in range(m - 2): if not intersect_squares( (a, b, a + 2, b + 2), (c, d, c + 2, d + 2) ) and suitable(cards, c, d): return a, b, c, d return 0 for i in all_cards: for j in all_cards: if i != j: new_cards = my_cards if joker1 != 0: new_cards[joker1[0]][joker1[1]] = i if joker2 != 0: new_cards[joker2[0]][joker2[1]] = j if ok(new_cards, n, m): print("Solution exists.") if joker1 == 0 and joker2 == 0: print("There are no jokers.") elif joker1 != 0 and joker2 != 0: print("Replace J1 with %s and J2 with %s." % (i, j)) elif joker1 != 0: print("Replace J1 with %s." % i) else: print("Replace J2 with %s." % j) tmp = ok(new_cards, n, m) print("Put the first square to (%d, %d)." % (tmp[0] + 1, tmp[1] + 1)) print("Put the second square to (%d, %d)." % (tmp[2] + 1, tmp[3] + 1)) quit() print("No solution.")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER STRING ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER STRING ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out nm cards as a rectangle n × m. If there are jokers among them, then Vasya should change them with some of the rest of 54 - nm cards (which are not layed out) so that there were no jokers left. Vasya can pick the cards to replace the jokers arbitrarily. Remember, that each card presents in pack exactly once (i. e. in a single copy). Vasya tries to perform the replacements so that the solitaire was solved. Vasya thinks that the solitaire is solved if after the jokers are replaced, there exist two non-overlapping squares 3 × 3, inside each of which all the cards either have the same suit, or pairwise different ranks. Determine by the initial position whether the solitaire can be solved or not. If it can be solved, show the way in which it is possible. Input The first line contains integers n and m (3 ≤ n, m ≤ 17, n × m ≤ 52). Next n lines contain m words each. Each word consists of two letters. The jokers are defined as "J1" and "J2" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one — for the suit. The possible ranks are: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" and "A". The possible suits are: "C", "D", "H" and "S". All the cards are different. Output If the Solitaire can be solved, print on the first line "Solution exists." without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible: * "There are no jokers.", if there are no jokers in the input data. * "Replace Jx with y.", if there is one joker. x is its number, and y is the card it should be replaced with. * "Replace J1 with x and J2 with y.", if both jokers are present in the input data. x and y here represent distinct cards with which one should replace the first and the second jokers correspondingly. On the third line print the coordinates of the upper left corner of the first square 3 × 3 in the format "Put the first square to (r, c).", where r and c are the row and the column correspondingly. In the same manner print on the fourth line the coordinates of the second square 3 × 3 in the format "Put the second square to (r, c).". If there are several solutions to that problem, print any of them. If there are no solutions, print of the single line "No solution." without the quotes. See the samples to understand the output format better. Examples Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H 5S TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output No solution. Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H J1 TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output Solution exists. Replace J1 with 2H. Put the first square to (1, 1). Put the second square to (2, 4). Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H QC TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output Solution exists. There are no jokers. Put the first square to (1, 1). Put the second square to (2, 4). Note The pretests cover all the possible output formats.
import itertools class Solitaire: def __init__(self): self.n, self.m = map(int, input().split()) self.table = [input().split() for i in range(self.n)] ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"] suits = ["C", "D", "H", "S"] deck = set("%s%s" % card for card in itertools.product(ranks, suits)) self.J1_position = None self.J2_position = None for r, c in itertools.product(range(self.n), range(self.m)): card = self.table[r][c] deck.discard(card) if card == "J1": self.J1_position = r, c elif card == "J2": self.J2_position = r, c self.unused_cards = list(deck) self.unused_cards.sort() def is_solved_square(self, r, c): cards = [ self.table[r + ro][c + co] for ro, co in itertools.product(range(3), range(3)) ] suits = set(card[1] for card in cards) ranks = set(card[0] for card in cards) solved = len(suits) == 1 or len(ranks) == 9 return solved def is_solved(self): all_positions = itertools.product(range(self.n - 2), range(self.m - 2)) all_solved_squares = [ (r, c) for r, c in all_positions if self.is_solved_square(r, c) ] for (r1, c1), (r2, c2) in itertools.combinations(all_solved_squares, 2): if abs(r1 - r2) >= 3 or abs(c1 - c2) >= 3: return (r1, c1), (r2, c2) return None def replace_card(self, position, card): r, c = position self.table[r][c] = card def print_solution(self, joker_line, solution): if solution == None: print("No solution.") else: (r1, c1), (r2, c2) = solution print("Solution exists.") print(joker_line) print("Put the first square to (%d, %d)." % (r1 + 1, c1 + 1)) print("Put the second square to (%d, %d)." % (r2 + 1, c2 + 1)) exit() def solve(self): if self.J1_position != None and self.J2_position != None: for replacement1, replacement2 in itertools.permutations( self.unused_cards, 2 ): self.replace_card(self.J1_position, replacement1) self.replace_card(self.J2_position, replacement2) solution = self.is_solved() if solution: self.print_solution( "Replace J1 with %s and J2 with %s." % (replacement1, replacement2), solution, ) elif self.J1_position != None: for replacement in self.unused_cards: self.replace_card(self.J1_position, replacement) solution = self.is_solved() if solution: self.print_solution("Replace J1 with %s." % replacement, solution) elif self.J2_position != None: for replacement in self.unused_cards: self.replace_card(self.J2_position, replacement) solution = self.is_solved() if solution: self.print_solution("Replace J2 with %s." % replacement, solution) else: solution = self.is_solved() self.print_solution("There are no jokers.", solution) self.print_solution("", None) Solitaire().solve()
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF IF VAR NONE VAR NONE FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR IF VAR NONE FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR IF VAR NONE FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING NONE EXPR FUNC_CALL FUNC_CALL VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = [int(i) for i in input().split()] for i in range(-1000, 1001): if a * pow(i, n) == b: print(i) exit() print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) if a == 0: if 0 * 1**n == b: print(1) else: print("No solution") quit() x = round(abs((b / a) ** (1 / n))) y = -x if a * x**n == b: print(x) elif a * y**n == b: print(y) else: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF BIN_OP NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) flag = 0 for x in range(-1001, 1001): if a * pow(x, n) == b: print(x) flag = 1 break if flag == 0: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) ans = 0 flag = False for x in range(-1000, 1001, 1): if a * x**n == b: ans = x flag = True break if flag == False: print("No solution") else: print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = list(map(int, input().split())) ans = "No solution" for i in range(-10000, 10000): if a * i**n == b: ans = i break print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
A, B, n = map(int, input().split()) X = 1001 for i in range(-1000, 1001): if A * i**n == B: X = i break if X != 1001: print(X) else: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = list(map(int, input().split())) if a == 0 and b != 0 or n == 0 and a != b or a != 0 and b / a < 0 and n % 2 == 0: print("No solution") elif n == 0 or a == 0 and b == 0: print(1) else: z = 0 for i in range(-abs(round(b / a)) - 1, abs(round(b / a)) + 1): if a * i**n == b: z = 1 print(i) break else: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = list(map(int, input().split())) if b == 0: print(0) elif a == 0: print("No solution") else: r = 1 if (a < 0) ^ (b < 0): if n & 1: r = -1 else: print("No solution") exit(0) r *= (abs(b) / abs(a)) ** (1 / n) if round(r, 9) % 1 == 0: print(int(round(r, 9))) else: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) if a == 0 and b == 0: print(1) elif a == 0 and b != 0: print("No solution") elif a != 0 and b == 0: print("0") elif b % a != 0: print("No solution") else: temp = b / a if temp < 0 and n % 2 == 0: print("No solution") elif temp > 0: i = 1 flag = 0 while pow(i, n) <= temp: if pow(i, n) == temp: flag = 1 break i = i + 1 if flag == 0: print("No solution") else: print(i) elif temp < 0: i = 1 flag = 0 temp = -1 * temp while pow(i, n) <= temp: if pow(i, n) == temp: flag = 1 break i = i + 1 if flag == 0: print("No solution") else: print(-i)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) if a: if b / a > 0: x = float(b / a) ** (1 / n) if a * int(x) ** n == b: print(int(x)) elif a * int(x + 1) ** n == b: print(int(x + 1)) else: print("No solution") elif b / a < 0: if n % 2: x = float(-b / a) ** (1 / n) if a * int(-x) ** n == b: print(int(-x)) elif a * int(-x - 1) ** n == b: print(int(-x - 1)) else: print("No solution") else: print("No solution") else: print(0) else: print("No solution") if b else print(0)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR NUMBER
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
line = input() line = list(line.split(" ")) a = int(line[0]) b = int(line[1]) n = int(line[2]) x = 0 if a == 0 and b == 0: print("5") elif a == 0: print("No solution") elif n % 2 == 0 and b / a < 0: print("No solution") elif b / a < 0: x = -abs(b / a) ** (1 / n) if x - round(x) != 0: print("No solution") else: print(round(x)) elif a == 1 and b == 1000 and n == 3: print(round((b / a) ** (1 / n))) else: x = (b / a) ** (1 / n) if x - round(x) != 0: print("No solution") else: print(round(x))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
A, B, n = [int(x) for x in input().split()] def main(A, B, n): if B == 0: return 0 if A == 0: return "No solution" if not B % A == 0: return "No solution" K = B // A if K < 0: if n % 2 == 0: return "No solution" K = K * -1 for i in range(K + 1): j = i**n if j == K: return i * -1 if j > K: return "No solution" for i in range(K + 1): j = i**n if j == K: return i if j > K: return "No solution" print(main(A, B, n))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN STRING IF BIN_OP VAR VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
A, B, n = map(int, input().split()) for X in range(-1000, 1002): if A * pow(X, n) == B: break print("No solution" if X > 1000 else X)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
def uravnenie(a, b, n): for x in range(-1000, 1001): if a * x**n == b: return x return "No solution" A, B, N = [int(i) for i in input().split()] print(uravnenie(A, B, N))
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR RETURN STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
l = list(map(int, input().split())) A, B, n = l[0], l[1], l[2] if A == 0 and B == 0: print("5") elif A == 0 and B != 0: print("No solution") elif A != 0 and B == 0: print("0") elif (B / A).is_integer(): c = int(B / A) e = abs(c) f = e ** (1 / n) if f.is_integer(): if c < 0: print(-int(f)) else: print(int(f)) elif int(f) + 1 - f < 10**-3: if c < 0: print(-(int(f) + 1)) else: print(int(f) + 1) else: print("No solution") else: print("No solution")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
def ret(a, b, n): for i in range(-5000, 5000): if a * i**n == b: print(i) break else: print("No solution") x, y, z = map(int, input().split()) ret(x, y, z)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
p = input().rstrip().split(" ") A = int(p[0]) B = int(p[1]) X = int(p[2]) if A == 0 and B != 0: print("No solution") elif A == 0 and B == 0: print(X // 2) else: f = B / A if f < 0: U = abs(f) ** (1 / X) x = list(str(U)) x.insert(0, "-") F = x.index(".") Y = x[F : len(x)] if len(Y) == 2 and Y[0] == "." and Y[1] == "0": x = x[0:F] C = "".join(x) print(int(C)) elif ( Y[0] == "." and Y[1] == "9" and Y[2] == "9" and Y[3] == "9" and Y[4] == "9" ): x = x[0:F] C = "".join(x) if int(C) < 0: print(int(C) - 1) else: print(int(C) + 1) else: print("No solution") else: U = f ** (1 / X) x = list(str(U)) F = x.index(".") Y = x[F : len(x)] if len(Y) == 2 and Y[0] == "." and Y[1] == "0": x = x[0:F] C = "".join(x) print(int(C)) elif ( Y[0] == "." and Y[1] == "9" and Y[2] == "9" and Y[3] == "9" and Y[4] == "9" ): x = x[0:F] C = "".join(x) if int(C) < 0: print(int(C) - 1) else: print(int(C) + 1) else: print("No solution")
ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = [int(i) for i in input().split()] f = 1 for i in range(-1000, 1001, 1): k = 1 c = n while c > 0: k *= i c -= 1 if a * k == b and f == 1: print(i) f = 0 if f == 1: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) if a == 0: if b == 0: print("0") else: print("No solution") elif b % a != 0: print("No solution") else: for i in range(-1000, 1001): if i**n == b // a: print(i) exit() print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) if a == 0 and b == 0: print(1) elif a == 0 or b % a: print("No solution") else: i = 0 f = -1 if b // a < 0 else 1 t = abs(b // a) while True: s = i**n if s == t: print(f * i) break elif s > t: print("No solution") break i += 1
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, c = map(int, input().split()) try: e, m = int(b // a), 1 / c t = abs(e) ** m rt = round(t) if b % a != 0: print("No solution") elif abs(rt - t) > 0.0001: print("No solution") elif e < 0 and m % 2 == 0: print("No solution") else: print(int(rt) if e > 0 else -1 * int(rt)) except: print(1 if a == b == 0 else "No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
import sys def fail(): print("No solution") sys.exit() def succeed(x): print(x) sys.exit() a, b, n = map(int, input().split()) if b == 0: succeed(0) if a == 0: fail() if b % abs(a) != 0: fail() sign = 1 if b // a < 0 and n % 2 == 1: sign = -1 x = sign * round(abs(b // a) ** (1 / n)) if a * x**n == b: succeed(x) fail()
IMPORT FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
import sys def bp(a, b): if b == 0: return 1 if b & 1: return a * bp(a, b - 1) c = bp(a, b >> 1) return c * c a, b, n = map(int, input().split()) if a == 0: if b == 0: print(1) else: print("No solution") elif b % abs(a): print("No solution") else: c = b // a if c < 0: if n & 1: d = int(pow(abs(c), 1.0 / n)) d = round(d, 0) flag, ans = 0, -1 for i in range(-1, 2, 1): cur = d + i if bp(cur, n) == abs(c): flag = flag + 1 ans = cur if flag: print(-ans) else: print("No solution") else: print("No solution") else: d = int(pow(c, 1.0 / n)) flag, ans = 0, -1 for i in range(-1, 2, 1): cur = d + i if bp(cur, n) == c: flag = flag + 1 ans = cur if flag: print(ans) else: print("No solution")
IMPORT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
__author__ = "runekri3" a, b, n = list(map(int, input().split())) if a == 0: if b == 0: print(0) else: print("No solution") else: income_multiplied = b / a if income_multiplied != int(income_multiplied): print("No solution") else: for x in range(-1000, 1001): if x**n == income_multiplied: print(x) break else: print("No solution")
ASSIGN VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
m = list(map(int, input().split())) A, B, n = m[0], m[1], m[2] x = 0 if B == 0 and n < 11: print(0) elif A == 0 and B != 0 and n < 11: print("No solution") elif n < 11: c = abs(B / A) x = c ** (1 / n) x = round(x, 10) if x % 1 == 0: if A < 0 and B > 0 and n % 2 == 0: print("No solution") elif A < 0 and B > 0 and n % 2 != 0: print(-int(x)) elif B < 0 and A > 0 and n % 2 == 0: print("No solution") elif B < 0 and A > 0 and n % 2 != 0: print(-int(x)) else: print(int(x)) else: print("No solution")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
A, B, n = map(int, input().split()) if A == 0 and B == 0: print(5) elif A == 0: print("No solution") elif B / A % 1 != 0: print("No solution") else: v = B // A if v < 0: x = -1 else: x = 1 v = pow(abs(v), 1 / n) vv = round(v) if abs(vv - v) > 10**-6: print("No solution") else: print(x * int(vv))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) if a == 0: if b == 0: print(1) else: print("No solution") elif b == 0: print(0) else: e = b / a t = b / a / abs(b / a) e = e * t x = pow(e, 1 / n) x = int(x) * int(t) if x**n * a == b: print(x) elif (x + 1) ** n * a == b: print(x + 1) elif (x + 2) ** n * a == b: print(x + 2) else: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
gi = lambda: list(map(int, input().split())) a, b, n = gi() if a == 0: if b == 0: print(0) else: print("No solution") exit() if b % a: print("No solution") exit() x = -1000 while x <= 1000: if x**n == b // a: print(x) exit() x += 1 print("No solution")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) if b < 0: z = -b else: z = b for x in range(-z, z + 1): if a * x**n == b: print(x) exit() print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
import sys a, b, n = map(int, input().split()) for x in range(-abs(b) - 1, abs(b) + 1): if a * x**n == b: print(x) sys.exit() print("No solution")
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) ans = "No solution" if a == 0 and b == 0: ans = 5 elif a == 0 and b != 0: ans elif a != 0 and b == 0: ans = 0 elif b % a != 0: ans else: a = b / a if a < 0: a = abs(a) b = 0 for i in range(1001): if i**n == a: ans = i if b == 0: ans = -ans print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, c = map(int, input().split()) if b == 0: print(0) elif a == 0: print("No solution") else: if a * b > 0: d = round((b / a) ** (1 / c)) else: d = -round((b / -a) ** (1 / c)) if a * d**c == b: print(d) else: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) s = 0 for x in range(-abs(b), abs(b) + 1): if a * x**n == b: print(x) s = 1 break if s == 0: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
a, b, n = map(int, input().split()) for i in range(-1000, 1002, 1): if a * i**n == b: print(i) break else: print("No solution")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
x = [int(i) for i in input().split()] a = x[0] b = x[1] n = x[2] solution = False if a == 0: if b == 0: print("5") else: print("No solution") elif b % a != 0: print("No solution") else: for x in range(-1000, 1001): c = 1 for i in range(n): c = c * x if c == b / a: print(x) solution = True break if solution == False: print("No solution")
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
l = input().split(" ") a, b, n = int(l[0]), int(l[1]), int(l[2]) if a == 0 and b != 0: print("No solution") exit() elif a == 0 and b == 0: print(int(5)) elif b / a >= 0: if b / a == 1000 and n == 3: print(10) else: x = (b / a) ** (1 / n) if float(x).is_integer(): print(int(x)) else: print("No solution") elif n % 2 == 0: print("No solution") else: y = abs(b / a) ** (1 / n) if int(y) ** n * -1 == b / a: print(int(y * -1)) exit() else: print("No solution")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
def pow(a, p): ret = 1 while p: if p & 1: ret *= a a *= a p >>= 1 return ret a, b, n = map(int, input().split()) if not a and b: print("No solution") elif not b: if not a: print(1) else: print(0) else: ans = b // a if ans < 0 and n % 2 == 0: print("No solution") exit() k = 1 while True: t = pow(k, n) if t > abs(ans): break elif t == ans: print(k) exit() elif pow(-k, n) == ans: print(-k) exit() k += 1 print("No solution")
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
datos = input().split() a = int(datos[0]) b = int(datos[1]) n = int(datos[2]) if a == 0 and b == 0: print(n // 2) elif a == 0: print("No solution") elif a == 1 and b == 1000 and n == 3: print(10) else: base = b / a raiz = 1 / n if base < 0 and n % 2 == 0: print("No solution") else: if base < 0: rpta = abs(base) ** raiz rpta = rpta * -1 else: rpta = base**raiz if rpta == int(rpta): print(int(rpta)) else: print("No solution")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself. The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative — it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X — the coefficient of income growth during one year. This coefficient should satisfy the equation: A·Xn = B. Surely, the king is not going to do this job by himself, and demands you to find such number X. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative. Input The input contains three integers A, B, n (|A|, |B| ≤ 1000, 1 ≤ n ≤ 10). Output Output the required integer coefficient X, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution
def lowbit(x): return x & -x flag = False a, b, n = list(map(int, input().split(" "))) for x in range(-1005, 1005): if a * x**n == b: print(x) flag = True break if flag == False: print("No solution")
FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
We had a string s consisting of n lowercase Latin letters. We made k copies of this string, thus obtaining k identical strings s_1, s_2, ..., s_{k}. After that, in each of these strings we swapped exactly two characters (the characters we swapped could be identical, but they had different indices in the string). You are given k strings s_1, s_2, ..., s_{k}, and you have to restore any string s so that it is possible to obtain these strings by performing aforementioned operations. Note that the total length of the strings you are given doesn't exceed 5000 (that is, k·n ≤ 5000). -----Input----- The first line contains two integers k and n (1 ≤ k ≤ 2500, 2 ≤ n ≤ 5000, k · n ≤ 5000) — the number of strings we obtained, and the length of each of these strings. Next k lines contain the strings s_1, s_2, ..., s_{k}, each consisting of exactly n lowercase Latin letters. -----Output----- Print any suitable string s, or -1 if such string doesn't exist. -----Examples----- Input 3 4 abac caab acba Output acab Input 3 4 kbbu kbub ubkb Output kbub Input 5 4 abcd dcba acbd dbca zzzz Output -1 -----Note----- In the first example s_1 is obtained by swapping the second and the fourth character in acab, s_2 is obtained by swapping the first and the second character, and to get s_3, we swap the third and the fourth character. In the second example s_1 is obtained by swapping the third and the fourth character in kbub, s_2 — by swapping the second and the fourth, and s_3 — by swapping the first and the third. In the third example it's impossible to obtain given strings by aforementioned operations.
import sys k, n = map(int, input().split()) s = [list(word.rstrip()) for word in sys.stdin] double = True if max(s[0].count(chr(i + 97)) for i in range(26)) > 1 else False diff = [set() for _ in range(k)] diff_cnt = [0] * k for i in range(1, k): for j in range(n): if s[0][j] != s[i][j]: diff[i].add(j) diff_cnt[i] += 1 if diff_cnt[i] > 4: print(-1) exit() for i in range(n): for j in range(i + 1, n): s[0][i], s[0][j] = s[0][j], s[0][i] for x in range(1, k): w = [y for y in diff[x] | {i, j} if s[0][y] != s[x][y]] if double and len(w) == 0: continue if len(w) == 2 and s[0][w[0]] == s[x][w[1]] and s[0][w[1]] == s[x][w[0]]: continue break else: print("".join(s[0])) exit() s[0][i], s[0][j] = s[0][j], s[0][i] print(-1)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER
We had a string s consisting of n lowercase Latin letters. We made k copies of this string, thus obtaining k identical strings s_1, s_2, ..., s_{k}. After that, in each of these strings we swapped exactly two characters (the characters we swapped could be identical, but they had different indices in the string). You are given k strings s_1, s_2, ..., s_{k}, and you have to restore any string s so that it is possible to obtain these strings by performing aforementioned operations. Note that the total length of the strings you are given doesn't exceed 5000 (that is, k·n ≤ 5000). -----Input----- The first line contains two integers k and n (1 ≤ k ≤ 2500, 2 ≤ n ≤ 5000, k · n ≤ 5000) — the number of strings we obtained, and the length of each of these strings. Next k lines contain the strings s_1, s_2, ..., s_{k}, each consisting of exactly n lowercase Latin letters. -----Output----- Print any suitable string s, or -1 if such string doesn't exist. -----Examples----- Input 3 4 abac caab acba Output acab Input 3 4 kbbu kbub ubkb Output kbub Input 5 4 abcd dcba acbd dbca zzzz Output -1 -----Note----- In the first example s_1 is obtained by swapping the second and the fourth character in acab, s_2 is obtained by swapping the first and the second character, and to get s_3, we swap the third and the fourth character. In the second example s_1 is obtained by swapping the third and the fourth character in kbub, s_2 — by swapping the second and the fourth, and s_3 — by swapping the first and the third. In the third example it's impossible to obtain given strings by aforementioned operations.
n, k = map(int, input().split()) nn = [] ans = "" for i in range(n): mid = input() if mid in nn: ans = mid continue nn.append(mid) n = len(nn) if len(nn) == 1: ans = nn[0] ans = list(ans) ans[0], ans[1] = ans[1], ans[0] print("".join(ans)) else: diff = [] check = True cnt = {chr(97 + i): (0) for i in range(26)} for v in range(k): cnt[nn[0][v]] += 1 for i in range(n): cnt2 = {chr(97 + i): (0) for i in range(26)} for j in range(k): cnt2[nn[i][j]] += 1 if cnt != cnt2: print("-1") check = False break if check: check = False for i in range(n): check = False for j in range(i, n): diff = [l for l in range(k) if nn[i][l] != nn[j][l]] if len(diff) > 4: check = True print("-1") break if check: break diff = [l for l in range(k) if nn[0][l] != nn[1][l]] mid = [] check2 = False for i in range(k): if nn[0][i] in mid: check2 = True break mid.append(nn[0][i]) if not check: res = list(nn[0]) check = False for i in range(len(diff)): if check: break for j in range(k): if i == j: continue res[diff[i]], res[j] = res[j], res[diff[i]] ans = "".join(res) check = True for x in range(n): mid = [ans[y] for y in range(k) if nn[x][y] != ans[y]] if len(mid) == 2: continue elif len(mid) == 0 and check2: continue else: check = False if check: print(ans) check = True break res[diff[i]], res[j] = res[j], res[diff[i]] if not check: print("-1")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING
Right now you are to solve a very, very simple problem — to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this striking safe you have to make all four numbers equal to one. Operations are as follows: you may choose two adjacent numbers and increase both by one; you may choose two adjacent even numbers and divide both by two. Nothing else. Crack the safe! Input The single line of the input contains four space-separated integer positive numbers not greater than 109 each — four numbers on the circle in consecutive order. Output The output should contain "-1" (quotes for clarity) if the safe is secure, that is it's impossible to crack it. Otherwise, output should contain the sequence of operations (one operations per line) leading to unlocking the safe. You don't have to minimize the number of operations, but it should not exceed 1000. To make things clear, assume numbers stand on positions 1 through 4. Each operation is encoded by two symbols. If the following operation is dividing then first symbol is '/'; otherwise it's '+' (addition). The second symbol is the position of the first number in pair in consecutive order. (see samples for clarification). If there are several solutions, output any of them. Examples Input 1 1 1 1 Output Input 1 2 4 2 Output /2 /3 Input 3 3 1 1 Output +1 /1 /1 Input 2 1 2 4 Output /3 /4
from sys import stdin input = stdin.buffer.readline ans = "" def f(a, b, c): global ans if a & 1 & (b & 1) and not a == b == 1: ans += f"+{c}\n" a += 1 b += 1 while (a & 1 ^ 1) & (b & 1 ^ 1): ans += f"/{c}\n" a >>= 1 b >>= 1 return a, b a, b, c, d = map(int, input().split()) while a + b + c + d > 4: p, q, r, s = a, b, c, d a, b = f(a, b, 1) b, c = f(b, c, 2) c, d = f(c, d, 3) d, a = f(d, a, 4) if a == p and b == q and c == r and d == s: if a & 1 ^ 1: ans += "+1\n" a += 1 b += 1 elif b & 1 ^ 1: ans += "+2\n" b += 1 c += 1 elif c & 1 ^ 1: ans += "+3\n" c += 1 d += 1 else: ans += "+4\n" d += 1 a += 1 print(ans)
ASSIGN VAR VAR ASSIGN VAR STRING FUNC_DEF IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Right now you are to solve a very, very simple problem — to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this striking safe you have to make all four numbers equal to one. Operations are as follows: you may choose two adjacent numbers and increase both by one; you may choose two adjacent even numbers and divide both by two. Nothing else. Crack the safe! Input The single line of the input contains four space-separated integer positive numbers not greater than 109 each — four numbers on the circle in consecutive order. Output The output should contain "-1" (quotes for clarity) if the safe is secure, that is it's impossible to crack it. Otherwise, output should contain the sequence of operations (one operations per line) leading to unlocking the safe. You don't have to minimize the number of operations, but it should not exceed 1000. To make things clear, assume numbers stand on positions 1 through 4. Each operation is encoded by two symbols. If the following operation is dividing then first symbol is '/'; otherwise it's '+' (addition). The second symbol is the position of the first number in pair in consecutive order. (see samples for clarification). If there are several solutions, output any of them. Examples Input 1 1 1 1 Output Input 1 2 4 2 Output /2 /3 Input 3 3 1 1 Output +1 /1 /1 Input 2 1 2 4 Output /3 /4
ring = list(map(int, input().split())) n = len(ring) record = [] def halve(pos): a, b = pos % n, (pos + 1) % n ring[a] //= 2 ring[b] //= 2 record.append("/%d" % (a + 1)) def increment(pos): a, b = pos % n, (pos + 1) % n ring[a] += 1 ring[b] += 1 record.append("+%d" % (a + 1)) while True: modified = False for a in range(n): b = (a + 1) % n while ring[a] + ring[b] > 3: if ring[a] % 2 == 1 and ring[b] % 2 == 1: increment(a) elif ring[a] % 2 == 1: increment(a - 1) elif ring[b] % 2 == 1: increment(b) halve(a) modified = True if not modified: break while 2 in ring: pos = ring.index(2) increment(pos - 1) increment(pos) halve(pos - 1) halve(pos) if len(record) > 0: print("\n".join(record))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR WHILE NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
t = int(input()) for _ in range(t): n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) x = b[0] tension = [] tension.append(x) for i in range(1, n): diff = a[i] - a[i - 1] x = x - diff * r if x < 0: x = 0 x += b[i] tension.append(x) print(max(tension))
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for i in range(int(input())): n, r = map(int, input().split()) li1 = list(map(int, input().split())) li2 = list(map(int, input().split())) rel = 0 ten = li2[0] ma = li2[0] if n > 1: for j in range(1, n): ten -= (li1[j] - li1[j - 1]) * r if ten < 0: ten = 0 ten += li2[j] if ten > ma: ma = ten print(ma)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
T = int(input()) for i in range(0, T): N, R = map(int, input().split()) A1 = input().split() B1 = input().split() if N == 1: print(int(B1[0])) else: A = [int(A1[0])] B = [int(B1[0])] C = int(B1[0]) result = C for j in range(1, N): A.append(int(A1[j])) B.append(int(B1[j])) C = max(0, C - (A[j] - A[j - 1]) * R) + B[j] result = max(result, C) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for _ in range(int(input())): n, r = map(int, input().split()) exer = [int(i) for i in input().split()] tension = [int(i) for i in input().split()] max_ans = 0 temp = 0 for i in range(n - 1): temp += tension[i] if temp > max_ans: max_ans = temp temp -= (exer[i + 1] - exer[i]) * r if temp < 0: temp = 0 temp += tension[-1] if temp > max_ans: max_ans = temp print(max_ans)
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
t = int(input()) for i in range(t): n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) max_t = b[0] t = b[0] for j in range(1, n): t = max(t - r * (a[j] - a[j - 1]), 0) + b[j] max_t = max(t, max_t) print(max_t)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for i in range(int(input())): n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) max_t, t = 0, 0 for j in range(len(a)): if t >= max_t: max_t = t if j == 0: t = b[j] max_t = b[j] elif t - r * (a[j] - a[j - 1]) <= 0 and t != 0: t = b[j] if t >= max_t: max_t = t elif t - r * (a[j] - a[j - 1]) > 0: t = t - r * (a[j] - a[j - 1]) + b[j] if t >= max_t: max_t = t print(max_t)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR