POJ 2605 数学


http://poj.org/problem?id=2605


Simple game on a grid
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 931   Accepted: 482

Description

There is an infinite grid and an m*n rectangle of stones on it (1 <= m,n <= 1000). The stones are located in the knots of the grid. 

A following game for a single player is being played. One stone can jump over another along a vertical or a horizontal line. A stone which had been overjumped is taken away. The purpose of the game is to minimize number of stones on a grid. 

Given a pair of numbers m and n separated with one space in an input file you are to write a program which should determine a minimal number of the stones left on the grid.

Input

Numbers m and n separated by space.

Output

The minimal number of the stones left on the grid.

Sample Input

3 4

Sample Output

2

Source

Ural State University collegiate programming contest 2000

题目大意:

给你一个n*m的矩阵,尽可能多的消去里面所有的石头,最后剩下几颗


其实就是找规律,表示和同学一起也找了半天

①如果是3*n的话,那么最后肯定能变成3*1的类型的,那么就是剩下两个。

②如果是2*n(n不能被3整除)或者是1*n(同2*n)的话,那么就是(n+1)/2。

③如果是4*n,5*n(n不能被3整除,且大于2)的话,那么就也只是剩下一个。(具体的操作自己去划一下才有感觉)

于是我们通过归纳得到,

当n大于3的时候,如果n和m在都不能被3整除,那么就是1。

当n=3的时候就是2

当n=1和2的时候就是(n+1)/2;

#include
#include
#include

using namespace std;


int main(){
    int n, m;
    while (scanf("%d%d", &n, &m) == 2){
        if (n > m) swap(n, m);
        int res = 0;
        if (n == 1){
            res = (m + 1) / 2;
        }
        else if (n % 3 == 0 || m % 3 == 0){
            res = 2;
        }
        else res = 1;
        printf("%d\n", res);
    }
    return 0;
}


你可能感兴趣的:(数学)