Search for a Hiding-Place (URAL 1762)

7 篇文章 0 订阅

1762. Search for a Hiding-Place

Time limit: 0.5 second
Memory limit: 64 MB
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


题意:n*m的空白方格,从四个角落中的某一个进入,斜着45度角移动,所经过的方块涂成黑色,碰到墙壁转90度接着移动直到走到某一个角落出去,途中若遇到前面已经涂黑的方格要再次把它涂白。问最后出去后一共有多少方格是黑色的。

思路:首先n和m自减,再求出n和m自减后的最小公倍数a(当走出去的时候走的总步数=a+1),接下来就是求有多少方格重复走了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int main()
{
    __int64 n,m;
    while (~scanf("%I64d%I64d",&n,&m))
    {
        if (n<m) swap(n,m);
        n--,m--;
        if (n%m==0){
            pf("%I64d\n",n+1);
            continue;
        }
        __int64 a=n/__gcd(n,m)*m;
        __int64 cnt=0,l=n;  //cnt记录重复走的方格数
        while (l<a)
        {
            cnt+=l/m;
            l+=n;
        }
        pf("%I64d\n",a-2*cnt+1);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值