https://s3.amazonaws.com/codechef_shared/download/translated/SEPT15/mandarin/BANROB.pdf
题目描述
两个江洋大盗(大厨也是其中之一,当然是更厉害的那个)在 Churuland 国家银行的金库里相遇了。他们都吓尿了!事实上,他们都没想到会在这样的地方遇到一位同样想来洗劫银行在 Churufest 2015 期间积累的资金的同行。
他们统计了金库中的钱,恰好为十亿。现在他们要决定如何瓜分这些钱。有一个问题是:在警察赶到之前,他们只剩 M 分钟可以撤离银行。同时,他们在金库里呆的越久,最终能搬走的钱就越少。正式地说,他们马上走的话可以带走十亿元,但如果他们选择 t 分钟后走,他们就只能带走 10亿 × pt 元,其中 p 是一个不大于 1 的非负常数。而当 t = M 时,他们就会被捕,也带不走任何钱。然而,在他们确定分赃方案之前,他们是不会离开的。
分赃的过程是这样的:从第一分钟起(即 t = 0),在每分钟的开始,他们中的一人会提出一个分赃方案,如果对方同意,他们就会照此方案带钱离开,否则他们就等到下一分钟的开始,由另一方提出方案。为了不被抓获,他们最晚只能在第 M 分钟的开始提出方案(即t = M − 1)。两个贼都想最大化自己的收获,但如果有两个方案他能拿到一样多的钱,他会选择盗走的总金额更大的那个。他们就要开始分赃了,而大厨将第一个提出方案。你想知道最后的分赃方案会是什么,假设两位大盗都以最优策略选择并且钱是可以以任意实数金额拆分的。
题解:
https://discuss.codechef.com/questions/74775/banrob-editorial
PROBLEM LINK:
Author: Kirill
Tester: Kevin Atienza
Editorialists: Pushkar Mishra and Suhash Venkatesh
DIFFICULTY:
Easy
PREREQUISITES:
Math, Binary Exponentiation, Sum of Geometric Progression
PROBLEM:
Given that two thieves have 1 billion to divide amongst them according to a particular method, report the amounts both the thieves will receive upon division. The particular method here is that after a certain amount of time t , only (1 Billion * pt ) amount of money can be taken, where 0≤p≤1 . Also, there are only M minutes to divide the stolen money and escape.
EXPLANATION:
Subtask 1
Let us start by taking an example. Let
p
= 0.5 and
t
= 4. We not proceed minute by minute.
-
First minute
Money left to take = 109 .
Thief 1 to propose a plan. -
Second minute
Money left to take = 109∗0.5 .
Thief 2 to propose a plan. -
Third minute
Money left to take = 109∗0.52 .
Thief 1 to propose a plan. -
Fourth minute
Money left to take = 109∗0.53 .
Thief 2 to propose a plan.
After the fourth minute, both the thieves will lose all the money. So, let's go back in time minute by minute and see what the optimal division would be.
- If in the fourth minute, Thief 2 proposes a division of 0 and 109∗0.53 respectively, Thief 1 will accept it.
- If in the third minute, Thief 1 proposes a division of 109∗0.52−(109∗0.53) and 109∗0.53 respectively, Thief 2 will accept it.
- If in the second minute, Thief 2 proposes a division of 109∗0.5−(109∗0.52)+(109∗0.53) and (109∗0.52)−(109∗0.53) respectively, Thief 1 will accept it.
- Finally, if in the second minute, Thief 1 proposes a division of 109−(109∗0.5)+(109∗0.52)−(109∗0.53) and (109∗0.5)−(109∗0.52)+(109∗0.53) respectively, Thief 2 will accept it.
The acceptances will be because the theif accepting the plan can in no way gain more money in the future than being offered at that point in time. Please note this point in the question:
"Each thief wants to maximize his earnings, but if there are two plans with the same amounts for him, he would choose the one which leads to a larger total amount of stolen dollars."
We can make a very important observation here. The amount that Thief 1 will gain is given by:
val
=
109(p0−p1+p2−p3…pM−1)
. Thus, the amount that Thief 2 gets is
109−val
.
Subtask 2
If we observe the terms inside the bracket in the expression we got for
val
in subtask 1, we can see that it is basically the summation of terms of a Geometric Progression (GP).
The GP is:
1
,
−p
,
p2
,
−p3
,
…
We common ratio
r
=
−p
and the first term of GP
a
=
1
. The formula to calculate the sum of first
n
terms of a GP is well known:
S=a(rn−1)r−1
Thus, in this case,
S=((−p)M−1)(−p)−1
.
This will work in
O(M)
with linear exponentiation.
Subtask 3
The solution of subtask 2 can be optimised by using binary exponentiation to calculate
(−p)M
. This will allow us to compute the sum of the GP in
O(logM)
.
COMPLEXITY:
O(logM) per test case.
SAMPLE SOLUTIONS:
#include<bits/stdc++.h>
using namespace std;
#define VAL (int)1e9
int main() {
int T, M;
double p;
scanf("%d", &T);
while (T--) {
scanf("%d%lf", &M, &p);
double fst = VAL * (1 - pow(-p, M)) / (p + 1);
double snd = VAL - fst;
printf("%.4lf %.4lf\n", fst, snd);
}
return 0;
}