Category: Simple Math
Problem Analysis:
Observation: This problem can be solved by simple iteration or by using mathematical observations. As the problem category is Simple Math, let’s solve it with math.
Let’s forget about the term “Buy 2 Get 1”. How many pizzas actually you can buy?
If for buying pizzas, you’re getting additional pizza for free, then for buying pizzas, how many additional pizzas will you get?
Solution Idea:
If the pizza price is , and the dollar you have is . Then the total number of pizzas you can buy is,
For example, , , then .
Now you have the . So, the additional pizzas you will get will be,
[ As, ]
So, total number of pizzas will be
Special Thanks: Md. Albin Hossain
Source Code:
#include <stdio.h>
int main(){
int test_case;
scanf("%d", &test_case);
while(test_case--){
int m, x;
scanf("%d %d", &m, &x);
int pizza_count = (m / x) + (m / (2 * x));
printf("%d\n", pizza_count);
}
return 0;
}