C. Quiz
http://codeforces.com/problemset/problem/337/C
time limit per test
memory limit per test
input
output
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.
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
n, m and k (2 ≤ k ≤ n ≤ 109; 0 ≤ m ≤ n).
Output
1000000009 (109 + 9).
Sample test(s)
input
5 3 2
output
3
input
5 4 2
output
6
思路:
用贪心的思想做,把连对放前面。
然后就是自己在纸上画画找规律了。
反思:多找几组数据,不要漏了某些情况。
完整代码:
/*30ms,0KB*/
#include<cstdio>
const int mod = 1000000009;
__int64 pow(__int64 a, __int64 b)///a^b % mod
{
__int64 r = 1, base = a;
while (b)
{
if (b & 1)
r = r * base % mod;
base = base * base % mod;
b >>= 1;
}
return r;
}
int main()
{
__int64 n, m, k, temp, ans;
scanf("%I64d%I64d%I64d", &n, &m, &k);
if (n < k * (n - m + 1))
printf("%I64d", m);
else
{
temp = n / k - n + m;
ans = ((k * (pow(2, temp + 1) - 2 - temp)) % mod + m + mod) % mod;
printf("%I64d", ans);
}
return 0;
}