HDU5100 Chessboard

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;
}
發佈了95 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章