Let us define the beauty of a sequence (a_1,... ,a_n) as the number of pairs of two adjacent elements in it whose absolute differences are d. For example, when d=1, the beauty of the sequence (3, 2, 3, 10, 9) is 3.
There are a total of n^m sequences of length m where each element is an integer between 1 and n (inclusive). Find the beauty of each of these n^m sequences, and print the average of those values.
Constraints
0 \leq d < n \leq 10^9
2 \leq m \leq 10^9
All values in
input are integers.
Input
Input is given from Standard Input in the following format:
n m d
Output
Print the average of the beauties of the sequences of length m where each element is an integer between 1 and n. The output will be judged correct if the absolute or relative error is at most 10^{-6}.
Examples
Input
2 3 1
Output
1.0000000000
Input
1000000000 180707 0
Output
0.0001807060
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to compute the average beauty over all sequences of length m, each element in {1,...,n}. The beauty is defined as number of pairs of adjacent elements with absolute difference exactly d. For each adjacent pair (i,i+1), we count whether |a_i - a_{i+1}| = d. The total beauty over all sequences is sum over all sequences of sum_{i=1}^{m-1} I(|a_i - a_{i+1}|=d). The average beauty is then (1/(n^m)) * sum_{sequences} sum_{i=1}^{m-1} I(...). By linearity, average = (m-1) * (probability that for a random adjacent pair, the absolute difference is d). Since each sequence is equally likely, and each position symmetric, the average per pair is the same.
So we need: For two independent uniformly random numbers X and Y from {1,...,n} (with replacement? Actually, in sequences, adjacent elements are chosen independently? Wait: In counting all sequences, each sequence a_1,...,a_m is equally likely. So for a fixed position i, the joint distribution of (a_i, a_{i+1}) is uniform over all n^2 pairs? Because each sequence is constructed by choosing each a_j independently uniformly from 1..n? Actually the distribution over sequences is uniform over all n^m sequences, which corresponds to each a_j being independent uniform over {1..n} (with each sequence equally likely). Indeed, if we choose each element uniformly and independently, the total number is n^m and all sequences equally likely. So (a_i, a_{i+1}) are independent and each uniform on 1..n, and they are independent of each other? For a fixed i, a_i and a_{i+1} are independent because they are chosen independently? In the uniform distribution over all sequences, the joint distribution factorizes: P(a_i = x, a_{i+1}=y) = 1/n * 1/n = 1/n^2. So they are independent uniform. So yes.
Thus, the probability that |X-Y| = d for two independent uniform integers from 1 to n is something we can compute. Then average beauty = (m-1) * P(|X-Y|=d). This is the answer.
Now compute P(|X-Y| = d). For given n and d (0 <= d < n). Since X,Y in [1,n] integer uniform. Need count number of pairs (x,y) such that |x-y| = d, divided by n^2.
We can compute: For each x, y can be x+d or x-d, but must be within 1..n. So count depends on d.
If d=0: condition is x=y. Number of pairs = n. So probability = n / n^2 = 1/n.
If d>0: For each x, y = x+d is valid if x+d <= n; y = x-d valid if x-d >= 1. But note these two may coincide if d=0; but not when d>0. However careful: Counting each (x,y) pair: We can count number of x such that x+d ∈ [1,n] gives n-d possibilities (x from 1 to n-d). Similarly, x-d gives x from d+1 to n, also n-d possibilities. So total 2*(n-d) pairs? But wait, check if double counted: For d>0, these two sets are disjoint because y=x+d and y=x-d cannot both be integer for same (x,y) pair? Actually for a given (x,y) with |x-y|=d, exactly one of x-y = d or x-y = -d holds. So each ordered pair (x,y) corresponds to either x = y+d (if x>y) or y = x+d (if x<y). So they are counted once in the first category (x+d) when x<y, and once in second (x-d) when x>y. So total ordered pairs = 2*(n-d). But careful: When d=0 it's different. So for d>0, number of ordered pairs = 2*(n-d). So probability = 2*(n-d) / n^2.
But need to consider d < n, yes. So for d>0: P = 2*(n-d)/n^2.
Double-check: For n=2,d=1: then 2*(2-1)/4 = 2/4=0.5. Let's enumerate: pairs (1,2) diff=1, (2,1) diff=1, (1,1) diff=0, (2,2) diff=0. So yes 2 out of 4 => 0.5. For m=3, average beauty = (3-1)*0.5 = 1.0, matches example.
For second example: n=1e9, m=180707, d=0. Then P = 1/n = 1e-9. Then average beauty = (180707-1)*1e-9 = 180706 * 1e-9 = 0.000180706. Which matches output: 0.0001807060.
So answer appears straightforward: average = (m-1) * p, where p = (d==0 ? 1/n : 2*(n-d)/n^2). But careful: What about d=0? We used 1/n. But also formula 2*(n-d) when d=0 gives 2n, which is wrong, so we treat separately.
Thus answer = (m-1) * ( if d == 0: 1.0/n else: 2.0*(n-d)/ (n*n) ).
But wait, is there any nuance? The problem statement says: "the beauty of a sequence (a1,..., an) as the number of pairs of two adjacent elements in it whose absolute differences are d." So for d=0, they count pairs where absolute difference is 0, i.e., equal adjacent elements. So indeed probability = 1/n.
Also constraints: n up to 1e9, m up to 1e9, so (m-1) * something could be large? But average beauty is at most m-1, which is up to 1e9-1, which fits in double. We need to output with error tolerance.
But wait: The problem says "print the average of those values." Since average is rational, we output floating.
Thus solution is trivial: read n,m,d; if d == 0: p = 1.0/n; else: p = 2.0*(n-d)/(1.0nn); ans = (m-1) * p; print with high precision.
But is there any catch? Let's double-check independence: The sequences are all n^m equally likely. For fixed i, (a_i, a_{i+1}) are uniformly distributed over all n^2 ordered pairs? Indeed, because the number of sequences with given values at positions i and i+1 is n^{m-2} (since other positions can be any). So probability = n^{m-2}/n^m = 1/n^2. So yes uniform. So expectation over independent uniform is valid. And linearity holds.
Thus answer is correct.
But there is a nuance: d could be larger than n-1? Constraint says 0 ≤ d < n. So d can be up to n-1. For d = n-1 > 0: Then number of ordered pairs with |x-y| = n-1: Only pairs (1,n) and (n,1). That's 2. So p = 2/n^2. Our formula 2*(n-d) = 2*(n-(n-1)) = 2*1=2, so correct.
Thus answer is simple.
Now need to output with absolute or relative error ≤ 1e-6. Use double precision.
Potential overflow: n up to 1e9, nn up to 1e18, which fits in 64-bit integer. But double can handle integers up to 2^53 exactly. 1e18 > 2^53 ≈ 9e15, so we lose integer precision if we compute nn as double? But we are computing p as a double, and final answer may be up to 1e9. The error tolerance is 1e-6, so we can compute using double but careful about rounding? Use long double for more precision? But standard double has 53-bit mantissa (~15-16 decimal digits). n=1e9 => n^2=1e18, which has 19 digits, but double can represent it exactly? Not all integers up to 2^53 exactly (~9e15). 1e18 > 9e15, so 1e18 cannot be represented exactly as a double; it will be rounded to the nearest representable. However, we are dividing by n^2, so the relative error in representing n^2 may be about 1e-18? Actually 1e18 in double: The spacing between representable numbers around 1e18 is 2^(exponent) * epsilon. Double's mantissa is 52 bits plus implicit 1, so precision: unit in the last place (ULP) = 2^(e-52). For numbers around 1e18, the exponent is floor(log2(1e18)) ≈ 59 (since 2^59 = 5.76e17, 2^60 = 1.15e18). So exponent 60 maybe? Let's compute: 1e18 ≈ 2^60 (1.15e18) actually 2^60=1.1529215e18, so exponent 60, mantissa scaled. Then ULP = 2^(60-52)=2^8=256. So the spacing is about 256. So 1e18 cannot be represented exactly; the nearest representable will be off by up to ~128. So relative error about 128/1e18 = 1.28e-16, which is very tiny. Then dividing and multiplying, overall error might be acceptable for 1e-6 tolerance. However, we need to be careful about (m-1) up to 1e9, product may have error ~1e-7? Possibly okay. But to be safe, we can compute using long double (80-bit extended precision on many platforms) or using rational arithmetic and then convert to double.
But typical solutions in contest for this problem (I recall AtCoder problem: "Average Length" or something? Actually this seems like AtCoder ABC something? I've seen a problem: "Beauty of a sequence" maybe AtCoder ABC 182? Not sure. However many solutions just use double.
But there is a nuance: For large n and m, the answer could be as small as ~ (m-1)2(n-d)/n^2. If n is large like 1e9, and m is up to 1e9, and d maybe 0, then p = 1e-9, product ~ 1, so not extremely small. If d is close to n, p is small: like 2/n^2 ~ 2e-18, times m-1 up to 1e9 yields ~2e-9, which is still > 1e-12. But error tolerance 1e-6 absolute or relative. For answer 2e-9, relative error 1e-6 means absolute error ~ 2e-15, which is extremely tight? Actually relative error 1e-6 for 2e-9 means absolute error 2e-15. That's small. But typical double precision may not guarantee that? However the problem likely expects using double and it's fine? Let's think: The computed value p = 2*(n-d)/n^2. For n=1e9, d close to n, say n-d=1, then p = 2e-18. Representing 2e-18 in double: It can represent numbers down to about 5e-324, but the relative precision is about 2^-52 ≈ 2.22e-16. So p = 2e-18 is much smaller than the machine epsilon relative to 1? Actually relative error for numbers near 2e-18 is also about 2.22e-16 relative? Wait double's relative precision is about 2e-16; that means that the spacing relative to the magnitude is about 2e-16. So the absolute error in representing p is about 2e-16 * p = 4e-34? Actually no: When you compute p via division, there will be rounding errors but relative error about machine epsilon. So p will be computed with relative error about 1e-16. Then multiplied by (m-1) ~1e9, relative error remains about 1e-16, so absolute error about 1e-16 * answer. If answer is 2e-9, absolute error ~2e-25, which is far below 2e-15. So it's fine.
But the problem is the representation of n^2 as double: 1e18 stored with absolute error ~100, relative error ~1e-16. Then (n-d) is integer up to 1e9, stored exactly as double. Division (2*(n-d))/(n*n) will have some rounding error, but relative error still about machine epsilon. So double should be fine. So I'll implement with double.
One more nuance: Input numbers up to 1e9, but m up to 1e9. (m-1) might overflow 32-bit, but double can hold it. However, for integer calculations, we may want to use 64-bit integers to compute numerator and denominator, then cast to double. For better accuracy, we can compute as:
if d == 0:
ans = (m-1) / n
else:
ans = (m-1) * 2 * (n-d) / (double)(n*n)
But note that (m-1)2(n-d) can be up to (1e9)2(1e9) = 2e18, which fits in 64-bit (since 2e18 < 9.22e18 of uint64). So we could compute numerator as (m-1)2(n-d) using 64-bit integer, then divide by (double) (nn). However we must be careful about overflow in integer multiplication if using 32-bit. Use long long (64-bit). Since n up to 1e9, nn = 1e18 fits in 64-bit (signed up to 9.22e18). (m-1) up to 1e9, (n-d) up to 1e9, product (m-1)2(n-d) up to 2e18, fits in signed 64-bit as well. So we can compute integer numerator and denominator as 64-bit integers, then cast to double and divide.
But be careful: For d=0, ans = (m-1)/n. That's straightforward.
Thus implement:
read n,m,d as long long.
if d == 0:
ans = (double)(m-1) / n;
else:
numerator = (m-1) * 2 * (n-d);
denominator = (double) n * n; // but we can compute as (double) denominator or use integer denominator.
ans = numerator / denominator;
But note that denominator as double may lose precision, but we can also compute as (double)numerator / n / n; that might be more accurate? Actually dividing by n twice might reduce error? Let's consider: (m-1)2(n-d) / (nn) as double. Converting numerator to double may cause rounding if numerator > 2^53 (~9e15). numerator up to 2e18, which is > 9e15, so numerator cannot be represented exactly as double. However dividing by nn (also approximate) yields result. But we can compute as (double)(m-1) * 2.0 * (n-d) / (double)(n) / (double)(n). This multiplies three doubles, each of which may be exact if the integer is <= 2^53? Actually n up to 1e9 (exact as double), (m-1) up to 1e9 (exact), (n-d) up to 1e9 (exact). So we can compute:
ans = (m-1) * 2.0 * (n-d) / n / n;
Because (m-1), (n-d) are integers <=1e9, converting to double gives exact representation (since they are <= 1e9 < 2^53). Then multiplication yields up to ~2e18, which may not be exactly representable. However division by n twice may produce same result. Let's compare two approaches:
Option A: Compute integer numerator as 64-bit, cast to double, divide by (double)(nn). numerator up to 2e18, not exactly representable as double; but double division yields a value that is the closest representable to the true rational. Option B: Compute as ((double)(m-1) * 2.0 * (n-d)) / ((double)n * (double)n). Here (double)(m-1), (n-d) are exact; product (double)(m-1) * 2.0 is exact? 2.0 is exactly representable. The multiplication (double)(m-1) * 2.0 = 2(m-1) exactly if m-1 <= 2^52? Actually 2*(m-1) up to 2e9, which is exact as double (since integer up to 2^53). Then multiply by (n-d) up to 1e9 gives up to 2e18; this multiplication may incur rounding error because the product may not be exactly representable. So both have similar error.
But we can compute as: ans = (m-1) * (d == 0 ? 1.0/n : 2.0*(n-d)/((double)n*n));
Better to compute using long double for extra safety. In C++, long double is often 80-bit on x86 (64-bit mantissa). That gives about 18-19 decimal digits of precision, which can represent 2e18 exactly (since 2^63 ~9.22e18, and 64-bit mantissa? Actually extended precision has 64 bits mantissa? On x86, long double uses 80 bits: 1 sign, 15 exponent, 64 mantissa (including explicit leading bit? Actually it's 64-bit mantissa with explicit integer bit? I'm not certain. Typically, long double on Intel has 64-bit mantissa (with leading bit explicit) giving 64 bits of precision, about 19-20 decimal digits. So 2e18 (~61 bits) can be represented exactly if it uses 64-bit mantissa? Let's check: 2e18 is about 2^61 (since 2^60=1.15e18). So 61 bits needed. With 64-bit mantissa, it can represent integers up to 2^64 exactly. So indeed 2e18 can be exact. So using long double would avoid rounding errors. But we can also compute as long double.
In many AtCoder problems, using double is enough, but we can use long double to be safe.
Now, check second example: n=1000000000, m=180707, d=0. (m-1)=180706, / n = 180706/1e9 = 0.000180706. That's exactly representable? 180706/1e9 = 0.000180706, which is 180706 * 1e-9. In floating point, it's a rational that may be not exact but close enough. Output matches example to 10 decimal places.
Thus solution is trivial.
But wait: There might be a twist: The average of beauties over all sequences of length m where each element is an integer between 1 and n. I'm confident.
Double-check with small values:
n=3,m=2,d=1. All 9 sequences: (1,1):0, (1,2):1, (1,3):0? |1-3|=2 !=1 so 0; (2,1):1, (2,2):0, (2,3):1, (3,1):0, (3,2):1, (3,3):0. Sum beauties=4, average=4/9≈0.44444. Formula: (m-1)=1, p=2*(n-d)/n^2=2*(3-1)/9=4/9≈0.44444. Good.
n=3,m=2,d=0: p=1/3≈0.33333, average=1/3. Sequences: (1,1):1, (2,2):1, (3,3):1, others 0 => 3 out of 9 => 1/3.
n=3,m=3,d=2: p=2*(3-2)/9=2/9≈0.22222, average=2*2/9=4/9≈0.44444. Could test a few but seems fine.
Now, note: d can be 0. Our formula works.
Thus answer.
But hold on: Problem statement: "0 ≤ d < n ≤ 10^9" and "2 ≤ m ≤ 10^9". So m can be up to 1e9, n up to 1e9. The answer may be large as m-1 close to 1e9 and p up to 1 (when n=2,d=1, p=0.5), so average up to 5e8. That fits in double.
Edge case: n=1. But constraints: n > d and d < n, and n ≤ 10^9, but is n allowed to be 1? The constraint says 0 ≤ d < n ≤ 10^9, and m ≥ 2. If n=1, then the only integer between 1 and n is 1. Then sequences only have 1's. For d=0, beauty = m-1 (since all adjacent pairs equal). For d>0, since d < n, but if n=1 then d can be 0 only because d < 1 implies d=0. So d must be 0. So we only have n=1,d=0. Then p = 1/n = 1/1 = 1. average = (m-1)*1 = m-1. That is fine. Our formula for d=0 gives 1/n = 1, works. For d>0, but d cannot be >0 if n=1 because d<n so d=0. So safe.
Now compute p for n=1,d=0: 1/1=1.
So algorithm:
Read n,m,d.
If d == 0:
ans = (m-1) / (double)n;
else:
ans = (m-1) * 2.0 * (n-d) / (double)n / (double)n; // or / ((double)n*n)
Print ans with e.g. printf("%.10lf\n", ans) or cout with setprecision.
But ensure using long double maybe. However, sample outputs show 10 decimal places, we can print 10 or more. I'll print with 10 digits after decimal using printf("%.10f\n", ans) with double. Or use cout << fixed << setprecision(10) << ans.
Second: "1000000000 180707 0" -> m-1=180706, / 1e9 = 0.000180706, output 0.0001807060 (with 10 decimal places: 0.0001807060). Let's compute: 180706/1e9 = 0.000180706 exactly? Actually 180706/1e9 = 1.80706e-4 = 0.000180706. So printing 10 digits: 0.0001807060 (10 digits after decimal: 0.0001807060 has 9 zeros? Let's count: after decimal: 0001807060? Actually 0.0001807060: digits: 0 . 0 0 0 1 8 0 7 0 6 0 => that's 10 digits after decimal? Count: 0 (1),0(2),0(3),1(4),8(5),0(6),7(7),0(8),6(9),0(10). So yes. So our output should match.
But careful: floating division may produce 0.00018070600000000001 or something. But with 10 decimal places it will be rounded to 0.0001807060. So it's fine.
Now I'll implement.
Time complexity O(1). Memory O(1).
But wait: The problem might be more subtle: What about sequences where elements are not independent? Actually we argued independence holds because each sequence equally likely, which yields uniform distribution over each coordinate and independence. But is there any dependence between a_i and a_{i+1}? In the uniform distribution over all sequences, they are independent. So fine.
Thus final answer is simple.
However, I recall a problem from AtCoder: "Beauty" maybe from ABC something? The problem statement "beauty of a sequence ... average of those values" is exactly AtCoder ABC 263 D? No, not sure. But anyway.
Now consider the possibility of using modular arithmetic? No, we need floating.
Thus solution: compute using long double for safety.
Implementation in C++:
#include
#include
using namespace std;
int main() {
long long n, m, d;
cin >> n >> m >> d;
double ans;
if (d == 0) {
ans = (double)(m-1) / n;
} else {
ans = (double)(m-1) * 2 * (n - d) / n / n;
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
But consider when n is large and d is small, double should be fine.
Now test some edge cases: n=2,m=2,d=1: average = (1)2(2-1)/4 = 2/4=0.5. Over all 4 sequences: (1,1):0, (1,2):1, (2,1):1, (2,2):0 => sum=2, average=0.5. Good.
n=1000000000,m=1000000000,d=999999999: p=2*(1)/1e18 = 2e-18, average = (1e9-1)*2e-18 ≈ 2e-9 - small. Use double should give around 2e-9. print with 10 decimal places: 0.0000000020? Actually 2e-9 is 0.000000002, so 10 decimal places would be 0.0000000020? That's fine.
Potential precision: (m-1)2(n-d) = (1e9-1)21 ≈ 2e9, dividing by n^2=1e18 yields 2e-9 exactly. That's representable as double? 2e-9 is 2*10^{-9}. In binary, not exact but floating point can represent many decimals? Actually 2e-9 = 2 * 2^{-?} Not exactly but with relative error 1e-16, absolute error ~2e-25, so printing 10 decimal places will show correct rounding.
Thus OK.
But the problem might be from AtCoder ABC 044 C? No. However I'll still implement.
Now, consider if we need to handle large numbers with high precision using long double? We'll just use double.
But I recall that in some languages (Python), they'd compute as fractions and print.
Since I'm coding in C++, using long double might be easier: long double ans = (m-1) * (d == 0 ? 1.0L : 2.0L*(n-d)/((long double)n*n)); and print with setprecision(10). But careful: For d==0, ans = (m-1)/(long double)n; But printing long double with cout may default to double? Actually cout << fixed << setprecision(10) << ans will convert long double to double? In C++, if ans is long double, and we pass to cout, it will be printed as long double? Actually operator<< is overloaded for long double. So it's fine.
Let me use long double for safety.
Implementation:
long double ans;
if (d == 0) ans = (long double)(m-1) / n;
else ans = (long double)(m-1) * 2 * (n-d) / n / n; // chain division.
Then output.
Alternatively, to avoid potential integer overflow when multiplying (m-1)2(n-d) before conversion, but we are converting to long double before multiplication? Actually we do (long double)(m-1) * 2 * (n-d) / n / n. The multiplication (long double)(m-1) * 2 is done, then * (n-d) etc. All values are within 1e9, exact as long double (since 1e9 < 2^53, but long double has higher precision). So it's fine.
But note: For d==0, ans = (m-1)/n; if m-1 and n are integers, conversion is fine.
Now we should output with high precision: setprecision(10) or maybe 12? The problem says error tolerance, not specific digits. Usually they print 10 decimal places in examples. So we'll print 10.
Thus final solution.
But wait: Check the constraints: "All values in input are integers." That's it.
Now, I'll draft the solution.
But before finalizing, think about potential pitfalls: Could the answer be computed as (m-1)*expected value per adjacent pair? Yes.
But could there be any correlation between adjacent pairs? For expectation, linearity holds regardless of correlation. So sum over i of E[I_i] = (m-1)*E[I]. And each I_i has same distribution as for any fixed pair. So indeed.
Now, verify with small m=2: Average beauty = 1 * P(|X-Y|=d) which matches our enumeration.
Thus the problem is trivial.
Now, I'll add code.
But recall: In AtCoder problems, they sometimes require to output with a certain number of digits, but here they printed 10 decimal digits in examples. So we'll print with 10 digits after decimal.
Alternatively, we could compute ans = (m-1) * p and print using printf("%.10f\n", (double)ans). Ensure cast to double.
Now, test with second sample:
n=1000000000, m=180707, d=0: m-1=180706, ans = 180706 / 1e9 = 0.000180706. Printing with 10 decimal digits: 0.0001807060. But if we compute ans = 180706.0 / 1e9 = 0.000180706, then printf("%.10f") will output "0.0001807060"? Let's simulate: 0.000180706 as double, with 10 digits after decimal: It will print 0.0001807060? Actually the rounding may yield 0.0001807060 if the value is exactly 0.000180706. But is 180706/1e9 exactly representable? 180706/1e9 = 180706 * 10^{-9}. 10^{-9} is not a power of 2, so not exact. Let's compute: 180706 = 0.000180706 exactly? In binary, it may be repeating. So the double representation is approximate. But when printing with 10 decimal digits, it will round to the nearest 10-digit decimal. The exact value is 180706/1000000000 = 0.000180706 exactly in decimal, because 180706/1e9 = 0.000180706 exactly? Actually 180706 / 1000000000 = 0.000180706? Let's compute: 1000000000 * 0.000180706 = 180706? 1000000000 * 0.000180706 = 180706 exactly? 0.000180706 * 1e9 = 180706.0, yes because 180706 / 1e9 = 0.000180706. That's a terminating decimal because denominator is 10^9, so it's exactly representable as a decimal with up to 9 digits? Actually 180706/10^9 = 0.000180706, which has 9 digits after decimal? Let's see: 0.000180706 has 9 digits after decimal? Count: 0 . 0 0 0 1 8 0 7 0 6 -> that's 9 digits? Actually 0.000180706: after decimal we have: 0 (1), 0 (2), 0 (3), 1 (4), 8 (5), 0 (6), 7 (7), 0 (8), 6 (9). So it's 9 digits. But printing 10 digits, we need to output one extra digit. In the example, they output "0.0001807060", which is 10 digits after decimal (including trailing zero). So the exact value is 0.000180706, and they added a trailing zero. So our program should output 0.0001807060. If the double representation is slightly off, rounding might produce 0.0001807060 or maybe 0.0001807059? We need to test. The double representation of 180706/1e9: Since 1e9 is 10^9 not a power of 2, but 180706/1e9 = 180706 * 10^{-9} = 180706 / (10^9). In binary, 10^9 = 1000000000, which is not a power of two. However, the division operation with double will produce the closest representable double to the true rational. The true value is 0.000180706 exactly as decimal. What is the binary representation? The double has 53 bits of precision, about 15-16 decimal digits. The decimal value 0.000180706 is representable exactly? Actually any rational with denominator a power of 10 may be exactly representable in binary only if denominator is a power of 2 after simplification. 0.000180706 = 180706 / 10^9 = simplify fraction? 180706 and 10^9 share factor 2? 180706 is even, 10^9=2^95^9? Actually 10^9=2^9 5^9. So 180706/10^9 = (180706 / 2^9) * 2^{-?} Let's check: 180706/512 = 353.332... not integer? Actually 180706 ÷ 2 = 90353, so it's divisible by 2 once, but not by 2^9? 2^9=512, 180706/512 is not integer because 180706 mod 512? Compute: 512353 = 180736, 512352 = 180224, remainder 482? So not integer. So the fraction does not have denominator a power of two; thus it cannot be represented exactly as a binary floating point. So the double will be an approximation.
But with 53-bit mantissa, the relative error is about 1e-16, so the absolute error in representing 1.80706e-4 is about 1e-20. When printing with 10 decimal digits after decimal, i.e., total 10 digits after decimal point, that is an absolute resolution of 1e-10. The error is many orders of magnitude smaller, so rounding to 10 decimal places should give the correct decimal digits. Specifically, the stored double is within ~1e-20 of the true value, so when rounding to 10 decimal places, the result will be the exact decimal representation (0.0001807060) because the true value is exactly 0.000180706 and the rounding threshold at the 11th digit is less than half of 1e-10? Actually, we need to ensure that the floating point value is not on a rounding boundary. The true value is exactly 0.000180706, which at 10 decimal places is 0.0001807060 (since the 10th digit is the digit after the 9th, we need to consider rounding to 10 decimal places). Usually, if we set precision to 10, the output will show 10 digits after decimal, rounding the actual stored value to the nearest 10-digit decimal. Since the stored value is extremely close to 0.000180706, it will round to 0.0001807060. So it's fine.
Thus double is safe.
Therefore, final solution.
But wait, the problem statement: "Print the average of the beauties of the sequences of length m where each element is an integer between 1 and n." Actually, "average of those values" meaning sum of beauties divided by number of sequences n^m. We computed expectation directly as (m-1)*P. That's correct.
Now, we should produce a solution.
But I recall a known AtCoder problem: "ABC 268 C"? No. Let's search memory: There is AtCoder "ABC 200 C" maybe? Not sure. But I've seen similar problem "Beauty" maybe in Codeforces? Anyway.
Now, is there any nuance about "beauty of a sequence" for d=0? They count pairs where absolute difference is d, which is 0. So pairs with equal adjacent numbers. So yes.
All good.
Now, I'll write final answer.
Implementation plan:
Read n, m, d as long long.
If d == 0: ans = (m-1) * 1.0 / n.
Else: ans = (m-1) * 2.0 * (n-d) / (n * 1.0) / n; // or divide by n*n.
Print ans with fixed and setprecision(10).
But to avoid integer overflow in n*n if we compute as integer before casting, we can do (double)n * (double)n. That's fine.
Let's implement.
One more check: For large n and d > 0, what if n-d is large, up to 1e9, product (m-1)2(n-d) up to about 2e18, which fits in 64-bit, but if we compute as integer in code: long long num = (m-1) * 2 * (n-d); this might overflow 64-bit? Let's compute maximum: m-1 <= 1e9, n-d <= 1e9, 2 constant. So product <= 2e18. 2e18 fits in signed 64-bit? Max signed 64-bit is 9.22e18, so it's fine. However, if we use int (32-bit) it will overflow, so use long long. So we could compute numerator as (m-1) * 2LL * (n-d) and then cast to double and divide by (double)(nn). But note nn can be up to 1e18, fits in 64-bit. So denominator can be computed as (long long) n * n. However nn could overflow 64-bit if n=1e9? 1e91e9=1e18 < 9.22e18, so safe.
But if we use long long on some platforms might be 64-bit. So it's fine.
But we can also compute directly using double and avoid 64-bit overflow by converting to double earlier. However double has 53-bit mantissa, but for 2e18 it's not exact; but as we argued, error is small. But it's simpler to compute using long double.
I'll compute using long double to minimize risk.
So:
long double ans;
if (d == 0) {
ans = (long double)(m-1) / n;
} else {
ans = (long double)(m-1) * 2.0L * (n - d) / n / n;
}
cout << fixed << setprecision(10) << (double)ans << endl; // could output as long double directly but setprecision works.
Better to output long double with cout << fixed << setprecision(10) << ans; That will print with 10 decimal digits. However, on some platforms, long double may have higher precision, but printing with setprecision(10) will round to 10 decimal places. It should be fine.
Alternatively, we can compute as double directly, since that is enough.
I'll use double for simplicity.
Now, write final code.
But wait: Could there be any case where (m-1) * 2 * (n-d) / (n*n) is not exactly representable? But it's fine.
Now, also note that m can be 1? But constraint says m >= 2, so okay.
Thus solution done.
Now I'll produce final answer.
But the problem statement: "All values in input are integers." and "Print the average ..." Done.
Thus final solution.
But before finishing, I want to double-check that the probability computation is indeed correct for the case where the two numbers are not independent? Actually they are independent as argued.
But one subtlety: In counting all sequences of length m, each sequence is equally likely. But does that imply that for a fixed pair of positions i and i+1, the two numbers are independent? Yes, because the marginal distribution: number of sequences with a_i = x and a_{i+1} = y is n^{m-2}, so the probability is 1/n^2, which is product of 1/n and 1/n. So they are independent. So expectation of indicator for that pair = 1/n^2 * count of (x,y) with |x-y|=d. So probability computed as count/n^2.
Thus correct.
So answer is as above.
Now final code in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, d;
cin >> n >> m >> d;
double ans;
if (d == 0) {
ans = (double)(m-1) / n;
} else {
ans = (double)(m-1) * 2.0 * (n - d) / n / n;
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
But we might consider using long double to ensure precision for large numbers. But I'll use double and it's okay.
But note: In the else branch, I'm dividing by n twice. That might be slightly more accurate? It's fine.
Example 1: n=2,m=3,d=1 => m-1=2, n-d=1 => ans = 221 / 2 / 2 = 4/4=1. Works.
Example 2: n=1e9, m=180707, d=0 => ans = 180706 / 1e9 = 0.000180706. Print 0.0001807060? My code using double and setprecision(10) will output 0.0001807060? Let's test quickly in mind: double value approximate, but printing with fixed and setprecision(10) will output exactly 10 digits after decimal. The actual stored double might be 0.00018070600000000001 or something. When rounding to 10 decimal places, it will see the 11th digit. If the error is small, the 11th digit may be something like 1e-11, which might cause rounding up the 10th digit? Possibly but unlikely because error is on order 1e-20, so the 11th digit will be 0 essentially. So it will print "0.0001807060". Good.
Now, I'll finalize.
But wait: The problem might be from AtCoder ABC 157 C? Not.
One more nuance: The sequence length m is given, but we used (m-1) pairs. That's correct.
Now final answer.
Thus solution is trivial.
I'll produce final answer with explanation.
But the problem might be "Beauty of a Permutation"? No, it's "Beauty of a Sequence" (maybe from AtCoder). Anyway.
Now, I'll write solution in proper format.
But there is a possibility that the problem originally required to compute sum of beauties over all sequences, not just average, but they ask average. So we computed expectation.
Now, I'll produce final output.
But before finishing, double-check if there is any tricky part: The integers are between 1 and n inclusive. So there are n values, each equally likely. Yes.
Now, confirm with some random tests via mental:
n=5,m=3,d=2. Compute p for d=2: 2*(5-2)/25 = 23/25=6/25=0.24. Average = 20.24=0.48. Let's brute force small: total sequences 125. Count number of adjacent pairs with diff=2. Compute all 125 sequences: For each first and second, and second and third. For each adjacent pair, probability 0.24. So expected count = 0.48. So total sum of beauties = 1250.48 = 60. So sum should be integer. 1250.48 = 60 exactly? 12548/100 = 6000/100=60. Good. Let's manually verify number of pairs: For each of the two adjacent positions, number of pairs with diff=2 is 2(n-d)=2*3=6 per pair, and there are n^{m-2}=5 other positions? Actually sum over sequences: For first pair, count = n^{m-2} * 6 = 5 * 6 = 30? Wait total sequences = n^m = 125. For a fixed position (e.g., positions 1-2), the number of sequences where |a1-a2|=2 is count of (a1,a2) pairs with diff=2 times number of ways for the rest (a3) = 6 * n^{m-2} = 6 * 5 = 30. Similarly for positions 2-3 also 30. So total sum of beauties = 30+30=60. Yes matches. So average = 60/125 = 0.48. So correct.
All good.
Thus final answer.
Now I'll produce final solution.
But note: Input could be large, so we should use long long.
Edge case: n can be up to 1e9, m up to 1e9, d up to n-1. Use long long.
Now implement.
I'll write:
#include
#include
using namespace std;
typedef long long ll;
int main() {
ll n, m, d;
cin >> n >> m >> d;
double ans;
if (d == 0) {
ans = (double)(m-1) / n;
} else {
ans = (double)(m-1) * 2 * (n - d) / ((double)n * n);
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
But we could also compute denominator as (double)n * (double)n.
Now, I'll produce final answer.
Given the simplicity, I think it's correct.
But wait, I recall a known problem: "Beauty of a sequence" appears in AtCoder "ABC 263 B" or something? Actually there is "Beauty" problem in AtCoder Beginner Contest 263 B? Let's check: B of ABC 263 is "Ancestor" maybe not. There is a problem "Average of Pairs"? No.
Alternatively, it could be from AtCoder "ABC 202 C" which is about counting pairs with difference? Not sure.
Anyway.
Now final answer: print the average with at least 10 decimal places.
Now I'll proceed to final.
```cpp
#include
#include
using namespace std;
int main() {
long long n, m, d;
cin >> n >> m >> d;
code
double ans;
if (d == 0) {
ans = (double)(m - 1) / n;
} else {
ans = (double)(m - 1) * 2.0 * (n - d) / n / n;
}
cout << fixed << setprecision(10) << ans << endl;
return 0;