Quiz(贪心,快速幂乘)

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 by1000000009 (109 + 9).

错误题数为n-m,可以n-m次在连续答对(k-1)道题时使下一道答错,共(n-m) * [(k-1) + 1] = (n-m)* k道题。

假设有x次连续答对k题,则有x * k + (n-m)* k + r = n,这里0 <= r < k,是余下的凑不成k道题目的题目数。得x = n / k - (n - m). 注意这里“/”是C++中向下取整的除法。

AC Code:

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const long long M = 1000000009;

long long POW(long long y)
{
    if(y == 0) return 1;
    long long res = POW(y >> 1);
    res = (res * res) % M;
    if(y & 1) res = (res << 1) % M;
    return res;
}

int main()
{
    long long n, m, k, s;
    while(scanf("%I64d %I64d %I64d", &n, &m, &k) != EOF){
        long long a = n / k;
        long long b = n - m;
        if(b >= a){
            s = m;
        }
        else{
            //a-b次连续答对k题,b次连续答对题数不足k
            long long r = POW(a - b + 1) - 2;
            if(r < 0) r += M;  //注意这里!
            s = (k * r) % M;
            s = (s + (m - (a - b) * k) % M) % M;
        }
        printf("%I64d\n", s);
    }
    return 0;
}

 

posted on 2013-08-26 21:00  铁树银花  阅读(530)  评论(0编辑  收藏  举报

导航