P4143PyramidSequences

数学

等价于在一个 \(n\times m\) 的矩形中做弹球,问经过的整点个数

\(t=gcd(n,m)\) ,将 \(n,m\) 分别除掉 \(t\) ,得到 \(n',m'\)

此时会有 \(n'm'\) 条线段,每条线段经过 \(t\) 个整点,另外还有 \(\lceil \frac{(n'+1)(m'+1)}{2} \rceil\) 个交点

所以最终答案为

\[\lceil \frac{(n'+1)(m'+1)}{2} \rceil+(t-1)n'm' \]

const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

int n, m;

int gcd(int x, int y)
{
    if (!x)
        return y;
    return gcd(y % x, x);
}

void solve()
{
    cin >> n >> m;
    n--;
    m--;
    int t = gcd(n, m);
    n /= t;
    m /= t;
    cout << ((n + 1) * (m + 1) + 1) / 2 + (t - 1) * n * m << endl;
}

#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif

signed main()
{
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int testcase = 1;
    // cin >> testcase;
    while (testcase--)
        solve();
#ifndef ONLINE_JUDGE
    cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
    cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
    return 0;
}
posted @ 2024-04-08 15:17  xiaruize  阅读(10)  评论(0)    收藏  举报