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;
}