B3925 [GSEP202312 三级] 小猫分鱼 tj

· · 题解

与 std 对拍了 1000 组没挂,应该没问题。

思路

枚举第 n 条小猫拿鱼的条数,然后逆推枚举答案,如果发现逆推时答案除不尽 n-1,那么跳出进入到下一次枚举,如果逆推完毕后还没有跳出,那么就直接输出答案。

Code

#include <bits/stdc++.h>
#define endl '\n'
#define ll long long

using namespace std;
const int E = 1e6 + 5;
ll n, i;
int main()
{
    cin >> n >> i;
    ll ans;
    auto check = [&](ll x) -> bool//lambda表达式
    {
        ans = x * n + i;//第 n 条鱼
        for (int j = 1; j < n; j++)
        {
            if (ans % (n - 1) != 0)//除不尽
                return false;
            ans = ans / (n - 1) * n + i;//反之继续逆推
        }
        return true;
    };
    ll j = 1;
    while (1)
    {
        if (check(j))
            break;
        j++;
    }
    cout << ans;
    return 0;
}