Problem Description
Young theoretical computer scientist Fxx designed a game for his students.
In each game, you will get three integers X,k,t .In each step, you can only do one of the following moves:
1.X=X−i(0<=i<=t) .
2. if k|X,X=X/k .
Now Fxx wants you to tell him the minimum steps to make X become 1.
In each game, you will get three integers X,k,t .In each step, you can only do one of the following moves:
1.X=X−i(0<=i<=t) .
2. if k|X,X=X/k .
Now Fxx wants you to tell him the minimum steps to make X become 1.
Input
In the first line, there is an integer
T(1≤T≤20)
indicating the number of test cases.
As for the following T lines, each line contains three integers X,k,t(0≤t≤106,1≤X,k≤106)
For each text case,we assure that it's possible to make X become 1。
As for the following T lines, each line contains three integers X,k,t(0≤t≤106,1≤X,k≤106)
For each text case,we assure that it's possible to make X become 1。
Output
For each test case, output the answer.
Sample Input
2 9 2 1 11 3 3
Sample Output
4 3
Source
因为x很小,因此可以考虑f[i]表示1到i的最小次数
f[i]=min(f[i-x])
if i%k==0 f[i]=min(f[i/k])
对于第一个转移我们可以直接用单调队列维护
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int f[1500001];
int q[1500001];
int main()
{
int T;
scanf("%d",&T);
while(T>0)
{
T--;
int x,k,t;
scanf("%d%d%d",&x,&k,&t);
f[1]=0;
int l=1,r=1;
q[1]=1;
int i;
for(i=2;i<=x;i++)
{
while(l<r&&q[l]<i-t)
l++;
f[i]=f[q[l]]+1;
if(i%k==0)
f[i]=min(f[i],f[i/k]+1);
while(l<=r&&f[i]<=f[q[r]])
r--;
r++;
q[r]=i;
}
printf("%d\n",f[x]);
}
return 0;
}