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 = ; long long POW(long long y)
{
if(y == ) return ;
long long res = POW(y >> );
res = (res * res) % M;
if(y & ) res = (res << ) % 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 + ) - ;
if(r < ) r += M; //注意这里!
s = (k * r) % M;
s = (s + (m - (a - b) * k) % M) % M;
}
printf("%I64d\n", s);
}
return ;
}

Quiz(贪心,快速幂乘)的更多相关文章

  1. Codeforces1062C. Banh-mi(贪心+快速幂)

    题目链接:传送门 题目: C. Banh-mi time limit per test second memory limit per test megabytes input standard in ...

  2. 贪心,打表(或者快速幂), UVA - 11636

    题目链接: https://cn.vjudge.net/problem/34398/origin 题目比较简单,就是水题,基础贪心,大于所需的即可: AC代码: 打表: #include <cm ...

  3. Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂

    A. Table time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  4. 小总结:快速幂+贪心————Bit Mask____UVA 10718 多多去理解去温习哦!

    传送门:https://vjudge.net/problem/UVA-10718 Preview: bitstream:a flow of data in binary form. in bit-wi ...

  5. “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】

    黑白图像直方图 发布时间: 2017年7月9日 18:30   最后更新: 2017年7月10日 21:08   时间限制: 1000ms   内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...

  6. 【BZOJ】2553: [BeiJing2011]禁忌 AC自动机+期望+矩阵快速幂

    [题意]给定n个禁忌字符串和字符集大小alphabet,保证所有字符在集合内.一个字符串的禁忌伤害定义为分割能匹配到最多的禁忌字符串数量(一个可以匹配多次),求由字符集构成的长度为Len的字符串的期望 ...

  7. Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check

    A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...

  8. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  9. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  10. hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                  ...

随机推荐

  1. EntityFramework 4/5/6 中执行自定义SQL语句

    参考:http://www.cnblogs.com/chengxiaohui/articles/2092001.html 在EF4(.NET  4)中,我们有了全新的API:ObjectContext ...

  2. AngularJS的date 过滤器

    date 过滤器可以将日期格式化成需要的格式.AngularJS中内置了几种日期格式,如果没有指定使用任何格式,默认会采用 mediumDate 格式,下面的例子中展示了这个格式. ·下面是内置的支持 ...

  3. Android之把手机的3g流量共享出来让其他人连接这个wifi

    转自:http://blog.csdn.net/luoboo525/article/details/7883998   亲测可用 用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi ...

  4. 转:LIRe 源代码分析

    1:整体结构 LIRE(Lucene Image REtrieval)提供一种的简单方式来创建基于图像特性的Lucene索引.利用该索引就能够构建一个基于内容的图像检索(content- based ...

  5. Advanced Puppet 系列的前言

    什么是Advanced 在网络上,你能找到大量关于Puppet的安装,配置以及基础用法的文章和博客.你在通过一段时间的实战后,熟练掌握了Puppet基础用法,随着你管理的集群日益扩大,你的部署逻辑越来 ...

  6. Tomcat远程调试

    1.如果tomcat在Windows下 打开%CATALINE_HOME%/bin下的文件catalina.bat,加入下面这行: set CATALINA_OPTS=-server -Xdebug ...

  7. GTD中落地执行篇

    前面几篇主要是分享GTD对事情进行 ”收集“,“分类”,“组织”.今天主要是想分享“落地执行” 先来看一个案例 (案例 来自于<小强升职记>) 通过这个案例我们看出 1: 当我们通过对事情 ...

  8. 一个简单的JavaScript Map

    用js写了一个Map,带遍历功能,请大家点评下啦. //map.js Array.prototype.remove = function(s) { for (var i = 0; i < thi ...

  9. Vmware9.0打开早期版本报错:this virtual machine’s policies are too old to be run by this version of vmware workstation”

    VMWare从6.0升级到9.0,打开以前的虚拟机报错如下: “this virtual machine’s policies are too old to be run by this versio ...

  10. mac os x用macport安装redis

    一.Redis简要介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的 ...