David was given a red checkered rectangle of size n×m. But he doesn't like it. So David cuts the original or any other rectangle piece obtained during the cutting into two new pieces along the grid lines. He can do this operation as many times as he wants.
As a result, he will get a set of rectangles. Rectangles 1×1 are forbidden.
David also knows how to paint the cells blue. He wants each rectangle from the resulting set of pieces to be colored such that any pair of adjacent cells by side (from the same piece) have different colors.
What is the minimum number of cells David will have to paint?
Input
The first line contains a single integer t (1≤t≤103) — the number of test cases. The next lines contain descriptions of test cases.
The only line of each test case contains two integers n, m (1≤n,m≤3⋅104, n⋅m≥2).
Output
For each test case print a single integer — the minimum number of cells David will have to paint blue.
Example
input
Copy
4 1 3 2 2 2 5 3 5
output
Copy
1 2 4 5
Note
The following pictures show how the initial rectangle can be split and cells colored blue.
In the first test case:
In the second test case:
In the third test case:
In the fourth test case:
思路:这是一道模拟题,这题模拟出图形的切割过程便就有答案了。先想如何情况下能让蓝色最少呢?就是让蓝色夹在中间,然后将三个为一块来切割。题上说有不能切成1*1的,(如果能被3整除的边直接将它切开,直接计算,ps:能整除的就不要管它是长短边了),可以先将长的边计算:能分成三个为一块的计算答案,将其去掉(就是不在管它)然后在对之后的操作,因为除余3,那么现在也只有余1和2的情况了:
当余2时:如果短边大于3,那么将短边除3(向下取整),记录在答案中,去掉。那么短边只有余1和2的了,余1时那么就剩 这种情况了(蓝的也可在上),余2时:也要加上两个蓝的
当余1时:同余2一样分析,但要记住会切出1*1的,另一种方法是除2向下取整即可。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
if(n>m) swap(n,m);
int ans=0;
if(n%3==0)
{
cout<<(n/3)*m<<endl;
continue;
}
else if(m%3==0)
{
cout<<(m/3)*n<<endl;
continue;
}
else
{
ans+=m/3*n;
m%=3;
if(m==2)
{
ans+=n/3*m;
n%=3;
if(n==1)
{
ans++;
}
else
{
ans+=2;
}
}
else
{
ans+=n/3;
n%=3;
if(n) ans++;//这里余1时相当于除2,不切了
}
}
cout<<ans<<endl;
}
return 0;
}