Codeforces 337C Quiz (Fast power)

Source: Internet
Author: User
Tags reset

Quiz
Time limit:1000ms Memory limit:262144kb 64bit IO format:%i64d &%i64u
Submit

Status

Practice

Codeforces 337C
Description
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's 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 (109 + 9).

Input
The single line contains three space-separated integers n, m 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 Input
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Hint
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 t Hough 2000000020 MoD 1000000009 is a smaller number.

Test instructions: A total of N to the question, this person answered the M-question correctly, the number of consecutive K-question scored double, ask how to answer to let him score the least
Key: The main core is to calculate the number of times doubled after how to double the minimum score, if doubled n times, then the resulting value is (2^n-1) * 2 with a quick power evaluation is good, plus do not double to get the value
The following code

 #include <cstdio> #include <cstring> #define LL Long long #define MOD 1000000009 ll Q
    Uickpow (ll N, ll m) {ll res = 1;
        while (m) {if (M & 1) {res = (RES * n)% MOD;
        } n = n * n% MOD;
    M >>= 1;
} return res;
    } int main () {int n, m, K, X1, x2, x3, x4;
    LL ans;
        while (scanf ("%d%d%d", &n, &m, &k)! = EOF) {ans = 0;
        x1 = n k;
        x2 = n/k * (k-1);
        x3 = x1 + x2;//This is the correct question can not double the upper limit if (M <= x3) {printf ("%d\n", m);
            } else {x4 = m-x3;//more than the upper limit more than one group doubles x1 = m-x4 * k;
            Ans = (Quickpow (ll) 2, (LL) x4)-1) * k% MOD;
            Ans = (ans * 2 mod + x1)% MoD;
        printf ("%lld\n", ans);
}} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.