-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram 08
More file actions
40 lines (30 loc) · 910 Bytes
/
Copy pathProgram 08
File metadata and controls
40 lines (30 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from collections import defaultdict
def prime_factors(num):
factors = defaultdict(int)
while num % 2 == 0:
factors[2] += 1
num //= 2
for i in range(3, int(num**0.5) + 1, 2):
while num % i == 0:
factors[i] += 1
num //= i
if num > 2:
factors[num] += 1
return factors
def calculate_prime_index_sum(arr, num):
if not arr:
return -1
factors = prime_factors(num)
total_sum = 0
valid_prime_found = False
for prime, power in factors.items():
if prime < len(arr):
total_sum += power * arr[prime]
valid_prime_found = True
return total_sum if valid_prime_found else 0
if _name_ == "_main_":
n = int(input())
arr = list(map(int, input().split()))
num = int(input())
result = calculate_prime_index_sum(arr, num)
print(result)