POJ 2605 数学
2016-05-02 15:26
387 查看
http://poj.org/problem?id=2605
Simple game on a grid
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
Sample Output
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;
Simple game on a grid
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 931 | Accepted: 482 |
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; }
相关文章推荐
- 图形绘制管线
- Spark-Avro学习8之Java Avro使用(不生成code方式)
- POJ 3264 RMQ—ST
- Android Studio 下的NDK环境搭建与JNI调用
- POJ 1555 Polynomial Showdown(字符串模拟)
- 51node1091 线段的最长交集(贪心)
- 性能测试定义
- hdu 1025 Constructing Roads In JGShining's Kingdom
- Java中final关键字的用法
- Is the server running on host “localhost” (::1) and accepting TCP/IP connections on port 5432?
- 解决使用pip安装lxml包报错问题
- JS学习6(函数表达式)
- 用HTML 5实现爱心小鱼的游戏
- 定量分析与print跟踪在代码分析中的运用
- 开源新闻速递(160502):Arch Linux 2016.05.01 发布
- 48-Merge Sorted Array
- 学生信息管理系统总结整理
- redis安装
- spec cpu2006 working set size
- 也许你的种子永远不会开花,因为他是一棵参天大树