题目描述
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft kk 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 11 stick for xx sticks (you lose 11 stick and gain xx sticks).
- exchange yy sticks for 11 coal (you lose yy sticks and gain 11 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 kk torches. The answer always exists under the given constraints.
You have to answer tt independent test cases.
输入格式
The first line of the input contains one integer tt ( 1 \le t \le 2 \cdot 10^41≤t≤2?104 ) — the number of test cases. Then tt test cases follow.
The only line of the test case contains three integers xx , yy and kk ( 2 \le x \le 10^92≤x≤109 ; 1 \le y, k \le 10^91≤y,k≤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.
输出格式
For each test case, print the answer: the minimum number of trades you need to craft at least kk torches. The answer always exists under the given constraints.
题意翻译
题目描述:
你要获得 kk 个火把,最开始你有 11 根木棒。一个木棒和一个煤炭可以合成一个火把,你也可以通过两种交易方式得到木棒或煤炭:
- 通过给商人 11 根木棒,获得 xx 根木棒。
- 通过给商人 yy 根木棒,获得 11 个煤炭。
其中 2\leq x\leq 10^{9};1\leq y,k\leq 10^{9}2≤x≤109;1≤y,k≤109。
求得到 kk 个火把的最少交易次数。
输入输出样例
输入 #1复制
5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000
输出 #1复制
14 33 25 2000000003 1000000001999999999
代码
#include <stdio.h>
int main()
{long long int n;scanf ("%lld",&n);for (long long int i=0;i<n;i++) {long long int x,y,k;scanf ("%lld %lld %lld",&x,&y,&k);long long int sc=(y*k+k-1)/(x-1)+k;if ((y*k+k-1)%(x-1)!=0) { //若不能整次换完,则再换1次sc++;}printf ("%lld\n",sc);}return 0;
}
思路:
1木棒=x木棒 -即每次交易获得(x-1)根木棒
y木棒=1煤炭
1木棒+1煤炭=1火把
先算总共需要多少木棒,其中有分用来换煤炭和用来合成火把的木棒,k根火把需要k*y根木棒来换取k煤炭,还需k根木棒用来合成火把,初始1根木棒,即还需(k-1)根木棒,那么就是除了初始的一根木棒,需换k*y+(k-1)根木棒,换取这些木棒需要交易 ( k * y + k - 1 ) / ( x - 1 ) 次,再加上其中换煤炭的次数,最少交易次数即为( k * y + k - 1 ) / ( x - 1 )+k 次