CF 337C(Quiz-pow2的嵌套)

来源:互联网 发布:淘宝店铺等级如何刷 编辑:程序博客网 时间:2024/05/16 11:37

C. Quiz
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

The single line contains three space-separated integers nm and k (2 ≤ k ≤ n ≤ 109; 0 ≤ m ≤ n).

Output

Print a single integer — the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 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 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.




这题本来是大水题。。。

可是我在写快速幂的时候忘了把内层循环改成pow2,。。失策失策、、、

Pia飞(考挂自己弱)

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (1000000009)long long mul(long long a,long long b){return (a*b)%F;}long long add(long long a,long long b){return (a+b)%F;}long long sub(long long a,long long b){return (a-b+abs(a-b)/F*F+F)%F;}typedef long long ll;ll n,m,k;ll pow2(ll a,int b){if (b==0) return 1;if (b==1) return a%F;ll p=pow2(a,b/2);p=p*p%F;if (b%2) p=p*a%F;return p;}int main(){//freopen("Quiz.in","r",stdin);//freopen(".out","w",stdout);//For(i,100) cout<<pow2(2,i)<<' ';cin>>n>>m>>k;ll m1=m;m=n-m;int maxk=n/k;if (m>=maxk) {cout<<m1%F<<endl;return 0;}else m=maxk-m;ll am=sub((4*pow2(2,m-1))%F,2);ll ans=add(am*k%F,m1-k*m);cout<<ans%F<<endl;return 0;}