0
点赞
收藏
分享

微信扫一扫

HDU5945 Fxx and game(DP+单调队列优化)

Fxx and game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2101    Accepted Submission(s): 573

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.

 

 

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.

 

 

Sample Input

2 9 2 1 11 3 3

 

 

Sample Output

4 3

 

 

Source

​​BestCoder Round #89​​

 

 

Recommend

wange2014   |   We have carefully selected several similar problems for you:  ​​6216​​​ ​​6215​​​ ​​6214​​​ ​​6213​​​ ​​6212​​ 

哇哈哈哈哈这题上数学课的时候发现忘记把第一个数存入队列了(上课的时候没事干思忖了一下单调队列的原理还有单调队列和单调栈的用途),然后下课飞奔至姬房,顺带发现忘记压入后面的数了,反正一个下课就把这题搞好咯~ 不虚此行,撤!

1 #include "bits/stdc++.h"
2 using namespace std;
3 typedef long long LL;
4 const int MAX=1e6+5;
5 int T,n,k,t;
6 int dp[MAX];
7 deque <int> q;
8 int main(){
9 freopen ("fxx.in","r",stdin);
10 freopen ("fxx.out","w",stdout);
11 int i,j;
12 scanf("%d",&T);
13 int an;
14 while (T--){
15 scanf("%d%d%d",&n,&k,&t);
16 if (t==0){
17 an=0;
18 while (n!=1){n/=k;an++;}
19 printf("%d\n",an);
20 continue;
21 }
22 if (k==0){
23 printf("%d\n",(n-2)/t+1);
24 continue;
25 }
26 dp[1]=0;
27 while (q.size()) q.pop_back();
28 q.push_back(1);
29 for (i=2;i<=n;i++){
30 dp[i]=999999999;
31 if (i%k==0) dp[i]=min(dp[i],dp[i/k]+1);
32 while (q.size() && (i-t)>q.front()) q.pop_front();
33 if (q.size()) dp[i]=min(dp[i],dp[q.front()]+1);
34 while (q.size() && dp[i]<dp[q.back()]) q.pop_back();
35 q.push_back(i);
36 }
37 printf("%d\n",dp[n]);
38 }
39 return 0;
40

 

举报

相关推荐

0 条评论