转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
The single line contains three space-separated integers n, m and k (2 ≤ k ≤ n ≤ 109; 0 ≤ m ≤ n).
Print a single integer — the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
5 3 2
3
5 4 2
6
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though2000000020 mod 1000000009 is a smaller number.
题意:
有n道题目,要求在答对m道题目的情况下所得到的最小的分数。每答对一道题目 加一分,并且在连续答对k道题目的时候当前分数翻倍,然后开始重新计数连续答对的题数。当然中间若有答错,也开始重新计数。
分析:
首先可以考虑到在不翻倍的情况下,最少要错的题目为n/k,即每次在连续k-1题的时候答错。
如果n-m>=n/k,那么一定不需要翻倍,直接输出m就行。
在要发生翻倍的情况下,我们可以发现,在最前面几轮的时候翻倍是最好的。
所以我们可以将整个过程划分为两段,第一段是连续若干轮答对,然后翻倍。
第二段是每到连续答对k-1题的时候答错,一直到最后一题。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //##################### 2 //Author:fraud 3 //Blog: http://www.cnblogs.com/fraud/ 4 //##################### 5 #include <iostream> 6 #include <sstream> 7 #include <ios> 8 #include <iomanip> 9 #include <functional> 10 #include <algorithm> 11 #include <vector> 12 #include <string> 13 #include <list> 14 #include <queue> 15 #include <deque> 16 #include <stack> 17 #include <set> 18 #include <map> 19 #include <cstdio> 20 #include <cstdlib> 21 #include <cmath> 22 #include <cstring> 23 #include <climits> 24 #include <cctype> 25 using namespace std; 26 #define XINF INT_MAX 27 #define INF 0x3FFFFFFF 28 #define MP(X,Y) make_pair(X,Y) 29 #define PB(X) push_back(X) 30 #define REP(X,N) for(int X=0;X<N;X++) 31 #define REP2(X,L,R) for(int X=L;X<=R;X++) 32 #define DEP(X,R,L) for(int X=R;X>=L;X--) 33 #define CLR(A,X) memset(A,X,sizeof(A)) 34 #define IT iterator 35 typedef long long ll; 36 typedef pair<int,int> PII; 37 typedef vector<PII> VII; 38 typedef vector<int> VI; 39 const ll MOD=1000000009; 40 ll fast_pow(ll n,ll m){ 41 ll ret=1; 42 while(m){ 43 if(m&1)ret=ret*n%MOD; 44 n=n*n%MOD; 45 m>>=1; 46 } 47 return ret; 48 } 49 int main() 50 { 51 ios::sync_with_stdio(false); 52 ll n,m,k; 53 cin>>n>>m>>k; 54 ll wa=n-m; 55 ll min_wa=n/k; 56 ll ans=0; 57 if(wa>=min_wa){ 58 ans=m; 59 }else{ 60 ll tmp=fast_pow(2,min_wa-wa+1)-2; 61 tmp=(tmp+MOD)%MOD; 62 tmp=tmp*k%MOD; 63 ans=tmp+(m-(min_wa-wa)*k); 64 ans%=MOD; 65 } 66 cout<<ans<<endl; 67 68 69 return 0; 70 }