Fill The Grid Codechef Solution | GRIDBL
You have a grid with
- Each cell of the grid is covered by exactly one tile; and
- The number of
1×1 tiles used is minimized.
Find the minimum number of
Input Format
- The first line of input will contain a single integer
T , denoting the number of test cases. - Each test case consists of a single line containing two space-separated integers
N,M
Output Format
For each test case, print on a new line the minimum number of
Constraints
1 ≤ T ≤ 10 ^ 4 1 ≤ N, M ≤ 10 ^ 4
Sample Input 1
4
1 1
4 5
6 8
3 2
Sample Output 1
1
4
0
2
Explanation
Test case
Test case
Test case
Test case
#include
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int c, d;
cin >> c >> d;
if ((c % 2 == 0) && (d % 2 == 0))
{
cout << 0;
}
else if ((c % 2 != 0) && (d % 2 == 0))
{
cout << d;
}
else if ((c % 2 == 0) && (d % 2 != 0))
{
cout << c;
}
else
{
cout << c + d - 1;
}
cout << endl;
}
return 0;
}
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int m=sc.nextInt();
if(n%2==0){
if( m%2==0){
System.out.println(0);
}
else{
System.out.println(n);
}
}
else if(m%2==0){
System.out.println(m);
}
else{
int res=n*m - (n-1)*(m-1);
System.out.println(res);
}
}
0 Comments