URAL 1762 Search for a Hiding-Place(数学·模拟)

题意  你在一个n*m个白色正方形格子组成的矩形的某个顶点格子  你沿着45度角的方向走  到了边界就改变方向90度  每次经过一个格子都改变他原来的颜(白或灰)  求你走到另一个顶点格子时矩形中有多少格子是灰色的

这题可以用公式也可以直接模拟  模拟就是一直向右移n-1位 每次超过边界就可以把边界向右移m-1位  用cnt记录超过边界的次数 那么每次都会经过cnt个已经走过的格子 (每个格子最多经过2次)  答案也就减去2*cnt    直到刚好到达边界 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n, m, cur, cnt, ans, k;
    while(~scanf("%lld%lld", &n, &m))
    {
        if (n > m) swap(n, m);
        cur = ans = 1, cnt = 0, k = m;
        while(cur != k)
        {
            cur += n - 1, ans += n - 1 - 2 * cnt;
            if(cur > k) k += m - 1, ++cnt;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Search for a Hiding-Place


Problem illustration
Scooby-Doo is fond of adventures. This time he wanted to find a hiding-place in a vampire castle. After a long search, Scooby ended up in a huge rectangular hall with four entrances, one in each corner, through one of which he had entered. The floor was paved with white square tiles. Scooby thought that the hiding-place was under one of these tiles. He started searching for it by turning the tiles over, the grey side up. He began his search from a corner moving at an angle of 45° to the walls. Each time he came to a wall, he made a 90° turn. If he stepped on a grey tile, he turned it back the white side up. The search went on until Scooby reached an entrance at one of the corners. Then, not having found the hiding-place, the tired dog sighed and went out to have a snack.
Given the dimensions of the hall, calculate the total number of tiles that were turned the grey side up at the end of the search.

Input

The only input line contains integers  n and  m separated with a space. They are the length and width of the hall measured in tiles  (2 ≤  nm ≤ 1 000 000) .

Output

In the only line output the number of grey tiles in the hall after Scooby-Doo's search.

Samples

input output
7 5
11
2 3
3


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值