Speedup with threads

More threads do not make every program faster. Amdahl's law says how much: if a fraction p of the work can run in parallel on n threads, the speedup is 1 / ((1 − p) + p / n).

What to build

For each query, compute the speedup that Amdahl's law gives.

Requirements

  • Each query gives the parallel part as a whole percent (so p is the percent divided by 100) and the number of threads n.
  • Print the speedup with 2 decimal places.

Input

The first line has q (1 ≤ q ≤ 20). Each of the next q lines has a percent (0 to 100) and a thread count n (1 to 64).

Output

One line per query: the speedup with 2 decimal places.

Constraints

All values are whole numbers as given. Use double for the arithmetic.

Example

Input:

3
50 2
90 4
100 8

Output:

1.33
3.08
8.00

Why it works

The part that is not parallel, 1 − p, is always paid in full, so it sets a ceiling: with 90% parallel work the speedup can never reach 10, however many threads you add. Splitting the work into more pieces only shrinks the parallel part of the time.

Examples

Example 1 · Input
3
50 2
90 4
100 8
Expected output
1.33
3.08
8.00
Example 2 · Input
2
0 16
75 1
Expected output
1.00
1.00

Hints

Hint 1

Work with doubles: p = percent / 100.0, then speedup = 1.0 / ((1.0 - p) + p / threads).

Hint 2

Print with printf("%.2f\n", value).

Hint 3

With 1 thread the speedup is exactly 1.00, whatever the percentage.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

int main(void) {
    int q, percent, threads;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &percent, &threads);
        double p = percent / 100.0;
        printf("%.2f\n", 1.0 / ((1.0 - p) + p / threads));
    }
    return 0;
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Threads and race conditions →