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;
    }
  • 相关阅读:
    【转】正则表达式参考
    php七牛批量删除空间内的所有文件方法
    星级评分(全星)
    [转]No 'AccessControlAllowOrigin' header is present on the requested resource.'Ajax跨域访问解决方案
    在canvas中插入图片
    vue2.0 引入css文件
    修改input框样式
    js实现简单分页浏览
    [转] js 移动端 长按事件
    TypeError: console.log(…) is not a function
  • 原文地址:https://www.cnblogs.com/cszlg/p/3283685.html
Copyright © 2011-2022 走看看