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=Xk
.
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。
Output
For each test case, output the answer.
Solution
DP[i]的值可以由
ik
(如果可行)和DP[i-t]到DP[i-1]中转移得。
那么用单调队列维护DP[i-t]到DP[i-1]的最小值即可
#include<stdio.h>
#include<algorithm>
int x,k,t,T,ans,d[1000005],dp[1000005];
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d%d",&x,&k,&t);
if (x==1) {puts("0");continue;}
if (k==1)
{
printf("%d\n",(x+t-2)/t);
continue;
}
if (t==0)
{
int i=0,a=1;
while (a<x) i++,a*=k;
printf("%d\n",i);
continue;
}
int l=1,r=1;d[1]=1;
for (int i=2;i<=x;i++)
{
while (d[l]+t<i && l<=r) l++;
dp[i]=std::min(dp[d[l]]+1,(i%k==0)?dp[i/k]+1:233333333);
while (dp[i]<=dp[d[r]] && l<=r) r--;
d[++r]=i;
}
printf("%d\n",dp[x]);
}
}