#P9963. [THUPC 2024 初赛] 前缀和

[THUPC 2024 初赛] 前缀和

Problem Description

Xiaolan really likes random numbers.

They first choose a real number 0<p<10 < p < 1, and then generate nn random numbers x1,,xnx_1,\dots,x_n. Each number is generated independently in the following way:

  • xix_i equals 11 with probability pp, equals 22 with probability (1p)p(1-p)p, equals 33 with probability (1p)2p(1-p)^2p, and so on.

After generating these random numbers, Xiaoai computes the prefix sums of this sequence and obtains a sequence y1,,yny_1,\dots,y_n.

Given 1lrn1\leq l\leq r\leq n, Xiaolan wants to know: in expectation, how many yiy_i fall within [l,r][l, r]?

Input Format

One line contains four numbers n,p,l,rn, p, l, r. It is guaranteed that 1lrn1091\leq l\leq r\leq n\leq 10^9, and pp has at most 66 digits.

Output Format

Output a real number, the answer. You need to ensure that the absolute or relative error does not exceed 10610^{-6}.

3 0.5 1 2

1.000000

Hint

Sample #1 Explanation

With probability 1/41/4, x1=1x_1=1 and x2>1x_2>1. In this case, only y1y_1 falls within [1,2][1, 2].

With probability 1/41/4, x1=1x_1=1 and x2=1x_2=1. In this case, y1,y2y_1,y_2 fall within [1,2][1, 2].

With probability 1/41/4, x1=2x_1=2. In this case, only y1y_1 falls within [1,2][1, 2].

So the expectation is 1/4(1+2+1)=11/4\cdot (1 + 2 + 1) = 1.

Problem License

From the THUPC2024 (Tsinghua University Student Programming Contest and Collegiate Invitational) Preliminary.

The following “this repository” all refer to the official THUPC2024 Preliminary repository (https://github.com/ckw20/thupc2024_pre_public)

  1. Any organization or individual may use or repost the problems in this repository free of charge.

  2. When using the problems in this repository, any organization or individual should do so for free and publicly. It is strictly forbidden to profit from these problems or add special access restrictions to them.

  3. If possible, please also provide ways to obtain resources such as testdata, standard solutions, and editorials when using the problems in this repository; otherwise, please attach the GitHub link of this repository.

Translated by ChatGPT 5