An inode reaches the blocks of a file through direct pointers and through single, double and triple indirect blocks. These limits set the largest file the file system can hold.
What to build
Compute the largest file size an inode can address for the given block and pointer sizes.
Requirements
- A block of
Bbytes holdsk = B / Ppointers ofPbytes. The inode hasDdirect pointers, one single indirect, one double indirect and one triple indirect pointer. - The largest file has
D + k + k² + k³data blocks. Print its size in bytes: that number of blocks timesB.
Input
The first line has q (1 ≤ q ≤ 10). Each of the next q lines has B, P and D (P divides B; 4 ≤ P ≤ 8, 64 ≤ B ≤ 16384, 0 ≤ D ≤ 15).
Output
One line per query: the largest file size in bytes.
Constraints
Every answer fits in a long long.
Example
Input:
2
4096 4 12
1024 4 12
Output:
4402345721856
17247252480
Why it works
Each level multiplies the reach by k, so the triple indirect pointer dominates: with 4096-byte blocks and 4-byte pointers, k is 1024 and k³ is about a billion blocks. Small files, which are the most common, are served by the direct pointers alone.
Examples
2 4096 4 12 1024 4 12
4402345721856 17247252480
1 64 8 0
37376
Hints
Hint 1
One indirect block holds k = block size / pointer size pointers.
Hint 2
The number of data blocks is direct + k + k*k + k*k*k. Multiply by the block size.
Hint 3
Use long long for everything: the result is far beyond 32 bits.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int q, b, p, d;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%d %d %d", &b, &p, &d);
long long k = b / p;
long long blocks = d + k + k * k + k * k * k;
printf("%lld\n", blocks * b);
}
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step