题意 你在一个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;
}| input | output | 
|---|---|
| 7 5 | 11 | 
| 2 3 | 3 | 
URAL 1762 Search for a Hiding-Place(数学·模拟)
原文地址:http://blog.csdn.net/acvay/article/details/44204637