可惜没如果=_=
时光的河入海流

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=Xi(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(1T20) indicating the number of test cases.

As for the following T lines, each line contains three integers X,k,t(0t106,1X,k106)

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
 

 

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 }

 

posted on 2017-10-17 14:57  珍珠鸟  阅读(281)  评论(0编辑  收藏  举报