Reaching Points

该博客讨论了一种数学问题,涉及如何通过特定操作将点(sx, sy)转换为点(tx, ty)。允许的操作包括将点(x, y)转换为(x, x+y)或(x+y, y)。文章提供了从终点开始逆向计算的解决方案,利用取模运算减少计算复杂性,并给出了满足条件的判断准则。示例展示了如何将(1, 1)转换为(3, 5)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point (sx, sy) to the point (tx, ty) through some operations, or false otherwise.

The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y).

Example 1:

Input: sx = 1, sy = 1, tx = 3, ty = 5
Output: true
Explanation:
One series of moves that transforms the starting point to the target is:
(1, 1) -> (1, 2)
(1, 2) -> (3, 2)
(3, 2) -> (3, 5)

思路:正着算,TLE,

(x,y)

(x, x+y) (x + y, y)

这个树形结构,永远是个二叉树,那么从终点往起点算,会很快,怎么算?

就是 tx < ty, ty = ty % tx.       tx > ty ,  tx = tx % ty;

这样算到最后一层了,只能有两种情况:

sx == tx && sy <= ty && (ty - sy) % sx == 0; 

sy == ty && sx <= tx && (tx - sx) % sy == 0; 可以找到root,否则就找不到;

class Solution {
    public boolean reachingPoints(int sx, int sy, int tx, int ty) {
        while(sx < tx && sy < ty) {
            if(tx < ty) {
                ty = ty % tx;
            } else {
                // tx > ty;
                tx = tx % ty;
            }
        }
        
        if(sx == tx && sy <= ty  && (ty - sy) % sx == 0) {
            return true;
        } 
        if(sy == ty && sx <= tx && (tx - sx) % sy == 0 ) {
            return true;
        }
        return false;   
    }
}

 

import numpy as np import matplotlib.pyplot as plt from scipy.integrate import cumulative_trapezoid # ==================== 参数设置 ==================== # 细菌生长参数 a, b, c = -0.0011, 0.0545, -0.0602 K = 3e10 N0 = 1 dt = 0.1 t_total = 36 # 压强模型参数 (调整后的值) k_EPS = 2.5e-6 # 增大EPS分泌速率 alpha = 2.5e3 # 增强EPS对水势的影响 beta = -3e5 gamma = 1000 P_crit = 2e5 # ==================== 函数定义 ==================== def temperature(t): x = t return (-8e-12 * x**6 + 7e-9 * x**5 - 2e-6 * x**4 + 0.0003 * x**3 - 0.0171 * x**2 + 0.2602 * x + 16.89) def humidity(t): x = t return (2e-11 * x**6 - 2e-8 * x**5 + 6e-6 * x**4 - 0.0008 * x**3 + 0.0374 * x**2 - 0.1759 * x + 74.181) def r_T(T): return a * T**2 + b * T + c def bacterial_growth(N, t): T = temperature(t) return r_T(T) * N * (1 - N / K) # ==================== 数值求解 ==================== time_points = np.arange(0, t_total + dt, dt) T_values = temperature(time_points) RH_values = humidity(time_points) # 细菌数量求解 N_values = np.zeros_like(time_points) N_values[0] = N0 for i in range(len(time_points) - 1): t_i = time_points[i] N_i = N_values[i] N_p = N_i + dt * bacterial_growth(N_i, t_i) N_c = N_i + 0.5 * dt * (bacterial_growth(N_i, t_i) + bacterial_growth(N_p, t_i + dt)) N_values[i + 1] = N_c # 压强计算 EPS_values = k_EPS * cumulative_trapezoid(N_values, time_points, initial=0) psi_bacteria = alpha * EPS_values + beta psi_leaf = gamma * (RH_values - 100) P_values = psi_bacteria - psi_leaf # 计算破裂时间 burst_idx = np.where(P_values >= P_crit)[0] t_burst = time_points[burst_idx[0]] if len(burst_idx) > 0 else None # ==================== 可视化 ==================== plt.figure(figsize=(12, 6)) # 压强曲线 plt.plot(time_points, P_values / 1e5, 'b-', label='Pressure (×10⁵ Pa)') plt.axhline(P_crit / 1e5, color='r', linestyle='--', label='Critical Pressure') # 标记破裂时间 if t_burst is not None: plt.axvline(t_burst, color='g', linestyle=':', linewidth=2, label=f'Burst Time: {t_burst:.1f} h') plt.scatter(t_burst, P_crit / 1e5, color='k', zorder=5) plt.xlabel('Time (hours)') plt.ylabel('Pressure (×10⁵ Pa)') plt.title('Necrotic Pressure Dynamics with Burst Time Threshold') plt.legend() plt.grid(True) plt.tight_layout() plt.show() # ==================== 结果输出 ==================== print(f"破裂时间 t_burst: {t_burst:.1f} 小时" if t_burst else "未达到临界压强") 帮我运行上述代码,显示出运行结果
最新发布
05-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值