Young theoretical computer scientist Fxx designed a game for his students.
In each game, you will get three integers X,k,t X,k,t.In each step, you can only do one of the following moves:
1.X=X−i(0<=i<=t) 1.X=X−i(0<=i<=t).
2. 2.if k|X,X=X/k k|X,X=X/k.
Now Fxx wants you to tell him the minimum steps to make X X become 1.
Input
In the first line, there is an integer
T(1≤T≤20)
T(1≤T≤20) indicating the number of test cases.
As for the following T T lines, each line contains three integers X,k,t(0≤t≤106,1≤X,k≤106) X,k,t(0≤t≤106,1≤X,k≤106)
For each text case,we assure that it's possible to make X X become 1。
Output
For each test case, output the answer.
Sample Input
Sample Output
In each game, you will get three integers X,k,t X,k,t.In each step, you can only do one of the following moves:
1.X=X−i(0<=i<=t) 1.X=X−i(0<=i<=t).
2. 2.if k|X,X=X/k k|X,X=X/k.
Now Fxx wants you to tell him the minimum steps to make X X become 1.
Input
As for the following T T lines, each line contains three integers X,k,t(0≤t≤106,1≤X,k≤106) X,k,t(0≤t≤106,1≤X,k≤106)
For each text case,we assure that it's possible to make X X become 1。
2 9 2 1 11 3 3
43
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
using namespace std;
typedef long long LL;
int dp[1000005], a[1000005];
int pre, tail;
int main()
{
int T, x, k, t;
cin >> T;
while (T--)
{
scanf("%d%d%d", &x, &k, &t);
pre = 0, tail = 0;
a[tail++] = x;
dp[x] = 0;
for (int i = x - 1; i >= 1; i--)
{
dp[i] = (t != 0) ? dp[a[pre]] + 1 : 9999999;
if ((1LL * i*k) <= (LL)x) dp[i] = min(dp[i], dp[i*k] + 1);
if (a[pre] - t == i) pre++;
while (dp[a[tail - 1]] >= dp[i] && tail>pre) tail--;
a[tail++] = i;
}
printf("%d\n", dp[1]);
}
return 0;
}
不知道为什么这样类似暴力都能过,而用单增队列却A不了。路过的大牛解释一下吧