( Problem B ) Pikachu vs Team Meowstic and Helping Hand

3.6

22 votes
Basic Programming, Bit Manipulation, Bit manipulation, Easy
Problem

Pikachu loves battling with other Pokemon. This time he has a team of N Meowstic to fight, ith of which has strength ai. He wants to fight with all of them K times. Team Meowstic came to know about this and now they have devised a strategy to battle against the mighty Pikachu.

All the N Meowstic stand in a straight line numbered from 1 to N. Before every round of battle, they simultaneously use a move, called Helping Hand. It changes the attacking power of Team Meowstic as follows:

  • The attacking power of first Meowstic remains a1.
  • The attacking power of remaining Meowstic changes as ai=ai|ai1 where 2iN and A|B represents the bitwise OR of A and B 

For example, if the current attacking powers are [2,1,4,6,3], after using the Helping Hand, the powers change to [2,1|2,4|1,6|4,3|6], or [2,3,5,6,7].

Help Pikachu by finding the attacking powers of all Meowstic when he fights each of them for the last time, that is, for the Kth round.

Note that, the influence of Helping Hand remains forever, and attacking powers DO NOT revert back after any round.

Constraints:

  • 1N105
  • 0K109
  • 0ai<230

Input format:

  • First line contains two space separated integers, N and K
  • Second line contains  N space separated integers, the ith of which is ai 

Output format:

  • Output a single line containing N integers, the ith of which is ai after the last round 
Sample Input
5 3
1 2 3 4 5
Sample Output
1 3 3 7 7
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Battle 1:

 a1=1,a2=1|2=3,a3=2|3=3,a4=3|4=7,a5=4|5=5

Battle 2:

a1=1,a2=1|3=3,a3=3|3=3,a4=3|7=7,a5=7|5=7

Battle 3:

a1=1,a2=1|3=3,a3=3|3=3,a4=3|7=7,a5=7|7=7 

Contributers:
Editor Image

?