Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud
Quiz
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 is answers a question correctly, the number on this counter increases by 1. If The player answers a question incorrectly, the counter is reset, that's, the number on it reduces to 0. If after a answer the counter reaches the Number K , then it is reset, and the player ' s score is doubled. Note 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 is set to Zero.
Manao remembers that he had answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to the figure of what He minimum score could be. Help him and compute the remainder of the corresponding number after division by 1000000009 (9 + 9).
Input
The single line contains three space-separated integers n, m and k (2≤ k ≤ n ≤10 9; 0≤ m ≤ n).
Output
Print a single integer-the remainder from division of Manao's minimum possible score in the quiz by 1000000009 ( 9 + 9).
Sample Test (s) input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and he score would double for each of the 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 are to the question 4.
Also Note that is 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.
Test instructions
There are N-way questions that require the smallest score to get the correct M-way problem. Add a point to each correct question, and double the current score when you correctly correct the K-course title, and then start to re-count the number of consecutive correct questions. Of course, if there is a wrong answer, also began to re-count.
Analysis:
First of all, we can consider that in the case of not doubling, at least the wrong topic is n/k, that is, each time in a continuous k-1 question answer wrong.
If n-m>=n/k, then certainly do not need to double, direct output m on the line.
In the case of doubling, we can see that doubling is the best in the first few rounds.
So we can divide the whole process into two paragraphs, the first one is the correct number of consecutive rounds, and then doubles.
The second paragraph is each to the consecutive correct k-1 question when the answer is wrong, until the last question.
1 //#####################2 //Author:fraud3 //Blog:http://www.cnblogs.com/fraud/4 //#####################5#include <iostream>6#include <sstream>7#include <ios>8#include <iomanip>9#include <functional>Ten#include <algorithm> One#include <vector> A#include <string> -#include <list> -#include <queue> the#include <deque> -#include <stack> -#include <Set> -#include <map> +#include <cstdio> -#include <cstdlib> +#include <cmath> A#include <cstring> at#include <climits> -#include <cctype> - using namespacestd; - #defineXinf Int_max - #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) in #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) to #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) + #defineDEP (x,r,l) for (int x=r; x>=l; x--) - #defineCLR (a,x) memset (a,x,sizeof (A)) the #defineIT iterator *typedefLong Longll; $typedef pair<int,int>PII;Panax Notoginsengtypedef vector<pii>VII; -typedef vector<int>VI; the Constll mod=1000000009; + ll Fast_pow (ll n,ll m) { All ret=1; the while(m) { + if(m&1) ret=ret*n%MOD; -n=n*n%MOD; $m>>=1; $ } - returnret; - } the intMain () - {WuyiIos::sync_with_stdio (false); the ll N,m,k; -Cin>>n>>m>>K; Wull wa=n-m; -ll min_wa=n/K; Aboutll ans=0; $ if(wa>=Min_wa) { -cn1=m; -}Else{ -ll Tmp=fast_pow (2, min_wa-wa+1)-2; Atmp= (tmp+mod)%MOD; +tmp=tmp*k%MOD; theAns=tmp+ (M-(MIN_WA-WA) *k); -ans%=MOD; $ } thecout<<ans<<Endl; the the the return 0; -}
View Code
Codeforces 337C Quiz (greedy)