练习cf1418A. Buying Torches

题目如下
A. Buying Torches
time limit per test1 second
memory limit per test256 megabytes
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft 𝑘 torches. One torch can be crafted using one stick and one coal.

Hopefully, you've met a very handsome wandering trader who has two trade offers:

exchange 1 stick for 𝑥 sticks (you lose 1 stick and gain 𝑥 sticks).
exchange 𝑦 sticks for 1 coal (you lose 𝑦 sticks and gain 1 coal).
During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order.

Your task is to find the minimum number of trades you need to craft at least 𝑘 torches. The answer always exists under the given constraints.

You have to answer 𝑡 independent test cases.

Input
The first line of the input contains one integer 𝑡 (1≤𝑡≤2⋅104) — the number of test cases. Then 𝑡 test cases follow.

The only line of the test case contains three integers 𝑥, 𝑦 and 𝑘 (2≤𝑥≤109; 1≤𝑦,𝑘≤109) — the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively.

Output
For each test case, print the answer: the minimum number of trades you need to craft at least 𝑘 torches. The answer always exists under the given constraints.

题目大意
初始有1根木棍,可以用一根换取x根木棍,用y根木棍换取一个煤炭,每次操作可以选择一种交换一次,每个火把都由一根木棍和一个煤炭构成,现在要造出k根火把,至少需要多少次操作。

题目分析
鉴于题目都数据范围,用模拟次数加一可能会超时。
所以,每次只能进行对一根操作,那么每次操作完木棍总数都加x - 1,计算出制造k个火把需要的木棍总数,再除以每次增加的木棍数,再加上换煤炭需要的操作数即可。

点击查看代码
#include <iostream>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        long long x, y, k;
        cin >> x >> y >> k;
        long long target = k * ( y + 1 ) -  1;
        long long ops = target  / (x - 1) + (target % (x - 1) != 0);
        long long sum = ops + k;
        cout << sum << endl;
    }
    return 0;
}

posted @ 2025-07-23 20:19  sirro1uta  阅读(4)  评论(0)    收藏  举报