We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Mathematics
  3. Algebra
  4. Sherlock and Queries

Sherlock and Queries

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Watson gives Sherlock an array of elements and two arrays and , of elements each. Then he asks Sherlock to perform the following program:

for i = 1 to M do
    for j = 1 to N do
        if j % B[i] == 0 then
            A[j] = A[j] * C[i]
        endif
    end do
end do

This code needs to be optimized. Can you help Sherlock and tell him the resulting array ? You should print all the array elements modulo .

Input Format
The first line contains two integer, and . The next line contains integers, the elements of array . The last two lines contain integers each, the elements of array and , respectively.

Output Format
Print space-separated integers, the elements of array after performing the program modulo .

Constraints


Sample Input

4 3
1 2 3 4
1 2 3
13 29 71

Sample Output

13 754 2769 1508

Author

darkshadows

Difficulty

Advanced

Max Score

30

Submitted By

5297

Need Help?


View discussions
View editorial
View top submissions
RESOURCES

  • Div Mod

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits

Choose a translation


  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature