Làm sao để có câu trả lời hay nhất?
23/03/2025
23/03/2025
def sieve(limit):
prime = [True] * (limit + 1)
prime[0] = prime[1] = False
for i in range(2, int(limit ** 0.5) + 1):
if prime[i]:
for j in range(i * i, limit + 1, i):
prime[j] = False
return prime
# Đọc file input
with open('BAI2.INP', 'r') as f:
n = int(f.readline())
arr = list(map(int, f.readline().split()))
# Sàng nguyên tố
is_prime = sieve(10**7)
# Tập hợp chứa các số nguyên tố duy nhất
prime_set = set()
for num in arr:
if is_prime[num]:
prime_set.add(num)
# Ghi file output
with open('BAI2.OUT', 'w') as f:
f.write(str(len(prime_set)))
Input:
5
1 3 25 19 8
Các số nguyên tố là: 3, 19
=> Có 2 số nguyên tố khác nhau.
Output: 2
# Đọc file input
with open('BAI4.INP', 'r') as f:
N, X = map(int, f.readline().split())
A = list(map(int, f.readline().split()))
maxA = max(A)
count = 0
possible = True
for ai in A:
if (maxA - ai) % X != 0:
possible = False
break
count += (maxA - ai) // X
# Ghi file output
with open('BAI4.OUT', 'w') as f:
if possible:
f.write(str(count))
else:
f.write('-1')
Input:
5 2
5 1 3 3 5
- maxA = 5
- Các chênh lệch: 0, 4, 2, 2, 0
- Kiểm tra chia hết X=2 → ok
- Tổng số bước: 0 + 2 + 1 + 1 + 0 = 4
Output: 4
Input:
2 100
1 10
- maxA = 10
- (10 - 1) = 9, 9 % 100 != 0 → không thể biến đổi
Output: -1
Nếu bạn muốn hỏi bài tập
Các câu hỏi của bạn luôn được giải đáp dưới 10 phút
CÂU HỎI LIÊN QUAN
07/04/2025
Top thành viên trả lời