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

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

Input

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

Output

Print a single integer — the remainder from division of Manao's minimum possible score in the quiz by1000000009 (109 + 9).spa

錯誤題數爲n-m,能夠n-m次在連續答對(k-1)道題時使下一道答錯,共(n-m) * [(k-1) + 1] = (n-m)* k道題。code

假設有x次連續答對k題,則有x * k + (n-m)* k + r = n,這裏0 <= r < k,是餘下的湊不成k道題目的題目數。得x = n / k - (n - m). 注意這裏「/」是C++中向下取整的除法。blog

AC Code:three

#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;
}
相關文章
相關標籤/搜索