The 2022 ICPC Asia Xian Regional Contest--C. Clone Ranran

Ranran needs to prepare a contest! A contest is made of c problems, and Ranran can do one of the following two things:

  • Clone himself in a minutes. That is, after a minutes, there will be one more Ranran.
  • Prepare a problem in b minutes. That is, after b minutes, there will be one more problem.

Note that a cloned Ranran can also do the two things above. A Ranran cannot do the two things at the same time.

Ranran wants to prepare the contest as fast as possible. But he is very lazy, so he asks you to find the minimum number of minutes to prepare the contest.

You need to answer T queries independently.

Input

The first line contains an integer T (1≤T≤10^5).

Each of the next T lines contains three integers aa, bb and cc (1≤a,b,c≤109), representing a query.

Output

For each test case, output a line with a single integer representing the answer.

Example

input

5
1 1 1
2 3 3
9 9 9
3 26 47
1064 822 1048576

output

1
7
45
44
21860

题意:主人公初始有0个题目,可以花a分钟克隆自己,可以花b分钟使每个自己创造1个题目,问创造c个题目的最小耗时。

解析:可以发现,克隆自己是2的幂次,可以推出,如果需要克隆N次,那么先克隆N次,最后再操作2肯定是最合理的,因此我们可以枚举克隆的次数,因为是指数,所以最多才31次(因为c最大10的9次)

#include 
#include 
using namespace std;
typedef long long ll;
void solve()
{
	ll a,b,c,r=1,minn=1e15;//最小值初始化要大一点
	scanf("%lld%lld%lld",&a,&b,&c);
	for(int i=0;i<32;i++)//克隆i次
	{
		minn=min(minn,i*a+((c-1)/r+1)*b);//更新答案
		r*=2;
	}
	printf("%lld\n",minn);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--) solve();
	return 0;
}

你可能感兴趣的:(算法,c++,c语言)