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 into k bacteria. After that some abnormal effects create b more bacteria in the test tube. Thus, if at the beginning of some second the test tube had x bacteria, then at the end of the second it will have kx + b bacteria.
The experiment showed that after n seconds there were exactly z bacteria and the experiment ended at this point.
For the second experiment Qwerty is going to sterilize the test tube and put there tbacteria. He hasn't started the experiment yet but he already wonders, how many seconds he will need to grow at least z 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 least z bacteria in the second experiment.
The first line contains four space-separated integers k, b, n and t (1 ≤ k, b, n, t ≤ 106) — the parameters of bacterial growth, the time Qwerty needed to grow zbacteria 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 least z bacteria in the tube.
3 1 3 5
2
1 4 4 7
3
2 2 4 100
0
题解:
题意:
一开始有1个细胞的时候,a=1,每一次增殖的结果为a*k+b,经过n次到达了z个细胞,现在从x个细胞开始问要结果几次能达到不少于z个细胞
一开始暴力模拟结果炸了long long,其实没必要算出z值
我们把每一次的细胞增殖的结果看做一层,初始总数为1,看做第一层,经过n层总数达到了z
我们可以算出到达x所需要的层数,然后用n减去1到达x的层数就是x到达z的层数了
但是要注意可能经过式子计算达到x的时候数目超出了x,那么所需要的层数就要多一层,这个画一个类似于金字塔的图形,然后x在一层的中间就能明白了
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
int main()
{
ll n,k,b,ans,i,j,z=1,x;
scanf("%lld%lld%lld%lld",&k,&b,&n,&x);
ans=n;
while(ans>0&&z<x)
{
z=b+k*z;
ans--;
}
if(z>x)
ans++;
printf("%lld\n",ans);
return 0;
}