程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> CF 337C(Quiz-pow2的嵌套)

CF 337C(Quiz-pow2的嵌套)

編輯:C++入門知識

C. Quiz
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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 test(s)
input
5 3 2
output
3
input
5 4 2
output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two 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 is to the question 4.

Also note that you are 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 though2000000020 mod 1000000009 is a smaller number.

 

 

 

 

這題本來是大水題。。。

可是我在寫快速冪的時候忘了把內層循環改成pow2,。。失策失策、、、

Pia飛(考掛自己弱)

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000009)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+abs(a-b)/F*F+F)%F;}
typedef long long ll;
ll n,m,k;
ll pow2(ll a,int b)
{
	if (b==0) return 1;
	if (b==1) return a%F;
	ll p=pow2(a,b/2);
	p=p*p%F;
	if (b%2) p=p*a%F;
	return p;
}
int main()
{
//	freopen("Quiz.in","r",stdin);
//	freopen(".out","w",stdout);
//	For(i,100) cout<<pow2(2,i)<<' ';
	
	cin>>n>>m>>k;
	ll m1=m;m=n-m;
	int maxk=n/k;
	if (m>=maxk) {cout<<m1%F<<endl;return 0;}
	else m=maxk-m;
	
	ll am=sub((4*pow2(2,m-1))%F,2);
	
	ll ans=add(am*k%F,m1-k*m);
	cout<<ans%F<<endl;
	
	
	
	return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved