Glowing Bulbs
Practice
4.2 (36 votes)
Algorithms
Approved
Binary search
Hard
Open
Sorting
Problem
47% Success 4075 Attempts 50 Points 6s Time Limit 256MB Memory 1024 KB Max Code

There is an infinite series of bulbs indexed from 1. And there are 40 switches indexed from 1 to 40. Switch with index x is connected to the bulbs with indexes that are multiple of x. i.e switch 2 is connected to bulb 4, 6, 8 ....

You can easily observe that some bulbs are connected to multiple switches and some are not connected to any switch.

Chotu is playing with these switches, he wants to know the Kth glowing bulb from the start if certain switches are in ON state. If any of the switch is ON, connected to any bulb then bulb starts glowing. But chotu has special fond of prime numbers so he only puts prime indexed switches ON.

INPUT-

First line contains number of test cases (T). Each test case contains two lines- First line contains a string S of length 40 containing 0s and 1s that represents the state of bulbs. 1 is ON , 0 is OFF. Second line contains one integer k. Atleast One switch is in ON condition.

OUTPUT-

Output one integer per test case representing kth glowing bulb.

CONSTRAINTS-

1 <= T <= 500

S contains only 0s and 1s. 1s are only at prime positions.

1 <= k <= 10^15

1 is not prime

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
11 votes
Tags:
AlgorithmsBinary SearchFenwick TreeHardHiringSearching
Points:50
7 votes
Tags:
Binary SearchAlgorithmsC++
Points:50
9 votes
Tags:
Binary SearchC++Algorithms