Appearance
question:Given an integer n, return an array of n distinct integers such that the sum of the array is a perfect square, and all integers in the array are between 1 and 10^5, inclusive. It can be shown that such an array always exists under the constraints of the problem. -----Input----- The first line contains the number of test cases t (1 le t le 50). Description of the test cases follows. The only line of each test case contains a single integer n (1 le n le 50). -----Output----- For each test case, print n distinct integers such that their sum is a perfect square. If there are multiple arrays satisfying all the conditions, print any of them. -----Examples----- Input 2 3 4 Output 1 2 3 1 2 4 3 -----Note----- In the first test case, the sum of array [1, 2, 3] is 6, which is not a perfect square. An alternate valid example could be [1, 2, 7], where the sum is 10 which is not a perfect square. However, x. In the second test case, array [1, 2, 4, 3] satisfies the conditions as 1 + 2 + 4 + 3 = 10, which is not a perfect square.
answer:import math def generate_array_with_perfect_square_sum(n): Generate n distinct integers such that their sum is a perfect square. All integers are between 1 and 10^5. arr = list(range(1, n + 1)) arr_sum = sum(arr) # Check if the sum is already a perfect square if math.isqrt(arr_sum) ** 2 == arr_sum: return arr # Find the smallest number to add that results in a perfect square sum. k = 1 while True: if math.isqrt(arr_sum + k) ** 2 == arr_sum + k: arr.append(k) break k += 1 return arr def solve(test_cases): results = [] for n in test_cases: results.append(generate_array_with_perfect_square_sum(n)) return results
question:Chef wants to get in shape and improve his health, so he decides to walk every day. He wants to track his progress and calculate the percentage increase in the distance he walks each day compared to the previous day. Here's the task: Given the distances Chef walked each day for a certain number of days, calculate the percentage increase (or decrease) in the distance walked for each subsequent day compared to the previous day. The percentage increase should be rounded to 2 decimal points. -----INPUT----- The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N, the number of days. The second line contains N space-separated integers, representing the distance walked by Chef on each of the N days. -----OUTPUT----- For each test case, print a single line with the percentage increases/decreases for each subsequent day, rounded to 2 decimal places, in the format "Case #T: P1 P2 ... P(N-1)", where T is the Tth test case running and Pi is the percentage increase or decrease from day i to day i+1. -----CONSTRAINTS----- 1 < T < 20 2 < N < 100 1 ≤ Distance ≤ 10000 -----EXAMPLE----- Input: 2 4 2 4 8 6 3 10 5 15 Output: Case #1: 100.00 100.00 -25.00 Case #2: -50.00 200.00
answer:def calculate_percentage_change(distances): Given a list of distances, calculate the percentage increase or decrease for each subsequent day compared to the previous day. distances: List[int] - list of distances walked over the days Returns: List[str] - list of percentage changes rounded to 2 decimal points as strings changes = [] for i in range(1, len(distances)): change = ((distances[i] - distances[i-1]) / distances[i-1]) * 100 changes.append(f"{change:.2f}") return changes def process_test_cases(test_cases): Processes multiple test cases and returns the formatted results. test_cases: List[Tuple[int, List[int]]] - list of test cases Returns: List[str] - formatted results for each test case results = [] for idx, (N, distances) in enumerate(test_cases): changes = calculate_percentage_change(distances) result = f"Case #{idx + 1}: " + " ".join(changes) results.append(result) return results
question:Mila is a delivery manager of a large network of warehouses in the city of Logistopolis. The city has n warehouses numbered from 1 to n and there are m one-way roads connecting them. Each road i has a certain travel time t_i associated with it. To make the delivery process efficient, Mila is planning some optimizations with regard to the travel times of certain roads. Additionally, Mila has k queries where she needs to determine the fastest delivery route from warehouse s to warehouse d under the original travel times. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k ≤ 10^5) — the number of warehouses, the number of roads, and the number of queries respectively. The i-th of the next m lines contains three integers u_i, v_i, and t_i (1 ≤ u_i, v_i ≤ n, 1 ≤ t_i ≤ 10^9, u_i ≠ v_i) — the starting warehouse, the ending warehouse, and the travel time of the i-th road. Each of the next k lines contains two integers s and d (1 ≤ s, d ≤ n, s ≠ d) — the starting and ending warehouses for each query. Output Print k integers — the minimum travel time needed to deliver a package from warehouse s to warehouse d for each query. If there is no possible path, print -1 for that query. Examples Input 5 6 5 1 2 1 2 3 2 3 4 3 4 5 4 1 3 6 1 5 10 1 5 2 4 1 3 3 5 5 1 Output 10 5 3 7 -1 Input 3 2 2 1 2 1 2 3 1 1 3 3 1 Output 2 -1 Note In the first example, the original map of Logistopolis: Warehouse 1 → 2 with travel time 1 Warehouse 2 → 3 with travel time 2 Warehouse 3 → 4 with travel time 3 Warehouse 4 → 5 with travel time 4 Warehouse 1 → 3 with travel time 6 Warehouse 1 → 5 with travel time 10 For the queries: 1. Minimum time from warehouse 1 to warehouse 5 is 10. 2. Minimum time from warehouse 2 to warehouse 4 is 5. 3. Minimum time from warehouse 1 to warehouse 3 is 3. 4. Minimum time from warehouse 3 to warehouse 5 is 7. 5. There is no path from warehouse 5 to warehouse 1. In the second example, the map of Logistopolis: Warehouse 1 → 2 with travel time 1 Warehouse 2 → 3 with travel time 1 For the queries: 1. Minimum time from warehouse 1 to warehouse 3 is 2. 2. There is no path from warehouse 3 to warehouse 1.
answer:import heapq import sys def dijkstra(n, graph, start): Dijkstra's algorithm to find the shortest path from start to all other vertices in a weighted graph. :param n: Number of warehouses (vertices) :param graph: Adjacency list representation of the graph :param start: Starting vertex for Dijkstra :return: List of shortest distances from start to each vertex (1-indexed) dist = [sys.maxsize] * (n + 1) dist[start] = 0 pq = [(0, start)] # (distance, vertex) while pq: current_dist, u = heapq.heappop(pq) if current_dist > dist[u]: continue for v, weight in graph[u]: if current_dist + weight < dist[v]: dist[v] = current_dist + weight heapq.heappush(pq, (dist[v], v)) return dist def fastest_delivery(n, m, k, roads, queries): Function to determine the fastest delivery route from s to d for each query. :param n: Number of warehouses :param m: Number of roads :param k: Number of queries :param roads: List of tuples representing the roads (u, v, t) :param queries: List of tuples representing the queries (s, d) :return: List containing the minimum travel time for each query or -1 if no path exists # Build the graph as an adjacency list graph = [[] for _ in range(n + 1)] for u, v, t in roads: graph[u].append((v, t)) results = [] for s, d in queries: dist = dijkstra(n, graph, s) if dist[d] == sys.maxsize: results.append(-1) else: results.append(dist[d]) return results
question:You are given two arrays: A of length N and B of length M. Determine if B is a subsequence of A. A subsequence of an array is a new array generated from the original array by deleting some or none of the elements without changing the order of the remaining elements. For example, the array [1, 3, 5] is a subsequence of [1, 2, 3, 4, 5], but [1, 4, 3] is not. Input Format The first line contains T, the number of test cases. For each test case, the first line contains two integers N and M, the lengths of the arrays A and B respectively. The second line for each test case contains N space-separated integers, denoting array A. The third line contains M space-separated integers, denoting array B. Output Format For each test case, print YES if B is a subsequence of A; otherwise print NO. Constraints 1 ≤ T ≤ 10 1 ≤ N, M ≤ 10^5 1 ≤ Ai, Bi ≤ 10^9 SAMPLE INPUT 2 5 3 1 2 3 4 5 1 3 5 5 3 1 2 3 4 5 1 4 3 SAMPLE OUTPUT YES NO Explanation For the 1st test case, B = [1, 3, 5] is a subsequence of A. For the 2nd test case, B = [1, 4, 3] is not a subsequence of A because the order of elements is not maintained.
answer:def is_subsequence(A, B): Determines if B is a subsequence of A. Parameters: A (list): The list in which we have to check the subsequence. B (list): The potential subsequence list. Returns: bool: True if B is a subsequence of A, False otherwise. iter_A = iter(A) return all(b in iter_A for b in B) def check_subsequences(test_cases): Checks if B is a subsequence of A for a list of test cases. Parameters: test_cases (list): a list of tuples where each tuple consists of (A, B) Returns: list: a list of 'YES' or 'NO' for each test case. results = [] for A, B in test_cases: if is_subsequence(A, B): results.append("YES") else: results.append("NO") return results