0% found this document useful (0 votes)
16 views2 pages

メモ

The document presents a mathematical program that calculates a specific value based on combinations and conditions involving three variables: n, s, and t. It includes a Python implementation that defines functions for calculating combinations and determining the maximum value of a function based on the input parameters. The program prompts the user for input and validates it to ensure proper execution.

Uploaded by

sakkun3910n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

メモ

The document presents a mathematical program that calculates a specific value based on combinations and conditions involving three variables: n, s, and t. It includes a Python implementation that defines functions for calculating combinations and determining the maximum value of a function based on the input parameters. The program prompts the user for input and validates it to ensure proper execution.

Uploaded by

sakkun3910n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

{\color{white}P_{3}=\sum_{f=t}^{n-s}\frac{{}_{n-f-1}C_{s-1}}{{}_nC_s}\cdot\frac{t}

{f}}

{\color{white}\frac{s(t-1)}{n(n-1)}+\frac{n-s}{n}-\frac{{}_{n-t}C_s}{{}_nC_s}+\
sum_{f=t}^{n-s}\frac{{}_{n-f-1}C_{s-1}}{{}_nC_s}\cdot\frac{t}{f}(s<n-t+1)}

{\color{white}\frac{s(t-1)}{n(n-1)}+\frac{n-s}{n}+\sum_{f=t}^{n-s}\frac{{}_{n-f-
1}C_{s-1}}{{}_nC_s}\cdot\frac{t}{f}(n-t+1\le s\le n-1)}

プログラム

import math

def combination(n, r):


if r > n or r < 0:
return 0
return math.comb(n, r)

def calculate_value(n, s, t):


# Case 1: s < n - t + 1
if s < n - t + 1:
term1 = s * (t - 1) / (n * (n - 1))
term2 = (n - s) / n
term3 = -combination(n - t, s) / combination(n, s)
term4 = 0
for f in range(t, n - s + 1):
term4 += (combination(n - f - 1, s - 1) / combination(n, s)) * (t / f)
return term1 + term2 + term3 + term4

# Case 2: n - t + 1 <= s <= n - 1


elif n - t + 1 <= s <= n - 1:
term1 = s * (t - 1) / (n * (n - 1))
term2 = (n - s) / n
term3 = 0
for f in range(t, n - s + 1):
term3 += (combination(n - f - 1, s - 1) / combination(n, s)) * (t / f)
return term1 + term2 + term3

else:
return float('-inf') # Invalid case

def find_max_value(n, t):


max_value = float('-inf')
max_s = -1
for s in range(1, n):
value = calculate_value(n, s, t)
if value > max_value:
max_value = value
max_s = s
return max_value, max_s

# Input n and t from the user


try:
n = int(input('n を入力してください (正の整数): '))
t = int(input('t を入力してください (正の整数): '))

if n > 0 and t > 0 and n > t:


max_value, max_s = find_max_value(n, t)
print(f"The maximum value is {max_value} at s = {max_s}")
else:
print("Invalid input: Ensure that n, s, t are positive integers and n > s,
n > t.")
except ValueError:
print("Invalid input: Please enter valid positive integers for n and t.")

You might also like