Compare commits
10
Commits
32b04527ab
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c866b67fa3 | ||
|
|
c21848e7f0 | ||
|
|
97343ec7e8 | ||
|
|
5b3351c2b8 | ||
|
|
ba2d3e9bf6 | ||
|
|
4d4fcd7c00 | ||
|
|
c1f241ff5a | ||
|
|
dea42ea39c | ||
|
|
156b2eaa64 | ||
|
|
2b1b79a3c7 |
No files matched your search
+54
@@ -0,0 +1,54 @@
|
||||
def test_distance(cows: int, locations: list[int], distance: int) -> bool:
|
||||
placed_cows = 1
|
||||
last = locations[0]
|
||||
for location in locations[1:]:
|
||||
if location - last >= distance:
|
||||
last = location
|
||||
placed_cows += 1
|
||||
|
||||
if placed_cows >= cows:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def main():
|
||||
test_cases: int = int(input())
|
||||
|
||||
while test_cases != 0:
|
||||
|
||||
stalls, cows = [int(n) for n in input().split()]
|
||||
|
||||
locations = []
|
||||
i = 0
|
||||
while i < stalls:
|
||||
|
||||
locations.append(int(input()))
|
||||
|
||||
i += 1
|
||||
|
||||
locations.sort()
|
||||
|
||||
l = locations[1] - locations[0]
|
||||
r = max(locations) - min(locations)
|
||||
m = (l + r) // 2
|
||||
|
||||
while True:
|
||||
old_m = m
|
||||
|
||||
# Current split worked, so try more
|
||||
if test_distance(cows, locations, m):
|
||||
l = m + 1
|
||||
else:
|
||||
r = m - 1
|
||||
|
||||
m = (l + r) // 2
|
||||
|
||||
if m == old_m:
|
||||
print(m)
|
||||
break
|
||||
|
||||
|
||||
test_cases -= 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
def find_closing_p(s: str, opening_index: int) -> int:
|
||||
skips = 0
|
||||
for i, c in enumerate(s[opening_index + 1:]):
|
||||
match c:
|
||||
case ')':
|
||||
if skips != 0:
|
||||
skips -= 1
|
||||
else:
|
||||
return i + opening_index + 1
|
||||
case '(':
|
||||
skips += 1
|
||||
|
||||
|
||||
def do_math(s: str) -> int:
|
||||
tokens = []
|
||||
|
||||
i = 0
|
||||
for c in s:
|
||||
if c.isdigit():
|
||||
try:
|
||||
tokens[i] += c
|
||||
except IndexError:
|
||||
tokens.append(c)
|
||||
elif c in {'+', '-', '*', '/'}:
|
||||
i += 1
|
||||
tokens.append(c)
|
||||
|
||||
last_piece = tokens.pop(0)
|
||||
result = 0
|
||||
for piece in tokens:
|
||||
result = eval(f'{last_piece}{piece}')
|
||||
last_piece = str(result)
|
||||
return result
|
||||
|
||||
def do_parentheses(s: str) -> str:
|
||||
if '(' in s:
|
||||
opening_index: int = s.find('(')
|
||||
closing_index: int = find_closing_p(s, opening_index)
|
||||
|
||||
sub_snp = s[opening_index + 1:closing_index]
|
||||
|
||||
return str(do_math(s[:opening_index] + do_parentheses(sub_snp)))
|
||||
|
||||
else:
|
||||
if len(s) == 1:
|
||||
return s
|
||||
else:
|
||||
return str(do_math(s))
|
||||
|
||||
|
||||
# (5*6)-(4+(1))
|
||||
def main():
|
||||
s: str = input()
|
||||
|
||||
# Wish I could just use eval() but it uses pemdas
|
||||
|
||||
# Evaluate all parentheses
|
||||
while '(' in s:
|
||||
opening_index: int = s.find('(')
|
||||
closing_index: int = find_closing_p(s, opening_index)
|
||||
|
||||
sub_snp = s[opening_index + 1:closing_index]
|
||||
sub_s = s[opening_index:closing_index + 1]
|
||||
|
||||
s = s.replace(sub_s, do_parentheses(sub_snp))
|
||||
|
||||
print(do_math(s))
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,69 @@
|
||||
# Returns result and index
|
||||
def para(s: str, i: int) -> tuple[int, int]:
|
||||
a, b = 0, 0
|
||||
operator = '+'
|
||||
c = s[i]
|
||||
while c != ')':
|
||||
c = s[i]
|
||||
if c == '(':
|
||||
i += 1
|
||||
b, i = para(s, i)
|
||||
match operator:
|
||||
case '+':
|
||||
a = a + b
|
||||
case '-':
|
||||
a = a - b
|
||||
case '*':
|
||||
a = a * b
|
||||
elif c.isdigit():
|
||||
b = int(c)
|
||||
match operator:
|
||||
case '+':
|
||||
a = a + b
|
||||
case '-':
|
||||
a = a - b
|
||||
case '*':
|
||||
a = a * b
|
||||
i += 1
|
||||
else:
|
||||
operator = c
|
||||
i += 1
|
||||
|
||||
return (a, i)
|
||||
|
||||
def main():
|
||||
s: str = input()
|
||||
|
||||
# Wish I could just use eval() but it uses pemdas
|
||||
a, b = 0, 0
|
||||
operator = '+'
|
||||
i = 0
|
||||
while i < len(s):
|
||||
c = s[i]
|
||||
if c == '(':
|
||||
i += 1
|
||||
b, i = para(s, i)
|
||||
match operator:
|
||||
case '+':
|
||||
a = a + b
|
||||
case '-':
|
||||
a = a - b
|
||||
case '*':
|
||||
a = a * b
|
||||
elif c.isdigit():
|
||||
b = int(c)
|
||||
match operator:
|
||||
case '+':
|
||||
a = a + b
|
||||
case '-':
|
||||
a = a - b
|
||||
case '*':
|
||||
a = a * b
|
||||
i += 1
|
||||
else:
|
||||
operator = c
|
||||
i += 1
|
||||
print(a)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
File renamed without changes.
@@ -18,12 +18,15 @@ int main() {
|
||||
int total = 1;
|
||||
int r = (int) std::floor(std::sqrt(n));
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
for (int n2 = 2; n2 < r + 1; n2++) {
|
||||
if (n % n2 == 0) {
|
||||
int other = n / n2;
|
||||
|
||||
if (other != n)
|
||||
if (other != n2) {
|
||||
total += other;
|
||||
}
|
||||
|
||||
total += n2;
|
||||
}
|
||||
Binary file not shown.
@@ -1,23 +0,0 @@
|
||||
def main():
|
||||
lines: list[str] = []
|
||||
t = int(input())
|
||||
while (t != 0):
|
||||
lines.append(input())
|
||||
t -= 1
|
||||
|
||||
print()
|
||||
for line in lines:
|
||||
n = int(line)
|
||||
|
||||
if n == 1:
|
||||
print(0)
|
||||
continue
|
||||
|
||||
total = 1
|
||||
for n2 in reversed(range(2, n)):
|
||||
total += n2 if n % n2 == 0 else 0
|
||||
|
||||
print(total)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,27 +0,0 @@
|
||||
|
||||
d = {1 : 0}
|
||||
|
||||
|
||||
def main():
|
||||
lines: list[str] = []
|
||||
t = int(input())
|
||||
while (t != 0):
|
||||
lines.append(input())
|
||||
t -= 1
|
||||
|
||||
print()
|
||||
for line in lines:
|
||||
n = int(line)
|
||||
|
||||
if n == 1:
|
||||
print(0)
|
||||
continue
|
||||
|
||||
total = 1
|
||||
for n2 in reversed(range(2, n)):
|
||||
total += n2 if n % n2 == 0 else 0
|
||||
|
||||
print(total)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,36 +0,0 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
int main() {
|
||||
int t = 200000;
|
||||
// std::scanf("%d", &t);
|
||||
|
||||
while (t--) {
|
||||
int n;
|
||||
// std::scanf("%d", &n);
|
||||
|
||||
n = std::rand() % 500000 + 1;
|
||||
|
||||
if (n == 1) {
|
||||
std::cout << "0" << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
int total = 1;
|
||||
int r = (int) std::floor(std::sqrt(n));
|
||||
|
||||
for (int n2 = 2; n2 < r + 1; n2++) {
|
||||
if (n % n2 == 0)
|
||||
total += n2 + (n/n2);
|
||||
// std::cout << n2 << std::endl;
|
||||
}
|
||||
|
||||
std::cout << total << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import math
|
||||
|
||||
def main():
|
||||
nums = []
|
||||
|
||||
for i in range(3):
|
||||
nums.append(int(input()))
|
||||
|
||||
print(math.gcd(*nums))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,69 @@
|
||||
def merge(l: list, start: int, end: int, end2: int) -> None:
|
||||
|
||||
n1 = end - start + 1
|
||||
n2 = end2 - end
|
||||
|
||||
left = [0] * (n1)
|
||||
right = [0] * (n2)
|
||||
|
||||
for i in range(n1):
|
||||
left[i] = l[start + i]
|
||||
|
||||
for i in range(n2):
|
||||
right[i] = l[end + 1 + i]
|
||||
|
||||
i = 0
|
||||
j = 0
|
||||
k = start
|
||||
|
||||
while i < n1 and j < n2:
|
||||
if left[i] <= right[j]:
|
||||
l[k] = left[i]
|
||||
i += 1
|
||||
else:
|
||||
l[k] = right[j]
|
||||
j += 1
|
||||
k += 1
|
||||
|
||||
while i < n1:
|
||||
l[k] = left[i]
|
||||
j += 1
|
||||
k += 1
|
||||
|
||||
while j < n2:
|
||||
l[k] = right[j]
|
||||
j += 1
|
||||
k += 1
|
||||
|
||||
def merge_sort(l: list, left: int, right: int) -> None:
|
||||
if left < right:
|
||||
|
||||
m = left + (right - 1) // 2
|
||||
|
||||
merge_sort(l, left, m)
|
||||
merge_sort(l, m + 1, right)
|
||||
merge(l, left, m, right)
|
||||
|
||||
|
||||
def main():
|
||||
lines: list[str] = []
|
||||
test_cases = int(input())
|
||||
while (test_cases != 0):
|
||||
n = int(input())
|
||||
|
||||
while (n != 0):
|
||||
|
||||
lines.append(input())
|
||||
|
||||
n -= 1
|
||||
|
||||
merge_sort(lines, 0, len(lines) - 1)
|
||||
for line in lines:
|
||||
print(line)
|
||||
|
||||
lines = []
|
||||
|
||||
test_cases -= 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,54 @@
|
||||
def partition(arr: list, low: int, high: int) -> int:
|
||||
|
||||
pivot = arr[high]
|
||||
|
||||
i = low - 1
|
||||
|
||||
for j in range(low, high):
|
||||
if arr[j] < pivot:
|
||||
i += 1
|
||||
swap(arr, i, j)
|
||||
|
||||
swap(arr, i + 1, high)
|
||||
return i + 1
|
||||
|
||||
def swap(arr: list, i: int, j: int) -> None:
|
||||
arr[i], arr[j] = arr[j], arr[i]
|
||||
|
||||
def quick_sort_h(arr: list, low: int, high: int) -> list:
|
||||
# copy = arr.copy()
|
||||
|
||||
if low < high:
|
||||
|
||||
pi = partition(arr, low, high)
|
||||
|
||||
quick_sort_h(arr, low, pi - 1)
|
||||
quick_sort_h(arr, pi + 1, high)
|
||||
|
||||
# return copy
|
||||
|
||||
# def quick_sort(arr: list) -> list:
|
||||
# return quick_sort_h(arr, 0, len(arr) - 1)
|
||||
|
||||
def main():
|
||||
lines: list[str] = []
|
||||
test_cases = int(input())
|
||||
while (test_cases != 0):
|
||||
n = int(input())
|
||||
|
||||
while (n != 0):
|
||||
|
||||
lines.append(input())
|
||||
|
||||
n -= 1
|
||||
|
||||
quick_sort_h(lines, 0, len(lines) - 1)
|
||||
for line in lines:
|
||||
print(line)
|
||||
|
||||
lines = []
|
||||
|
||||
test_cases -= 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,69 @@
|
||||
def merge(l: list, start: int, end: int, end2: int) -> None:
|
||||
|
||||
n1 = end - start + 1
|
||||
n2 = end2 - end
|
||||
|
||||
left = [0] * (n1)
|
||||
right = [0] * (n2)
|
||||
|
||||
for i in range(n1):
|
||||
left[i] = l[start + i]
|
||||
|
||||
for i in range(n2):
|
||||
right[i] = l[end + 1 + i]
|
||||
|
||||
i = 0
|
||||
j = 0
|
||||
k = start
|
||||
|
||||
while i < n1 and j < n2:
|
||||
if left[i] <= right[j]:
|
||||
l[k] = left[i]
|
||||
i += 1
|
||||
else:
|
||||
l[k] = right[j]
|
||||
j += 1
|
||||
k += 1
|
||||
|
||||
while i < n1:
|
||||
l[k] = left[i]
|
||||
j += 1
|
||||
k += 1
|
||||
|
||||
while j < n2:
|
||||
l[k] = right[j]
|
||||
j += 1
|
||||
k += 1
|
||||
|
||||
def merge_sort(l: list, left: int, right: int) -> None:
|
||||
if left < right:
|
||||
|
||||
m = left + (right - 1) // 2
|
||||
|
||||
merge_sort(l, left, m)
|
||||
merge_sort(l, m + 1, right)
|
||||
merge(l, left, m, right)
|
||||
|
||||
|
||||
def main():
|
||||
lines: list[str] = []
|
||||
test_cases = int(input())
|
||||
while (test_cases != 0):
|
||||
n = int(input())
|
||||
|
||||
while (n != 0):
|
||||
|
||||
lines.append(input())
|
||||
|
||||
n -= 1
|
||||
|
||||
merge_sort(lines, 0, len(lines) - 1)
|
||||
for line in lines:
|
||||
print(line)
|
||||
|
||||
lines = []
|
||||
|
||||
test_cases -= 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,79 @@
|
||||
# Python program for implementation of MergeSort
|
||||
|
||||
# Merges two subarrays of arr[].
|
||||
# First subarray is arr[l..m]
|
||||
# Second subarray is arr[m+1..r]
|
||||
|
||||
|
||||
def merge(arr, l, m, r):
|
||||
n1 = m - l + 1
|
||||
n2 = r - m
|
||||
|
||||
# create temp arrays
|
||||
L = [0] * (n1)
|
||||
R = [0] * (n2)
|
||||
|
||||
# Copy data to temp arrays L[] and R[]
|
||||
for i in range(0, n1):
|
||||
L[i] = arr[l + i]
|
||||
|
||||
for j in range(0, n2):
|
||||
R[j] = arr[m + 1 + j]
|
||||
|
||||
# Merge the temp arrays back into arr[l..r]
|
||||
i = 0 # Initial index of first subarray
|
||||
j = 0 # Initial index of second subarray
|
||||
k = l # Initial index of merged subarray
|
||||
|
||||
while i < n1 and j < n2:
|
||||
if L[i] <= R[j]:
|
||||
arr[k] = L[i]
|
||||
i += 1
|
||||
else:
|
||||
arr[k] = R[j]
|
||||
j += 1
|
||||
k += 1
|
||||
|
||||
# Copy the remaining elements of L[], if there
|
||||
# are any
|
||||
while i < n1:
|
||||
arr[k] = L[i]
|
||||
i += 1
|
||||
k += 1
|
||||
|
||||
# Copy the remaining elements of R[], if there
|
||||
# are any
|
||||
while j < n2:
|
||||
arr[k] = R[j]
|
||||
j += 1
|
||||
k += 1
|
||||
|
||||
# l is for left index and r is right index of the
|
||||
# sub-array of arr to be sorted
|
||||
|
||||
|
||||
def mergeSort(arr, l, r):
|
||||
if l < r:
|
||||
|
||||
# Same as (l+r)//2, but avoids overflow for
|
||||
# large l and h
|
||||
m = l+(r-l)//2
|
||||
|
||||
# Sort first and second halves
|
||||
mergeSort(arr, l, m)
|
||||
mergeSort(arr, m+1, r)
|
||||
merge(arr, l, m, r)
|
||||
|
||||
|
||||
# Driver code to test above
|
||||
arr = [12, 11, 13, 5, 6, 7]
|
||||
n = len(arr)
|
||||
print("Given array is")
|
||||
for i in range(n):
|
||||
print("%d" % arr[i],end=" ")
|
||||
|
||||
mergeSort(arr, 0, n-1)
|
||||
print("\n\nSorted array is")
|
||||
for i in range(n):
|
||||
print("%d" % arr[i],end=" ")
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
|
||||
#define MAX_BOXES 50000
|
||||
|
||||
bool check_candies(int boxes[], int mid, int student_count, int box_count) {
|
||||
for (int i = 0; i < box_count; ++i) {
|
||||
student_count -= boxes[i] / mid;
|
||||
if (student_count < 1)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// O(log n)
|
||||
int main() {
|
||||
int t;
|
||||
std::scanf("%d", &t);
|
||||
|
||||
int boxes[MAX_BOXES];
|
||||
|
||||
while (t--) {
|
||||
int n, k;
|
||||
|
||||
std::scanf("%d %d", &n, &k);
|
||||
|
||||
int largest = INT_MIN;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::scanf("%d", &boxes[i]);
|
||||
|
||||
if (boxes[i] > largest)
|
||||
largest = boxes[i];
|
||||
}
|
||||
|
||||
int low = 0;
|
||||
int high = largest;
|
||||
|
||||
while (low < high) {
|
||||
int mid = low + (high - low + 1) / 2;
|
||||
|
||||
if (check_candies(boxes, mid, k, n)) {
|
||||
low = mid;
|
||||
} else {
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::printf("%d\n", low);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
def main():
|
||||
test_cases = int(input())
|
||||
while (test_cases != 0):
|
||||
n, k = [int(n) for n in input().split()]
|
||||
|
||||
boxes: list[int] = [int(candies) for candies in input().split()]
|
||||
|
||||
boxes.sort(reverse=True)
|
||||
|
||||
# k = 2
|
||||
# [4, 3, 1]
|
||||
i = 0
|
||||
l = len(boxes)
|
||||
largest = boxes[i]
|
||||
while largest > 0:
|
||||
|
||||
correct = 0
|
||||
for candies in boxes:
|
||||
if (c := candies // largest) > 0:
|
||||
correct += c
|
||||
|
||||
if correct >= k:
|
||||
break
|
||||
|
||||
i += 1
|
||||
if i >= l:
|
||||
largest = 0
|
||||
break
|
||||
largest = boxes[i]
|
||||
|
||||
|
||||
print(largest)
|
||||
|
||||
|
||||
|
||||
test_cases -= 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,54 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
int test_cases;
|
||||
|
||||
std::scanf("%d", &test_cases);
|
||||
|
||||
char * test_case2 = (char *) malloc(10000000 * sizeof(char));
|
||||
char * token2 = (char *) malloc(10000000 * sizeof(char));
|
||||
|
||||
while (test_cases != 0) {
|
||||
|
||||
size_t pos = 0;
|
||||
int i = 0;
|
||||
int count = 0;
|
||||
std::vector<size_t> positions;
|
||||
|
||||
std::fscanf(stdin, "%s %s", test_case2, token2);
|
||||
|
||||
std::string test_case = test_case2;
|
||||
std::string token = token2;
|
||||
|
||||
while ((pos = test_case.find(token, i)) != std::string::npos) {
|
||||
positions.push_back(pos + 1);
|
||||
|
||||
i = pos + 1;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (positions.size() == 0) {
|
||||
std::printf("Not Found");
|
||||
} else {
|
||||
std::printf("%d\n", count);
|
||||
for (size_t pos : positions) {
|
||||
if (pos != positions.back()) {
|
||||
std::printf("%d ", pos);
|
||||
} else {
|
||||
std::printf("%d", pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (test_cases != 1)
|
||||
std::printf("\n");
|
||||
|
||||
test_cases--;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
def kmp_table(S: str, W: str) -> list[int]:
|
||||
T = [0] * len(S)
|
||||
|
||||
pos = 1
|
||||
cnd = 0
|
||||
|
||||
while pos < len(W):
|
||||
if W[pos] == W[cnd]:
|
||||
cnd += 1
|
||||
T[pos] = cnd
|
||||
pos += 1
|
||||
else:
|
||||
if cnd != 0:
|
||||
cnd = T[cnd - 1]
|
||||
else:
|
||||
T[pos] = 0
|
||||
pos += 1
|
||||
|
||||
return T
|
||||
|
||||
# Returns a list of positions (indexes)
|
||||
def kmp(S: str, W: str) -> list[int]:
|
||||
|
||||
if W not in S:
|
||||
return []
|
||||
|
||||
j = 0
|
||||
k = 0
|
||||
T = kmp_table(S, W)
|
||||
P = [] # Positions List
|
||||
|
||||
while j < len(S):
|
||||
if W[k] == S[j]:
|
||||
j += 1
|
||||
k += 1
|
||||
if k == len(W):
|
||||
P.append(j - k)
|
||||
k = T[k - 1]
|
||||
else:
|
||||
if k != 0:
|
||||
k = T[k - 1]
|
||||
else:
|
||||
j += 1
|
||||
|
||||
return P
|
||||
|
||||
def find_search(S: str, W: str) -> list[int]:
|
||||
pos = 0
|
||||
i = 0
|
||||
count = 0
|
||||
positions = []
|
||||
while (pos := S.find(W, i)) != -1:
|
||||
i = pos + 1
|
||||
positions.append(pos + 1)
|
||||
count += 1
|
||||
|
||||
return positions
|
||||
|
||||
def main():
|
||||
test_cases = int(input())
|
||||
|
||||
while (test_cases != 0):
|
||||
test_case, token = input().split()
|
||||
positions = kmp(test_case, token)
|
||||
|
||||
if len(positions) == 0:
|
||||
print("Not Found", end='')
|
||||
else:
|
||||
print(len(positions))
|
||||
for pos in positions:
|
||||
if pos != positions[-1]:
|
||||
print(f'{pos + 1} ', end='')
|
||||
else:
|
||||
print(f'{pos + 1}', end='')
|
||||
|
||||
if test_cases != 1:
|
||||
print('\n')
|
||||
|
||||
test_cases -= 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
BIN
Binary file not shown.
File renamed without changes.
@@ -1,5 +0,0 @@
|
||||
4
|
||||
your
|
||||
progress
|
||||
is
|
||||
noticeable
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <math.h>
|
||||
#include <cstring>
|
||||
|
||||
const int MAXS = 500000;
|
||||
const int MAXC = 26;
|
||||
|
||||
|
||||
int out[MAXS];
|
||||
int f[MAXS];
|
||||
int g[MAXS][MAXC];
|
||||
|
||||
int build_string_matcher(std::string arr[], int k) {
|
||||
std::memset(out, 0, sizeof out);
|
||||
std::memset(g, -1, sizeof g);
|
||||
|
||||
int states = 1;
|
||||
|
||||
for (int i = 0; i < k; ++i)
|
||||
{
|
||||
const std::string &word = arr[i];
|
||||
int currentState = 0;
|
||||
|
||||
for (int j = 0; j < word.size(); ++j)
|
||||
{
|
||||
int ch = word[j] - 'a';
|
||||
|
||||
|
||||
if (g[currentState][ch] == -1)
|
||||
g[currentState][ch] = states++;
|
||||
|
||||
currentState = g[currentState][ch];
|
||||
}
|
||||
|
||||
out[currentState] |= (1 << i);
|
||||
}
|
||||
|
||||
for (int ch = 0; ch < MAXC; ++ch)
|
||||
if (g[0][ch] == -1)
|
||||
g[0][ch] = 0;
|
||||
|
||||
std::memset(f, -1, sizeof f);
|
||||
|
||||
std::queue<int> q;
|
||||
|
||||
for (int ch = 0; ch < MAXC; ++ch)
|
||||
{
|
||||
if (g[0][ch] != 0)
|
||||
{
|
||||
f[g[0][ch]] = 0;
|
||||
q.push(g[0][ch]);
|
||||
}
|
||||
}
|
||||
|
||||
while (q.size())
|
||||
{
|
||||
int state = q.front();
|
||||
q.pop();
|
||||
|
||||
for (int ch = 0; ch <= MAXC; ++ch)
|
||||
{
|
||||
if (g[state][ch] != -1)
|
||||
{
|
||||
int failure = f[state];
|
||||
|
||||
while (g[failure][ch] == -1)
|
||||
failure = f[failure];
|
||||
|
||||
failure = g[failure][ch];
|
||||
f[g[state][ch]] = failure;
|
||||
|
||||
out[g[state][ch]] |= out[failure];
|
||||
|
||||
q.push(g[state][ch]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return states;
|
||||
}
|
||||
|
||||
int find_next_state(int currentState, char nextInput)
|
||||
{
|
||||
int answer = currentState;
|
||||
int ch = nextInput - 'a';
|
||||
|
||||
|
||||
while (g[answer][ch] == -1)
|
||||
answer = f[answer];
|
||||
|
||||
return g[answer][ch];
|
||||
}
|
||||
|
||||
void search_words(std::string queries[], int k, std::string text) {
|
||||
build_string_matcher(queries, k);
|
||||
|
||||
int current_state = 0;
|
||||
|
||||
int counts[k];
|
||||
|
||||
std::memset(counts, 0, sizeof counts);
|
||||
|
||||
for (int i = 0; i < text.size(); i++) {
|
||||
current_state = find_next_state(current_state, text[i]);
|
||||
|
||||
if (out[current_state] == 0)
|
||||
continue;
|
||||
|
||||
for (int j = 0; j < k; j++) {
|
||||
if (out[current_state] & (1 << j)) {
|
||||
counts[j]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < k; i++) {
|
||||
std::printf("%d\n", counts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int N, Q;
|
||||
std::string S, query;
|
||||
|
||||
std::scanf("%d %d", &N, &Q);
|
||||
|
||||
char c_s[N];
|
||||
std::fscanf(stdin, "%s", c_s);
|
||||
S = c_s;
|
||||
|
||||
std::string queries[Q];
|
||||
int i = 0;
|
||||
int q_copy = Q;
|
||||
char c_query[3000];
|
||||
while(q_copy != 0) {
|
||||
|
||||
std::fscanf(stdin, "%s", c_query);
|
||||
query = c_query;
|
||||
queries[i] = query;
|
||||
|
||||
i++;
|
||||
q_copy--;
|
||||
}
|
||||
|
||||
search_words(queries, Q, S);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#define LIMIT 20000000
|
||||
|
||||
int main() {
|
||||
|
||||
int c;
|
||||
|
||||
std::cin >> c;
|
||||
|
||||
std::vector<int> nums;
|
||||
std::vector<int> twins;
|
||||
|
||||
int max = 0;
|
||||
while (c != 0) {
|
||||
int a;
|
||||
|
||||
std::cin >> a;
|
||||
if (a > max) {
|
||||
max = a;
|
||||
}
|
||||
nums.push_back(a);
|
||||
|
||||
c--;
|
||||
}
|
||||
|
||||
std::vector<bool> sieve(LIMIT + 1, true);
|
||||
sieve[0] = sieve[1] = false;
|
||||
|
||||
for (int p = 2; p * p <= LIMIT; ++p) {
|
||||
if (sieve[p]) {
|
||||
for (int i = p * p; i <= LIMIT; i += p) {
|
||||
sieve[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int last = 0;
|
||||
int k = 0;
|
||||
for (int p = 2; p <= LIMIT; ++p) {
|
||||
if (sieve[p]) {
|
||||
k++;
|
||||
|
||||
if ((p - last) == 2) {
|
||||
for (int n : nums) {
|
||||
if (n == k) {
|
||||
printf("%d %d", p, last);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
last = p;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
# n is the largest number prime pair we need
|
||||
def prime_pairs(n):
|
||||
if n <= 0:
|
||||
return []
|
||||
|
||||
limit = 1000
|
||||
primes = []
|
||||
sieve = [True] * (limit + 1)
|
||||
sieve[0] = sieve[1] = False
|
||||
prime_pairs = []
|
||||
|
||||
while True:
|
||||
print(len(sieve))
|
||||
for p in range(2, int(limit**0.5) + 1):
|
||||
if sieve[p]:
|
||||
for i in range(p * p, limit + 1, p):
|
||||
sieve[i] = False
|
||||
|
||||
for p in range(2, limit + 1):
|
||||
if sieve[p]:
|
||||
primes.append(p)
|
||||
if len(primes) > 1:
|
||||
if primes[-1] - primes[-2] == 2 and (primes[-2], primes[-1]) not in prime_pairs:
|
||||
prime_pairs.append((primes[-2], primes[-1]))
|
||||
|
||||
if len(prime_pairs) >= n:
|
||||
break
|
||||
|
||||
limit *= 2
|
||||
sieve.extend([True] * (limit - len(sieve) + 1))
|
||||
|
||||
print(prime_pairs)
|
||||
|
||||
return prime_pairs
|
||||
|
||||
def main():
|
||||
n = int(input())
|
||||
|
||||
nums = []
|
||||
|
||||
for i in range(n):
|
||||
nums.append(int(input()))
|
||||
|
||||
primes_list = prime_pairs(max(nums))
|
||||
|
||||
for num in nums:
|
||||
print(f'{primes_list[num - 1][0]} {primes_list[num - 1][1]}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
std::vector<bool> sieve(int limit) {
|
||||
std::vector<bool> is_prime(limit + 1, true);
|
||||
is_prime[0] = is_prime[1] = false;
|
||||
|
||||
for (int i = 2; i * i <= limit; i++) {
|
||||
if (is_prime[i]) {
|
||||
for (int j = i * i; j <= limit; j += i) {
|
||||
is_prime[j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return is_prime;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int T;
|
||||
std::cin >> T;
|
||||
|
||||
int limit = 20000000;
|
||||
|
||||
std::vector<bool> is_prime = sieve(limit);
|
||||
|
||||
std::vector<std::pair<int, int>> twin_primes;
|
||||
|
||||
for (int i = 2; i < limit - 2; ++i) {
|
||||
if (is_prime[i] && is_prime[i + 2]) {
|
||||
twin_primes.push_back({i, i + 2});
|
||||
}
|
||||
}
|
||||
|
||||
while (T--) {
|
||||
int N;
|
||||
std::cin >> N;
|
||||
printf("%d %d\n", twin_primes[N - 1].first, twin_primes[N - 1].second);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user