CF2009C The Legend of Freya the Frog 题解

· · 题解

原题传送门

分析

考虑 xy 两个方向,发现在 x 方向上所需的跳跃次数为 \lceil \frac{x}{k} \rceil。那么同样,在 y 方向上所需的跳跃次数为 \lceil \frac{y}{k} \rceil。试着组合一下,便发现两种情况。

时间复杂度:O(t)

Code

/*
Codeforces CF2009C The Legend of Freya the Frog  
*/
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define per(i,a,b) for(int i = a;i >= b;i--)
#define pb push_back
#define PII pair<int,int>
using namespace std;
typedef long long ll;
int t;
ll x,y,k;
inline void solve()
{
    cin>>x>>y>>k;
    ll pos = (x + k - 1) / k;//向上取整
    ll res = (y + k - 1) / k;
    if(pos <= res)
    {
        cout<<res * 2<<"\n";
    }
    else
    {
        cout<<pos * 2 - 1<<"\n";
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

AC submissions