zoukankan      html  css  js  c++  java
  • 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
    ", s);
        }
        return 0;
    }
  • 相关阅读:
    Python基础__字符串拼接、格式化输出与复制
    Python基础__Python序列基本类型及其操作(1)
    Selenium 元素等待
    Selenium 键盘鼠标实践
    Selenium 元素定位
    Selenium 启动浏览器
    Selenium Webdriver概述
    linux虚拟机安装和xshell远程连接
    python实现dubbo接口的调用
    Python+Appium自动化测试(6)-元素等待方法与重新封装元素定位方法
  • 原文地址:https://www.cnblogs.com/cszlg/p/3283685.html
Copyright © 2011-2022 走看看