We are given a problem: multiple datasets, each dataset: n (number of banks), y (number of years to deposit money), then n lines each containing: bank number bi, annual interest rate ri (integer 1-100), and type ti (1=simple interest, 2=compound interest). Need to compute for each bank the final amount after y years (principal + interest) starting with a principal of 1? Actually they didn't specify principal amount; typical in such problems, we compare based on a fixed initial deposit (e.g., 1). The output is the bank number with the highest final amount; it's guaranteed that only one bank has the highest. We need to produce output per dataset.
The problem statement includes some Japanese and images but the text says: "The calculation method of principal and interest is as follows. " The images likely show formulas for simple and compound interest. Since they are not provided, we infer typical formulas:
Simple interest: final amount = principal * (1 + (interest_rate/100) * years). Usually simple interest is calculated on original principal only. Interest is not added to principal. So after y years: A = P * (1 + (r/100)*y).
Compound interest: final amount = principal * (1 + (interest_rate/100))^years. Usually compounded annually.
But need to confirm: The problem says "annual interest rate (percentage)" and "interest rate type ti (1 or 2)". So yes.
But caution: The images might specify exact details: Possibly simple interest is calculated per year and added, but the resulting amount after y years is P*(1+ r/100 * y). Compound interest: P*(1+ r/100)^y.
Edge cases: y can be up to 30, r up to 100, so compound interest can be large but within double range. But we could also compute using logarithms to avoid overflow? Actually P=1, r=100, y=30 -> 2^30 ~ 1e9, fits in double. Even integer using long double or integer with scaling: but we can use double.
But careful: Because the bank number bi is given; could be not sequential? It might be an identifier. We need to output the bank number with the highest amount. Guarantee unique.
Also: There is mention "the number of datasets does not exceed 100."
Now example:
Input:
2
8
1 5 2
2 6 1
2
9
1 5 2
2 6 1
0
First dataset: n=2, y=8. Banks: 1: r=5, type=2 (compound). 2: r=6, type=1 (simple). Compute:
Bank1: amount = (1+0.05)^8 = 1.05^8 ≈ 1.477455... Actually compute: 1.05^2=1.1025; ^4=1.21550625; ^8=1.477455... maybe 1.477. Bank2: amount = 1+0.06*8=1+0.48=1.48. So bank2 gives 1.48 which is slightly higher than 1.477, so output 2. That matches first output 2.
Second dataset: n=2, y=9. Bank1 compound: 1.05^9 = 1.051.477455=1.551328. Bank2 simple: 1+0.069=1+0.54=1.54. So bank1 wins: output 1. That matches.
Thus our interpretation is correct.
Now we just need to parse multiple datasets until 0.
But note: Input format says: "The first line gives the number of banks n (1 ≤ n ≤ 50) and the second line gives the number of years to deposit money y (1 ≤ y ≤ 30)." So each dataset has n on its own line, then y on its own line, then n lines. Then next dataset starts with n, and so on. Terminated by 0. So we read integers.
Also note: In example, they have the datasets separated by blank lines? But actual input will be lines of numbers.
We need to handle multiple datasets.
Implementation: while True: read line for n. If n == 0: break. Then read y. Then loop n times reading three integers: bi, ri, ti. Compute amount for each bank. Keep track of max amount and the bank number (bi). Since guarantee unique max.
Compute amount: For simple: 1 + ri/100 * y. For compound: (1 + ri/100) ** y. Use float. But be careful about floating point precision: since comparing amounts and guarantee unique, we can safely use double. However if two amounts are extremely close due to rounding, might cause error. But with these numbers, it's unlikely. Or we could compute using rational numbers or decimal to be safe: For simple: 100 + riy (since denominator 100). Actually amount = (100 + riy) / 100 if principal=1? Wait: For simple interest: A = 1 + (ri/100)y = (100 + riy)/100. That is a rational number. For compound: A = ( (100+ri)/100 )^y = ( (100+ri)^y ) / (100^y). So we can compare by cross-multiplying to avoid floating errors. But since y up to 30 and ri up to 100, the numerator could be huge: (200)^30 is about 1.07e69, which is beyond 64-bit integer. But we can use Python's arbitrary precision integers easily. So we can compute exactly as rational numbers and compare via cross multiplication.
In Python, we can compute as fractions or just compute as floats, it's fine. The numbers are not extremely large; Python's float has 53 bits of precision, which is about 15 decimal digits. For compound amounts up to maybe (200/100)^30 = 2^30=1e9, the amount is at most around 2^30 = 1e9, or if r=100, (2)^30 = 1.07e9; if r=1, (1.01)^30 ≈ 1.347, so numbers range from ~1 to ~1e9. That's safe. But if r=100, y=30, it's 2^30 ~ 1e9; if r=100, y=30, the rational numerator is 200^30 which is huge but we don't need to compute it fully if using floats. Using float is fine: (1 + r/100) ** y is computed with double, error relative small. The guarantee unique max ensures no ties.
Thus we can just use float. But careful: if simple and compound amounts are very close, floating errors might cause inversion? In the example, for y=8, simple gave 1.48 exactly (since 0.06*8 = 0.48, plus 1 = 1.48), and compound gave about 1.477455, difference 0.002545, safe. For y=9, difference 0.011328. So fine.
But to be robust, we could compute exact using Python's integers: for simple, compute integer value: (100 + riy) (since denominator 100 common for all). For compound, compute integer value: (100+ri)y. But denominator is 100y. To compare two banks, cross-multiply: For bank A (amount = numA/denA) and bank B (numB/denB), compare numA * denB vs numB * denA. But denominators could be different (e.g., simple has denom 100, compound has denom 100^y). To avoid large numbers, we could compute logs or use decimals. But Python's integers can handle arbitrarily large numbers, so we can compute exactly. Let's see size: (100+ri)^y <= 200^30 ~ 2^30 * 100^30? Actually 200^30 = (2100)^30 = 2^30 * 100^30. That's huge: 100^30 = 10^60, so 2^3010^60 ~ 1e69, which Python can handle but might be a bit heavy for many datasets and up to 100 datasets and 50 banks each; still, Python's big integer multiplication of such huge numbers is okay? 10^69 is ~230 bits? Wait, 10^69 is about 2^230? Actually 2^10 ≈ 1024 ~ 10^3, so 2^230 ≈ 10^69. So 200^30 is about 2^30 * (10^2)^30 = 2^30 * 10^60 = ~1e91e60=1e69, which is about 2^230. That's not huge; 230 bits is small. Actually compute: 2^10 ≈ 1024 ~ 1e3; 2^230 = (2^10)^23 = 1024^23 ≈ (10^3)^23 = 10^69. So it's around 230 bits. That's tiny. Python's big integers can handle that easily. So we can compute exact values with integers.
Better: We can compute the amount as a fraction (numerator, denominator). For simple: numerator_s = 100 + ri*y, denominator_s = 100. For compound: numerator_c = (100+ri)y, denominator_c = 100y.
To compare two banks, we can compute A's numerator * B's denominator and B's numerator * A's denominator. Since denominators can be different, we can convert to common denominator perhaps 100**y? But we can just compute cross products per pair when tracking max. But we need to compare each bank to current max. We could compute a comparable value: For a given bank, we can compute a scaled amount to a common denominator. The easiest is to compute using Python's Fraction from fractions module, which does exact rational numbers. But Fraction uses simplification, and comparison will be exact. Could be okay. But we can also compute by using decimal with high precision. But using integer cross multiplication is easy.
Simplify: Since we only need to find max, we can compute using float, as it's safe. However, there could be edge cases where simple interest gives exactly a rational number and compound gives something like (1 + 5/100)^8, but float representation of compound might be slightly less or more than true value. But with numbers up to 30 exponent, double precision should be accurate enough to not misorder unless they are extremely close. But could they be arbitrarily close? Possibly if one interest rate is just slightly higher but the type makes them close? Let's consider worst-case: simple amount = 1 + ry/100, compound amount = (1+r/100)^y. They can be equal for some specific y? Possibly for y=1 they are equal. For y>1, they differ. The difference may be small if r is small and y is moderate? For example, r=1, y=30: simple = 1 + 0.0130 = 1.3; compound = 1.01^30 ≈ 1.3478, difference 0.0478. For r=2, y=2: simple=1.04, compound=1.02^2=1.0404, difference 0.0004. Actually r=2, y=2: simple = 1 + 0.022=1.04; compound = 1.02^2=1.0404; difference 0.0004. For r=1, y=2: simple=1.02, compound=1.0201, difference 0.0001. That's small but still far beyond double precision? Double precision has about 15 decimal digits. So 1.02 vs 1.0201 is difference 0.0001, which is accurately representable. Actually 1.02 and 1.0201 are both representable with some rounding but difference ~1e-4, so not an issue. For r=0.5? But r is integer 1-100, so smallest r=1. So difference at y=2: simple=1+0.012=1.02; compound=1.01^2=1.0201; diff=1e-4. That's fine. For y=3: simple=1.03, compound=1.01^3=1.030301, diff=0.000301; etc. For r=1, y=30 diff ~0.0478. So all differences are well above machine epsilon for numbers around 1. The worst-case relative difference might be for r=100, y=1: both same, but y>1 they differ. For r=100, y=2: simple=1+1*2=3, compound=2^2=4, diff=1. So not small. So float should be safe.
Nevertheless, we'll implement using exact integers to be absolutely correct and avoid any floating errors. It's straightforward.
Thus algorithm:
For each dataset:
- Read n.
- If n == 0: break.
- Read y.
- Initialize max_amount_numer = None, max_denom = None, max_bank = None.
- For i in range(n):
read bi, ri, ti.
if ti == 1: # simple
numer = 100 + ri * y # because amount = (100 + ri*y)/100
denom = 100
else: # ti == 2, compound
numer = (100 + ri) ** y
denom = 100 ** y
compare with current max
if max_bank is None:
max_bank = bi
max_numer = numer
max_denom = denom
else:
# compare current amount = numer/denom with max_amount = max_numer/max_denom
# compute cross multiplication: a = numer * max_denom; b = max_numer * denom
a = numer * max_denom
b = max_numer * denom
if a > b:
max_bank = bi
max_numer = numer
max_denom = denom
# if equal, according to problem guarantee there is only one highest, so we don't need to handle ties.
after loop, output max_bank
But careful: For simple interest, denominator is 100. For compound, denominator is 100^y. When cross multiplying, we multiply numbers that may become large: for compound vs simple: max_denom could be 100^y (if current max is compound) and denom could be 100 (simple). Then a = numer_simple * 100^y, b = numer_compound * 100. That's okay as numbers up to (100+riy) <= 100 + 10030 = 3100, times 100^30 which is 10^60, so about 3100*10^60 = 3.1e63, which is about 2^210? Actually 10^63 ~ 2^210. That's still manageable in Python. But maybe for performance, it's okay. However, if we compare two compound banks, then denominators are both 100^y, so cross multiplication reduces to comparing numerators directly. So it's fine.
We can also compute a common denominator, e.g., 100^y, by scaling simple interest numerator accordingly: simple amount scaled = (100+ri*y) * 100^(y-1) (since denominator 100^y). Then we compare integers. That might be simpler: Compute for each bank an integer value representing the amount times 100^y (i.e., scaled amount). Since y is same for all banks, we can use denominator = 100^y. Then for each bank:
- If simple: scaled_amount = (100 + riy) * (100**(y-1)) # because original amount = (100+riy)/100 = (100+ri*y)*100^{y-1} / 100^y.
- If compound: scaled_amount = (100+ri)**y.
Then all scaled amounts are integers. Then find max scaled_amount, output corresponding bank. This avoids cross multiplication each time. But careful: For simple interest, need to multiply by 100^{y-1}. That is a large number but okay. And we need to compute 100**y once. However, note that for simple interest, the expression (100+ri*y)*100^{y-1} might overflow? But Python big ints ok.
We could compute once: base = 100**y. Then for simple: scaled = (100+ri*y) * (base // 100). For compound: scaled = (100+ri)**y.
That's efficient.
But careful: If y=0? But y>=1. So base//100 is integer.
Thus plan: read n, y; compute base = 100**y; then for each bank compute:
if ti == 1:
scaled = (100 + ri * y) * (base // 100)
else:
scaled = (100 + ri) ** y
Then track max scaled, and corresponding bank number.
Note: Since base = 100**y, dividing by 100 yields 100^{y-1} exactly.
We must ensure that for simple, we treat the amount exactly: amount_simple = 1 + (ri/100)y = (100 + riy)/100. Multiply numerator and denominator to base: (100+riy)/100 = (100+riy)* (base/100) / base = scaled / base. So indeed scaled is integer.
Thus max scaled corresponds to max amount.
We need to guarantee that scaled values are comparable across banks. Yes.
So implement:
import sys
def solve():
import sys
data = sys.stdin.read().strip().split()
# We'll parse sequentially because it's simpler.
it = iter(data)
out_lines = []
while True:
try:
n = int(next(it))
except StopIteration:
break
if n == 0:
break
y = int(next(it))
base = 100 ** y
base_div100 = base // 100 # since y>=1
max_scaled = -1
best_bank = None
for _ in range(n):
bi = int(next(it))
ri = int(next(it))
ti = int(next(it))
if ti == 1:
scaled = (100 + ri * y) * base_div100
else: # ti == 2
scaled = (100 + ri) ** y
if scaled > max_scaled:
max_scaled = scaled
best_bank = bi
out_lines.append(str(best_bank))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()
Check with sample.
Given sample input:
2
8
1 5 2
2 6 1
2
9
1 5 2
2 6 1
0
First dataset: y=8, base=100^8, base_div100=100^7.
Bank1: compound: (100+5)8 = 1058.
Bank2: simple: (100+68)=100+48=148, scaled = 148 * 100^7.
Now compare 105**8 vs 148100^7. Compute actual numbers? 105^8 = 105^4^2? but we can compute relative amounts: 105^8 vs 148100^7. Dividing both by 100^7: Bank1: (105/100)^8 * 100? Actually 105^8 / 100^7 = 105^8 / 100^7 = 105^8 / 100^7 = (105/100)^7 * 105? Not needed. But earlier we saw bank2 slightly higher, so 148100^7 > 105^8. So best_bank=2. Good.
Second dataset: y=9, bank1: 105^9, bank2: (100+54)=154 * 100^8. 105^9 / 100^8 = 105*(1.05)^8? Actually 105^9 / 100^8 = 105^9 / 100^8 = (105/100)^8 * 105 = 1.05^8 * 105 ≈ 1.477455105=155.13, so scaled ≈155.13100^8? Wait careful: bank1 scaled = 105^9. bank2 scaled = 154 * 100^8. Divide both by 100^8: bank1 becomes 105^9 / 100^8 = 105 * (105/100)^8 = 105 * 1.05^8 ≈ 105*1.477455=155.132, bank2 becomes 154. So bank1 > bank2. So best_bank=1. Good.
Thus works.
Now check edge cases: n=1, then output that bank. y=30, r=100, ti=2, scaled = 200^30, fits big int.
Potential performance: For each dataset, computing 100**y, base//100, and for each compound bank computing (100+ri)y. y up to 30, base = 10030 = 10^60, okay. (100+ri)**y is up to 200^30, about 2^30 * 10^60 ~ 1e69, still okay. Python exponentiation of big ints with exponent 30 is fast. So fine.
But note: The number of datasets up to 100, banks up to 50, so at most 5000 such calculations. Fine.
Thus final answer is code.
Now produce final solution in Python.
But wait: The problem statement includes "Enter the number of banks, the number of years to deposit money, and the information of each bank (bank number, interest rate type, annual interest rate (percentage)), and create a program that outputs the bank number with the highest principal and interest. However, only one bank has the highest principal and interest." So we are done.
Now include the code with proper reading and writing. Also note that input might have blank lines. Using split handles that.
Thus final solution.
But I'm ChatGPT; need to respond with solution description and code. According to typical instructions: provide code with explanation.
Thus final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
it = iter(data)
out = []
while True:
try:
n = int(next(it))
except StopIteration:
break
if n == 0:
break
y = int(next(it))
# common denominator: 100^y
base = 100 ** y # = 100^y
base_div100 = base // 100 # = 100^(y-1)
best_bank = None
max_scaled = -1
for _ in range(n):
bi = int(next(it))
ri = int(next(it))
ti = int(next(it))
if ti == 1: # simple interest
# (100 + ri*y) / 100 -> scaled = (100 + ri*y) * 100^(y-1)
scaled = (100 + ri * y) * base_div100
else: # compound interest
# (100+ri)^y / 100^y -> scaled = (100+ri)^y
scaled = (100 + ri) ** y
if scaled > max_scaled:
max_scaled = scaled
best_bank = bi
out.append(str(best_bank))
sys.stdout.write("\n".join(out))
if name == "main":
solve()