bestcoder#17 1001&&HDU 5100 Chessboard(數學)

Chessboard

                                                                 點擊打開題目鏈接
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 85    Accepted Submission(s): 50


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 (). 
In the next T lines contain T cases , Each case has two integers n and k. ()
 

Output
Print the maximum number of chessboard squares tiled.
 

Sample Input
2 6 3 5 3
 

Sample Output
36 24
 

Source
 


先來吐槽一下題目,我去,就兩句話的英語,讀了好長時間纔讀懂,這次1001栽了不少人;
題目很簡單,給一個n*n的正方形棋盤,由kX1的小塊組成,問用這個小的(k*1)的小塊來覆蓋棋盤,最多能佔多少格(每格是一個1X1的小格);
有個結論:當n%k!=0時,最多爲:n*n-min(a,b);其中a=(n%k)*(n%k),b=(k-n%k)*(k-n%k);
當n%k==0時,顯然是n*n;
至於證明,當時只是感性的認爲這個結論是正確的,按照貪心的思想來放的,發現最後又兩種情況;
網上有一則證明:附個網址:http://www.matrix67.com/blog/archives/5900
                          http://www.brand.site.co.il/riddles/201403a.html
代碼:
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<stdlib.h>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<set>
#define inf 0x3f3f3f3f
#define eps 1e-5
#define max(a,b) a>b?a:b
//#define min(a,b) a<b?a:b
using namespace std;
int main()
{
    int t,n,k,ans;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d%d",&n,&k);
            if(k>n)
            {
                printf("%d\n",0);
            }
            else
            {
                if(n%k==0)
                    ans=n*n;
                else
                {
                    int a=(n%k)*(n%k);
                    int b=(k-n%k)*(k-n%k);
                    ans=(n*n)-min(a,b);
                }
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章