<?php

if (empty($_GET['a']) || empty($_GET['b']) || empty($_GET['c'])) {
    $a = 1950;
    $b = 1625;
    $c = 1300;
} else {
    $a = $_GET['a'];
    $b = $_GET['b'];
    $c = $_GET['c'];
}

$input = 0;
$x = $y = $z = 0;
$sum = 0;
$difference = 0;
$calculation_time = 0;
$calculation_steps = 0;
$solution_found = false;

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input = isset($_POST['input_value']) ? (int)$_POST['input_value'] : 0;
    
    if ($input > 0) {
        $start_time = microtime(true);
        
        // 初始化最佳解
        $best_sum = 0;
        $best_x = $best_y = $best_z = 0;
        
        // 将面额从大到小排序
        $values = [
            ['value' => $a, 'var' => 'x'],
            ['value' => $b, 'var' => 'y'],
            ['value' => $c, 'var' => 'z']
        ];
        
        usort($values, function($x, $y) {
            return $y['value'] <=> $x['value'];
        });
        
        $val1 = $values[0]['value'];
        $val2 = $values[1]['value'];
        $val3 = $values[2]['value'];
        
        // 计算最大可能范围（按排序后的面额）
        $max1 = floor($input / $val1);
        $max2 = floor($input / $val2);
        $max3 = floor($input / $val3);
        
        // 设置步长
        $step1 = $max1 > 100 ? ceil($max1 / 50) : 1;
        $step2 = $max2 > 100 ? ceil($max2 / 50) : 1;
        $step3 = $max3 > 100 ? ceil($max3 / 50) : 1;
        
        // 三层循环（从大到小面额）
        for ($i = 0; $i <= $max1; $i += $step1) {
            $remaining1 = $input - $i * $val1;
            if ($remaining1 < 0) continue;
            
            for ($j = 0; $j <= $max2; $j += $step2) {
                $remaining2 = $remaining1 - $j * $val2;
                if ($remaining2 < 0) continue;
                
                for ($k = 0; $k <= $max3; $k += $step3) {
                    $current_sum = $i * $val1 + $j * $val2 + $k * $val3;
                    $calculation_steps++;
                    
                    if ($current_sum <= $input && $current_sum > $best_sum) {
                        $best_sum = $current_sum;
                        $best_i = $i;
                        $best_j = $j;
                        $best_k = $k;
                    }
                }
            }
        }
        
        // 精细搜索
        $refine_range = 5;
        $start_i = max(0, $best_i - $refine_range);
        $end_i = min($max1, $best_i + $refine_range);
        $start_j = max(0, $best_j - $refine_range);
        $end_j = min($max2, $best_j + $refine_range);
        $start_k = max(0, $best_k - $refine_range);
        $end_k = min($max3, $best_k + $refine_range);
        
        for ($i = $start_i; $i <= $end_i; $i++) {
            $remaining1 = $input - $i * $val1;
            if ($remaining1 < 0) continue;
            
            for ($j = $start_j; $j <= $end_j; $j++) {
                $remaining2 = $remaining1 - $j * $val2;
                if ($remaining2 < 0) continue;
                
                for ($k = $start_k; $k <= $end_k; $k++) {
                    $current_sum = $i * $val1 + $j * $val2 + $k * $val3;
                    $calculation_steps++;
                    
                    if ($current_sum <= $input && $current_sum > $best_sum) {
                        $best_sum = $current_sum;
                        $best_i = $i;
                        $best_j = $j;
                        $best_k = $k;
                    }
                }
            }
        }
        
        // 映射回原始变量
        $vars = [
            $values[0]['var'] => $best_i,
            $values[1]['var'] => $best_j,
            $values[2]['var'] => $best_k
        ];
        
        $x = $vars['x'];
        $y = $vars['y'];
        $z = $vars['z'];
        
        $end_time = microtime(true);
        $calculation_time = round(($end_time - $start_time) * 1000, 2);
        
        if ($best_sum > 0) {
            $solution_found = true;
            $sum = $best_sum;
            $difference = $input - $sum;
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组合优化计算器</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#4f46e5',
                        secondary: '#818cf8',
                        dark: '#1e293b'
                    }
                }
            }
        }
    </script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #f0f9ff 0%, #e6f7ff 100%);
        }
        .card {
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
            transition: all 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .input-field:focus {
            box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.3);
        }
        .result-item {
            transition: transform 0.3s ease, background-color 0.3s ease;
        }
        .result-item:hover {
            transform: scale(1.02);
            background-color: #f8fafc;
        }
        .glow {
            animation: glow 2s infinite alternate;
        }
        @keyframes glow {
            from {
                box-shadow: 0 0 5px -5px #4f46e5;
            }
            to {
                box-shadow: 0 0 20px 0px #4f46e5;
            }
        }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center p-4">
    <div class="w-full max-w-4xl">
        <div class="text-center mb-10">
            <h1 class="text-4xl md:text-5xl font-bold text-gray-800 mb-3">玉山县2024年配网计算器</h1>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
            <!-- 输入区域 -->
            <div class="card bg-white rounded-2xl p-6">
                <div class="flex items-center mb-6">
                    <div class="bg-primary p-3 rounded-lg mr-4">
                        <i class="fas fa-calculator text-white text-2xl"></i>
                    </div>
                    <h2 class="text-2xl font-bold text-gray-800">计算参数设置</h2>
                </div>
                
                <form method="post" class="space-y-6">
                    <div>
                        <label class="block text-gray-700 font-medium mb-2">输入值</label>
                        <div class="relative">
                            <input type="number" name="input_value" value="<?= $input ?>" 
                                   class="input-field w-full px-4 py-3 rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent transition"
                                   placeholder="请输入正整数" min="0" required>
                            <div class="absolute inset-y-0 right-0 flex items-center pr-3">
                                <i class="fas fa-keyboard text-gray-400"></i>
                            </div>
                        </div>
                        <p class="mt-2 text-sm text-gray-500">请输入要计算的目标值</p>
                    </div>
                    
                    <div class="pt-4">
                        <button type="submit" class="w-full bg-primary hover:bg-secondary text-white font-bold py-3 px-4 rounded-lg transition duration-300 transform hover:scale-[1.02] flex items-center justify-center">
                            <i class="fas fa-bolt mr-2"></i> 计算最优组合
                        </button>
                    </div>
                </form>
                
                <div class="mt-8 p-4 bg-blue-50 rounded-lg border border-blue-200">
                    <h3 class="font-semibold text-blue-800 flex items-center">
                        <i class="fas fa-info-circle mr-2"></i> 计算说明
                    </h3>
                    <ul class="mt-2 text-blue-700 text-sm space-y-1">
                        <li>• 算法将寻找满足 <?php echo htmlspecialchars($a); ?>*X + <?php echo htmlspecialchars($b); ?>*Y + <?php echo htmlspecialchars($c); ?>*Z ≤ 输入值 的组合</li>
                        <li>• 目标是最小化输入值与计算值之间的偏差</li>
                        <li>• X, Y, Z 均为非负整数</li>
                        <li>• 输入值越大，计算时间越长</li>
                    </ul>
                </div>
            </div>
            
            <!-- 结果区域 -->
            <div class="card bg-white rounded-2xl p-6">
                <div class="flex items-center mb-6">
                    <div class="bg-green-500 p-3 rounded-lg mr-4">
                        <i class="fas fa-chart-line text-white text-2xl"></i>
                    </div>
                    <h2 class="text-2xl font-bold text-gray-800">计算结果</h2>
                </div>
                
                <?php if ($solution_found): ?>
                <div class="space-y-4">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div class="result-item bg-gray-50 p-4 rounded-lg border border-gray-200">
                            <div class="text-gray-600">输入值</div>
                            <div class="text-2xl font-bold text-primary"><?= number_format($input) ?></div>
                        </div>
                        <div class="result-item bg-gray-50 p-4 rounded-lg border border-gray-200">
                            <div class="text-gray-600">计算偏差</div>
                            <div class="text-2xl font-bold <?= $difference == 0 ? 'text-green-600' : 'text-orange-600' ?>">
                                <?= number_format($difference) ?>
                                <?php if ($difference == 0): ?>
                                    <span class="text-sm">(完美匹配)</span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    
                    <div class="result-item bg-gradient-to-r from-blue-50 to-indigo-50 p-4 rounded-lg border border-blue-200 glow">
                        <div class="text-gray-600">计算总和</div>
                        <div class="text-3xl font-bold text-primary"><?= number_format($sum) ?></div>
                        <div class="mt-1 text-gray-700">
                            = 1950 × <?= $x ?> + 1625 × <?= $y ?> + 1300 × <?= $z ?>
                        </div>
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mt-4">
                        <div class="result-item bg-indigo-50 p-4 rounded-lg border border-indigo-200">
                            <div class="text-gray-700">X 值</div>
                            <div class="text-2xl font-bold text-indigo-700"><?= $x ?></div>
                            <div class="text-sm text-gray-600">1950 × <?= $x ?> = <?= number_format(1950 * $x) ?></div>
                        </div>
                        <div class="result-item bg-purple-50 p-4 rounded-lg border border-purple-200">
                            <div class="text-gray-700">Y 值</div>
                            <div class="text-2xl font-bold text-purple-700"><?= $y ?></div>
                            <div class="text-sm text-gray-600">1625 × <?= $y ?> = <?= number_format(1625 * $y) ?></div>
                        </div>
                        <div class="result-item bg-pink-50 p-4 rounded-lg border border-pink-200">
                            <div class="text-gray-700">Z 值</div>
                            <div class="text-2xl font-bold text-pink-700"><?= $z ?></div>
                            <div class="text-sm text-gray-600">1300 × <?= $z ?> = <?= number_format(1300 * $z) ?></div>
                        </div>
                    </div>
                    
                    <div class="mt-6 p-4 bg-gray-50 rounded-lg">
                        <div class="flex justify-between text-sm text-gray-600">
                            <span>计算时间:</span>
                            <span class="font-medium"><?= $calculation_time ?> 毫秒</span>
                        </div>
                        <div class="flex justify-between text-sm text-gray-600 mt-1">
                            <span>计算步骤:</span>
                            <span class="font-medium"><?= number_format($calculation_steps) ?> 步</span>
                        </div>
                    </div>
                </div>
                
                <?php elseif ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
                <div class="text-center py-10">
                    <div class="inline-block p-4 bg-yellow-100 rounded-full mb-4">
                        <i class="fas fa-exclamation-triangle text-yellow-600 text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800">未找到有效解</h3>
                    <p class="text-gray-600 mt-2">请输入大于0的整数</p>
                </div>
                
                <?php else: ?>
                <div class="text-center py-10">
                    <div class="inline-block p-4 bg-gray-100 rounded-full mb-4">
                        <i class="fas fa-calculator text-gray-500 text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800">等待计算</h3>
                    <p class="text-gray-600 mt-2">输入数值后点击"计算最优组合"按钮</p>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <div class="mt-10 text-center text-gray-500 text-sm">
            <p>© 2023 组合优化计算器 | 基于 PHP 算法实现</p>
        </div>
    </div>
    <div class="toast-container"></div>
</body>
</html>