您的位置:首页 > 其它

URAL 1831|Tsyfirkin's Lesson|期望DP

2016-04-26 08:35 357 查看
中文大意:计算两个k位数(没有前缀0)加法的期望时间计算(跟我们竖式加法一样从低位到高位逐位相加)。规则:一秒钟来写一个数字或对进位1做个记号。如果一次一位加法是0+?或者1+?或者?+0或者?+1只需要1秒,如果两个数都大于1就要2秒,如果a+b或者b+a在之前算过,这次再算那么也只用1秒。计算期望秒数。

稍后给方程。。

#include <stdio.h>
int main() {
static double f[5001];
double p = 1, q = 1, r = 1, x = 3.54;
f[1] = 316. / 81;
for (int i = 2; i <= 5000; ++i) {
p *= 0.99; q *= 0.98; r *= 0.1;
f[i] = (260 + 8 * p + 56 * q - 8 * r) / 81 + x;
x += 3 + 0.08 * p + 0.56 * q - r / 10;
}
for (int i; scanf("%d", &i) != EOF; printf("%.8f\n", f[i]));
return 0;
}


我在vjudge上看到了maxflow

Tsyfirkin’s Lesson

Time Limit: 1000MS

Description

Tsyfirkin’s new student Feofan is much more intelligent and bright than Mitrofanushka. In the first three lessons, he has learned to add positive integers in columns, not very quickly but without mistakes. For this he uses the following algorithm.

Feofan adds numbers from right to left: first he adds units, then tens, and so on.

He takes the pair of digit in each successive column and adds them.

If a 1 is carried from the previous column, Feofan adds it to the obtained sum.

He writes the rightmost digit of the sum in the answer line and, if necessary, makes a mark about carrying 1 to the next column.

If there are no more columns and there is a 1 carried from the leftmost column, then Feofan writes 1 on the left of the answer.

Feofan needs one second to write one digit or to make a mark about carrying a 1. If at least one of summands is 0 or 1, then Feofan spends one second adding them; if both numbers are greater than 1, then he adds them in two seconds. However, having added two numbers, Feofan remembers the result and can recall it afterwards in one second. If he needs to calculate a + b and he has calculated b + a before, then he also can use the result obtained earlier. Unfortunately, Feofan does not remember all the results at the next lesson and has to calculate and remember them anew.

For example, Feofan adds the numbers 526 and 625 in 12 seconds: he spends four seconds for writing the digits of the answer, two seconds for making marks about carrying over, two seconds for calculating each of the sums 6 + 5 and 2 + 2, and one second for calculating each of the sums 4 + 1 and 5 + 6 (since he remembers from the previous calculations that 6 + 5 = 11).

In the beginning of a new lesson Tsyfirkin writes on the blackboard two k-digit integers and gives Feofan the task of adding them. Tsyfirkin has chosen each of the integers with equal probabilities from the set of positive k-digit integers without leading zeros. Find the mathematical expectation of the time Feofan will need to add the integers.

Input

In the only line you are given the integer k (1 ≤ k ≤ 5000).

Output

Output the expected time of adding two positive k-digit integers with an absolute or relative error of at most 10 −6.

Sample Input

2


Sample Output

7.51530864
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: