Counting primes

3.3

26 votes
Hashmap, Data Structures, Arrays, Prime Factorization, Basic Math, Math
Problem

N people are standing in a row. Each of them has been assigned a rating of x and a range value y. A person with a range value of y has access to ratings of the first y person standing in front of him.

For each person, determine the count of unique prime factors of his or her rating x that divides any of the ratings of the person standing in his range.

Note: For each person, their range value is always less than or equal to the number of people standing in front of him.

Input format

  • The first line contains an integer N denoting the number of people in a row.
  • The second line consists of N space-separated integers denoting the ratings of people in the row.
  • The third line consists of N space-separated integers denoting the range values of people in a row.

Output format

For each person, print the count of unique prime factors of his or her rating x that divides any of the ratings of the person standing in his or her range.

Constraints

1N1001x1000y<i1iN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the person at position i = 1, the range value is 0 so the answer is 0.

For the person at position i = 2, the range value is 1. The prime factor 3 does not divide 7 so the answer is 0.

For the person at position i = 3, the range value is 1. The prime factors of 6 are {2,3}, 3 divides 3 ,but 2 does not divide 3 so the answer is 1.

For the person at position i = 4, the range value is 1. The prime factor 3 divides 6 so the answer is 1.

For the person at position i = 5, the range value is 4. The prime factor 7 divides 7 so the answer is 1.

Editor Image

?