Chessboard
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 799 Accepted Submission(s): 335
Problem Description
Consider the problem of tiling an n×n chessboard by polyomino pieces that are k×1 in size; Every one of the k pieces of each polyomino tile must align exactly with one of the chessboard squares. Your task is to figure out the maximum number of chessboard squares tiled.
Input
There are multiple test cases in the input file.
First line contain the number of cases T (T≤10000).
In the next T lines contain T cases , Each case has two integers n and k. (1≤n,k≤100)
Output
Print the maximum number of chessboard squares tiled.
Sample Input
2
6 3
5 3
Sample Output
36
24
不一定是按照
这样的填充再是剩余最小在r=n%k r>k/2的时候将不适用
需要把其余的填满再剩余一个(r+k)*(r+k)的区域然后用风车型填充类似于这样
对于样例 5 3
这样并不最优可以看到r=2,k=3。r>k/2;
这时我们这样填充
这样就是最优的方案没有覆盖的区域变为(k-r)*(k-r)
不费话了下面是代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
int n;
int k;
int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&k);
if(k>n)
printf("0\n");
else{
int r = n%k;
if(r<=k/2)//k为奇数的时候向下取整这是r还是小于k/2所以要加上等于
printf("%d\n",n*n-r*r);
else{
printf("%d\n",n*n-(k-r)*(k-r));
}
}
}
return 0;
}