Qwerty the Ranger took up a government job and arrived on planet Mars. He should stay in the secret lab and conduct some experiments on bacteria that have funny and abnormal properties. The job isn't difficult, but the salary is high.
At the beginning of the first experiment there is a single bacterium in the test tube. Every second each bacterium in the test tube divides itself intok bacteria. After that some abnormal effects createb more bacteria in the test tube. Thus, if at the beginning of some second the test tube hadx bacteria, then at the end of the second it will havekx + b bacteria.
The experiment showed that after n seconds there were exactlyz bacteria and the experiment ended at this point.
For the second experiment Qwerty is going to sterilize the test tube and put theret bacteria. He hasn't started the experiment yet but he already wonders, how many seconds he will need to grow at leastz bacteria. The ranger thinks that the bacteria will divide by the same rule as in the first experiment.
Help Qwerty and find the minimum number of seconds needed to get a tube with at leastz bacteria in the second experiment.
The first line contains four space-separated integers k,b, n andt (1 ≤ k, b, n, t ≤ 106) — the parameters of bacterial growth, the time Qwerty needed to growz bacteria in the first experiment and the initial number of bacteria in the second experiment, correspondingly.
Print a single number — the minimum number of seconds Qwerty needs to grow at leastz bacteria in the tube.
3 1 3 5
2
1 4 4 7
3
2 2 4 100
0
这题就是要你得到一个递推公式 x(i+1)=k * x(i) + b; 实验一的x1(0)=1, 实验二的x2(0)=t; 目标是:求最小的q, 使得x2(q)>=x1(n);
当k!=1时,对于递推公式,令h=b/(k-1), y(i)=x(i)+b, 则y(i+1)=k * y(i), 既y(i)是以y(0)为首项,k为公比的等比数列。由于实验一和实验二的首项已知,x(n)=k^n * (x(0)+h)-h;
为了避免小数和log运算产生精度误差,等式两边同时乘以k-1, 最后得到不等式(k-1+b) * k^(n-q)<=(k-1) * t + b, 其中q要尽可能小(废话,求最少的天数)。求法是令i=n-q, i尽可能大既可。
当k=1时,x(n)就是等差数列,直接算出答案就可以了。
由于(k-1)*t超过了int , 所以读入时就要__int64 !!!!!
#include <iostream>
#include <math.h>
using namespace std;
int main(){
__int64 k,b,n,t,q,i;
cin>>k>>b>>n>>t;
if (k>1){
__int64 tmp;
__int64 abc;
tmp=(k-1+b);
i=0;q=0;abc=(k-1)*t+b;
while (tmp<=abc) {i++;tmp*=k;}i--;
q=n-i;
}
else {
q=0;
while (q*b+t<n*b+1) q++;
}
if (q<0) q=0;
cout<<q<<endl;
return 0;
}