<?php
/**
 * 上期正码与当期特码关联规律分析脚本
 *
 * 分析维度：
 * 1. 上期正码平均值与下期特码的差值分布
 * 2. 上期正码范围[min,max]与下期特码的关系
 * 3. 上期正码与下期特码的最短距离分布
 * 4. 上期正码和值尾数与下期特码尾数的关系
 * 5. 上期正码覆盖区间与下期特码所在区间的关系
 * 6. 上期正码波色分布与下期特码波色的关系
 * 7. 上期特码与下期特码的转移关系
 */

// 波色映射表
$colorMap = [
    1 => '红', 2 => '红', 3 => '蓝', 4 => '蓝', 5 => '绿', 6 => '绿',
    7 => '红', 8 => '红', 9 => '蓝', 10 => '蓝', 11 => '绿', 12 => '红',
    13 => '红', 14 => '蓝', 15 => '蓝', 16 => '绿', 17 => '绿', 18 => '红',
    19 => '红', 20 => '蓝', 21 => '绿', 22 => '绿', 23 => '红', 24 => '红',
    25 => '蓝', 26 => '蓝', 27 => '绿', 28 => '绿', 29 => '红', 30 => '红',
    31 => '蓝', 32 => '绿', 33 => '绿', 34 => '红', 35 => '红', 36 => '蓝',
    37 => '蓝', 38 => '绿', 39 => '绿', 40 => '红', 41 => '蓝', 42 => '蓝',
    43 => '绿', 44 => '绿', 45 => '红', 46 => '红', 47 => '蓝', 48 => '蓝',
    49 => '绿'
];

// 区间划分（1-10小号，11-30中号，31-49大号）
function getRange($num) {
    if ($num >= 1 && $num <= 10) return '小号(1-10)';
    if ($num >= 11 && $num <= 30) return '中号(11-30)';
    return '大号(31-49)';
}

// 获取数字的尾数
function getTail($num) {
    return $num % 10;
}

// 从SQL数据提取历史记录
$history = [];
$sqlFile = 'C:\Users\91611\Desktop\fa_history.sql';
$content = file_get_contents($sqlFile);

// 解析INSERT语句
preg_match_all("/INSERT INTO `fa_history` VALUES \((\d+), (\d+), (\d+), (\d+), (\d+), (\d+), (\d+), (\d+), '([^']+)'\)/", $content, $matches);

for ($i = 0; $i < count($matches[0]); $i++) {
    $history[] = [
        'expect' => (int)$matches[1][$i],
        'num1' => (int)$matches[2][$i],
        'num2' => (int)$matches[3][$i],
        'num3' => (int)$matches[4][$i],
        'num4' => (int)$matches[5][$i],
        'num5' => (int)$matches[6][$i],
        'num6' => (int)$matches[7][$i],
        'num7' => (int)$matches[8][$i],
        'openTime' => $matches[9][$i]
    ];
}

// 按期号排序
usort($history, function($a, $b) {
    return $a['expect'] - $b['expect'];
});

echo "=== 数据概览 ===\n";
echo "总期数: " . count($history) . "\n";
echo "期号范围: {$history[0]['expect']} - {$history[count($history)-1]['expect']}\n\n";

// 分析结果存储
$analysisResults = [];

// ============ 维度1: 上期正码平均值与下期特码的差值分布 ============
echo "=== 维度1: 上期正码平均值与下期特码的差值分布 ===\n";

$avgDifferences = [];
$avgDiffPredictions = []; // 基于平均值的预测命中率

for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    // 计算上期正码平均值
    $avg = ($current['num1'] + $current['num2'] + $current['num3'] +
            $current['num4'] + $current['num5'] + $current['num6']) / 6;

    // 与下期特码的差值
    $diff = $next['num7'] - $avg;
    $avgDifferences[] = round($diff);
}

// 统计差值分布
$avgDiffStats = array_count_values($avgDifferences);
ksort($avgDiffStats);

echo "差值分布（差值=下期特码-上期正码平均值）:\n";
foreach ($avgDiffStats as $diff => $count) {
    $percent = round($count / (count($history) - 1) * 100, 2);
    echo "  差值 $diff: $count次 ($percent%)\n";
}

// 分析差值范围预测效果
echo "\n差值范围预测效果:\n";
$diffRanges = [
    '[-40,-20]' => 0,
    '[-20,-10]' => 0,
    '[-10,0]' => 0,
    '[0,10]' => 0,
    '[10,20]' => 0,
    '[20,40]' => 0
];

foreach ($avgDifferences as $diff) {
    if ($diff >= -40 && $diff < -20) $diffRanges['[-40,-20]']++;
    elseif ($diff >= -20 && $diff < -10) $diffRanges['[-20,-10]']++;
    elseif ($diff >= -10 && $diff < 0) $diffRanges['[-10,0]']++;
    elseif ($diff >= 0 && $diff < 10) $diffRanges['[0,10]']++;
    elseif ($diff >= 10 && $diff < 20) $diffRanges['[10,20]']++;
    elseif ($diff >= 20 && $diff <= 40) $diffRanges['[20,40]']++;
}

$total = count($avgDifferences);
foreach ($diffRanges as $range => $count) {
    $percent = round($count / $total * 100, 2);
    echo "  $range: $count次 ($percent%)\n";
}

// 基于平均值±10范围的预测命中率分析
$hitCount = 0;
$predictionRange = 10; // 预测范围
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $avg = ($current['num1'] + $current['num2'] + $current['num3'] +
            $current['num4'] + $current['num5'] + $current['num6']) / 6;

    // 预测范围：平均值±10
    $predictMin = max(1, floor($avg) - $predictionRange);
    $predictMax = min(49, floor($avg) + $predictionRange);

    // 检查下期特码是否在预测范围内
    if ($next['num7'] >= $predictMin && $next['num7'] <= $predictMax) {
        $hitCount++;
    }
}
echo "\n基于平均值±$predictionRange范围的预测命中率: " . round($hitCount / (count($history) - 1) * 100, 2) . "% ($hitCount/" . (count($history)-1) . ")\n";

echo "\n";

// ============ 维度2: 上期正码范围[min,max]与下期特码的关系 ============
echo "=== 维度2: 上期正码范围[min,max]与下期特码的关系 ===\n";

$inRangeCount = 0;
$belowRangeCount = 0;
$aboveRangeCount = 0;
$rangeStats = [];

for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    // 上期正码范围
    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];
    $min = min($nums);
    $max = max($nums);
    $rangeWidth = $max - $min;

    // 下期特码与范围的关系
    if ($next['num7'] >= $min && $next['num7'] <= $max) {
        $inRangeCount++;
        $relation = '在范围内';
    } elseif ($next['num7'] < $min) {
        $belowRangeCount++;
        $relation = '低于范围';
    } else {
        $aboveRangeCount++;
        $relation = '高于范围';
    }

    // 统计范围宽度与关系
    $rangeKey = "范围宽度$rangeWidth";
    if (!isset($rangeStats[$rangeKey])) {
        $rangeStats[$rangeKey] = ['在范围内' => 0, '低于范围' => 0, '高于范围' => 0];
    }
    $rangeStats[$rangeKey][$relation]++;
}

$total = count($history) - 1;
echo "下期特码位置分布:\n";
echo "  在上期正码范围内: $inRangeCount次 (" . round($inRangeCount / $total * 100, 2) . "%)\n";
echo "  低于上期正码范围: $belowRangeCount次 (" . round($belowRangeCount / $total * 100, 2) . "%)\n";
echo "  高于上期正码范围: $aboveRangeCount次 (" . round($aboveRangeCount / $total * 100, 2) . "%)\n";

echo "\n范围宽度与特码位置关系:\n";
ksort($rangeStats);
foreach ($rangeStats as $width => $stats) {
    $widthTotal = array_sum($stats);
    echo "  $width (共$widthTotal期):\n";
    foreach ($stats as $relation => $count) {
        $percent = round($count / $widthTotal * 100, 2);
        echo "    $relation: $count次 ($percent%)\n";
    }
}

// 计算范围宽度的平均值
$avgRangeWidth = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];
    $avgRangeWidth += max($nums) - min($nums);
}
$avgRangeWidth = round($avgRangeWidth / (count($history) - 1), 2);
echo "\n平均范围宽度: $avgRangeWidth\n";

echo "\n";

// ============ 维度3: 上期正码与下期特码的最短距离分布 ============
echo "=== 维度3: 上期正码与下期特码的最短距离分布 ===\n";

$minDistances = [];
$distanceStats = [];

for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    // 计算上期正码与下期特码的最短距离
    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];

    $minDist = PHP_INT_MAX;
    foreach ($nums as $num) {
        $dist = abs($next['num7'] - $num);
        if ($dist < $minDist) $minDist = $dist;
    }
    $minDistances[] = $minDist;

    if (!isset($distanceStats[$minDist])) {
        $distanceStats[$minDist] = 0;
    }
    $distanceStats[$minDist]++;
}

ksort($distanceStats);
echo "最短距离分布:\n";
foreach ($distanceStats as $dist => $count) {
    $percent = round($count / (count($history) - 1) * 100, 2);
    echo "  距离 $dist: $count次 ($percent%)\n";
}

// 分析距离≤5的命中率
$closeHitCount = count(array_filter($minDistances, function($d) { return $d <= 5; }));
echo "\n最短距离≤5的比例: " . round($closeHitCount / (count($history) - 1) * 100, 2) . "% ($closeHitCount/" . (count($history)-1) . ")\n";

$veryCloseHitCount = count(array_filter($minDistances, function($d) { return $d <= 3; }));
echo "最短距离≤3的比例: " . round($veryCloseHitCount / (count($history) - 1) * 100, 2) . "% ($veryCloseHitCount/" . (count($history)-1) . ")\n";

// 基于最短距离预测（围绕上期正码±3的范围）
$predictionHit = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];

    // 预测范围：每个正码±3
    $predicted = [];
    foreach ($nums as $num) {
        for ($p = max(1, $num - 3); $p <= min(49, $num + 3); $p++) {
            $predicted[$p] = true;
        }
    }

    if (isset($predicted[$next['num7']])) {
        $predictionHit++;
    }
}
echo "基于正码±3范围预测命中率: " . round($predictionHit / (count($history) - 1) * 100, 2) . "% ($predictionHit/" . (count($history)-1) . ")\n";
echo "预测范围大小: 约" . count($predicted) . "个数字\n";

echo "\n";

// ============ 维度4: 上期正码和值尾数与下期特码尾数的关系 ============
echo "=== 维度4: 上期正码和值尾数与下期特码尾数的关系 ===\n";

$sumTailRelations = [];
$tailSameCount = 0;
$tailDiff1Count = 0;
$tailDiff2Count = 0;

for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    // 上期正码和值
    $sum = $current['num1'] + $current['num2'] + $current['num3'] +
           $current['num4'] + $current['num5'] + $current['num6'];
    $sumTail = getTail($sum);

    // 下期特码尾数
    $nextTail = getTail($next['num7']);

    // 统计尾数关系
    $tailDiff = abs($sumTail - $nextTail);
    if ($tailDiff > 5) $tailDiff = 10 - $tailDiff; // 考虑环形差异

    if (!isset($sumTailRelations[$sumTail])) {
        $sumTailRelations[$sumTail] = [];
    }
    if (!isset($sumTailRelations[$sumTail][$nextTail])) {
        $sumTailRelations[$sumTail][$nextTail] = 0;
    }
    $sumTailRelations[$sumTail][$nextTail]++;

    if ($tailDiff == 0) $tailSameCount++;
    elseif ($tailDiff == 1) $tailDiff1Count++;
    elseif ($tailDiff == 2) $tailDiff2Count++;
}

$total = count($history) - 1;
echo "尾数关系分布:\n";
echo "  尾数相同: $tailSameCount次 (" . round($tailSameCount / $total * 100, 2) . "%)\n";
echo "  尾数相差1: $tailDiff1Count次 (" . round($tailDiff1Count / $total * 100, 2) . "%)\n";
echo "  尾数相差2: $tailDiff2Count次 (" . round($tailDiff2Count / $total * 100, 2) . "%)\n";

echo "\n上期和值尾数→下期特码尾数转移矩阵:\n";
for ($sumTail = 0; $sumTail <= 9; $sumTail++) {
    echo "  和值尾数$sumTail → ";
    if (isset($sumTailRelations[$sumTail])) {
        $maxTail = -1;
        $maxCount = 0;
        foreach ($sumTailRelations[$sumTail] as $nextTail => $count) {
            if ($count > $maxCount) {
                $maxCount = $count;
                $maxTail = $nextTail;
            }
        }
        $totalCount = array_sum($sumTailRelations[$sumTail]);
        $percent = round($maxCount / $totalCount * 100, 2);
        echo "最可能尾数$maxTail ($maxCount次, $percent%), 其他: ";
        $others = [];
        foreach ($sumTailRelations[$sumTail] as $nextTail => $count) {
            if ($nextTail != $maxTail) {
                $others[] = "$nextTail($count)";
            }
        }
        echo implode(', ', $others) . "\n";
    } else {
        echo "无数据\n";
    }
}

// 基于尾数预测命中率
$tailPredictionHit = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $sum = $current['num1'] + $current['num2'] + $current['num3'] +
           $current['num4'] + $current['num5'] + $current['num6'];
    $sumTail = getTail($sum);

    // 预测：和值尾数±2范围内的尾数
    $predictTails = [
        $sumTail,
        ($sumTail + 1) % 10,
        ($sumTail - 1 + 10) % 10,
        ($sumTail + 2) % 10,
        ($sumTail - 2 + 10) % 10
    ];

    $nextTail = getTail($next['num7']);
    if (in_array($nextTail, $predictTails)) {
        $tailPredictionHit++;
    }
}
echo "\n基于和值尾数±2范围的尾数预测命中率: " . round($tailPredictionHit / (count($history) - 1) * 100, 2) . "% ($tailPredictionHit/" . (count($history)-1) . ")\n";
echo "预测范围: 5个尾数，每个尾数对应约5个数字，共约25个数字\n";

echo "\n";

// ============ 维度5: 上期正码覆盖区间与下期特码所在区间的关系 ============
echo "=== 维度5: 上期正码覆盖区间与下期特码所在区间的关系 ===\n";

$rangeCoverStats = [];
$rangeTransferStats = [];

for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];

    // 上期正码覆盖的区间
    $covers = [];
    foreach ($nums as $num) {
        $covers[getRange($num)] = true;
    }
    $coverStr = implode('+', array_keys($covers));

    // 下期特码所在区间
    $nextRange = getRange($next['num7']);

    if (!isset($rangeCoverStats[$coverStr])) {
        $rangeCoverStats[$coverStr] = [];
    }
    if (!isset($rangeCoverStats[$coverStr][$nextRange])) {
        $rangeCoverStats[$coverStr][$nextRange] = 0;
    }
    $rangeCoverStats[$coverStr][$nextRange]++;

    // 统计覆盖度与特码位置
    $coverCount = count($covers);
    if (!isset($rangeTransferStats[$coverCount])) {
        $rangeTransferStats[$coverCount] = [
            '小号(1-10)' => 0,
            '中号(11-30)' => 0,
            '大号(31-49)' => 0
        ];
    }
    $rangeTransferStats[$coverCount][$nextRange]++;
}

echo "上期正码覆盖区间→下期特码区间转移:\n";
foreach ($rangeCoverStats as $cover => $nextStats) {
    $totalCover = array_sum($nextStats);
    echo "  $cover (共$totalCover期):\n";
    foreach ($nextStats as $nextRange => $count) {
        $percent = round($count / $totalCover * 100, 2);
        echo "    → $nextRange: $count次 ($percent%)\n";
    }
}

echo "\n上期正码覆盖区间数量与下期特码分布:\n";
foreach ($rangeTransferStats as $coverCount => $stats) {
    $totalCover = array_sum($stats);
    echo "  覆盖$coverCount个区间 (共$totalCover期):\n";
    foreach ($stats as $range => $count) {
        $percent = round($count / $totalCover * 100, 2);
        echo "    $range: $count次 ($percent%)\n";
    }
}

// 分析特码是否在上期正码覆盖的区间内
$hitInCoveredRange = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];

    $covers = [];
    foreach ($nums as $num) {
        $covers[getRange($num)] = true;
    }

    $nextRange = getRange($next['num7']);
    if (isset($covers[$nextRange])) {
        $hitInCoveredRange++;
    }
}
echo "\n下期特码在上期正码覆盖区间内的比例: " . round($hitInCoveredRange / (count($history) - 1) * 100, 2) . "% ($hitInCoveredRange/" . (count($history)-1) . ")\n";

echo "\n";

// ============ 维度6: 上期正码波色分布与下期特码波色的关系 ============
echo "=== 维度6: 上期正码波色分布与下期特码波色的关系 ===\n";

$colorDistributionStats = [];
$colorTransferStats = [];
$dominantColorStats = [];

for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    // 上期正码波色分布
    $colors = [];
    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];
    foreach ($nums as $num) {
        $color = $colorMap[$num];
        if (!isset($colors[$color])) {
            $colors[$color] = 0;
        }
        $colors[$color]++;
    }

    // 波色分布字符串
    $colorStr = "红{$colors['红']}蓝{$colors['蓝']}绿{$colors['绿']}";

    // 下期特码波色
    $nextColor = $colorMap[$next['num7']];

    // 统计波色分布→特码波色
    if (!isset($colorDistributionStats[$colorStr])) {
        $colorDistributionStats[$colorStr] = [];
    }
    if (!isset($colorDistributionStats[$colorStr][$nextColor])) {
        $colorDistributionStats[$colorStr][$nextColor] = 0;
    }
    $colorDistributionStats[$colorStr][$nextColor]++;

    // 统计主导波色→特码波色
    $dominantColor = array_keys($colors, max($colors))[0];
    if (!isset($dominantColorStats[$dominantColor])) {
        $dominantColorStats[$dominantColor] = [];
    }
    if (!isset($dominantColorStats[$dominantColor][$nextColor])) {
        $dominantColorStats[$dominantColor][$nextColor] = 0;
    }
    $dominantColorStats[$dominantColor][$nextColor]++;
}

echo "上期正码波色分布→下期特码波色转移:\n";
// 按出现次数排序
$sortedColorDist = $colorDistributionStats;
uasort($sortedColorDist, function($a, $b) {
    return array_sum($b) - array_sum($a);
});

foreach ($sortedColorDist as $dist => $nextStats) {
    $totalDist = array_sum($nextStats);
    if ($totalDist >= 5) { // 只显示出现5次以上的分布
        echo "  $dist (共$totalDist期):\n";
        foreach ($nextStats as $nextColor => $count) {
            $percent = round($count / $totalDist * 100, 2);
            echo "    → $nextColor: $count次 ($percent%)\n";
        }
    }
}

echo "\n上期主导波色→下期特码波色转移:\n";
foreach ($dominantColorStats as $dominant => $nextStats) {
    $totalDom = array_sum($nextStats);
    echo "  主导$dominant (共$totalDom期):\n";
    foreach ($nextStats as $nextColor => $count) {
        $percent = round($count / $totalDom * 100, 2);
        echo "    → $nextColor: $count次 ($percent%)\n";
    }
}

// 基于主导波色预测命中率
$dominantPredictionHit = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];
    $colors = [];
    foreach ($nums as $num) {
        $color = $colorMap[$num];
        if (!isset($colors[$color])) $colors[$color] = 0;
        $colors[$color]++;
    }

    $dominantColor = array_keys($colors, max($colors))[0];
    $nextColor = $colorMap[$next['num7']];

    if ($dominantColor == $nextColor) {
        $dominantPredictionHit++;
    }
}
echo "\n主导波色预测命中率: " . round($dominantPredictionHit / (count($history) - 1) * 100, 2) . "% ($dominantPredictionHit/" . (count($history)-1) . ")\n";

// 基于上期正码波色预测（扩展到两种最可能波色）
$expandedColorPredictionHit = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];
    $colors = ['红' => 0, '蓝' => 0, '绿' => 0];
    foreach ($nums as $num) {
        $colors[$colorMap[$num]]++;
    }

    // 选择出现次数最多的两种波色
    arsort($colors);
    $topColors = array_keys(array_slice($colors, 0, 2, true));

    $nextColor = $colorMap[$next['num7']];
    if (in_array($nextColor, $topColors)) {
        $expandedColorPredictionHit++;
    }
}
echo "扩展到两种主导波色预测命中率: " . round($expandedColorPredictionHit / (count($history) - 1) * 100, 2) . "% ($expandedColorPredictionHit/" . (count($history)-1) . ")\n";

echo "\n";

// ============ 维度7: 上期特码与下期特码的转移关系（马尔可夫分析） ============
echo "=== 维度7: 上期特码与下期特码的转移关系（马尔可夫分析） ===\n";

$specialTransfer = [];
$specialRangeTransfer = [];

for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $currentSpecial = $current['num7'];
    $nextSpecial = $next['num7'];

    // 精确转移统计
    if (!isset($specialTransfer[$currentSpecial])) {
        $specialTransfer[$currentSpecial] = [];
    }
    if (!isset($specialTransfer[$currentSpecial][$nextSpecial])) {
        $specialTransfer[$currentSpecial][$nextSpecial] = 0;
    }
    $specialTransfer[$currentSpecial][$nextSpecial]++;

    // 区间转移统计
    $currentRange = getRange($currentSpecial);
    $nextRange = getRange($nextSpecial);

    if (!isset($specialRangeTransfer[$currentRange])) {
        $specialRangeTransfer[$currentRange] = [];
    }
    if (!isset($specialRangeTransfer[$currentRange][$nextRange])) {
        $specialRangeTransfer[$currentRange][$nextRange] = 0;
    }
    $specialRangeTransfer[$currentRange][$nextRange]++;
}

echo "特码区间转移矩阵:\n";
foreach ($specialRangeTransfer as $fromRange => $toStats) {
    $totalFrom = array_sum($toStats);
    echo "  $fromRange → :\n";
    foreach ($toStats as $toRange => $count) {
        $percent = round($count / $totalFrom * 100, 2);
        echo "    $toRange: $count次 ($percent%)\n";
    }
}

// 找出高频转移
echo "\n高频特码转移（出现2次以上）:\n";
foreach ($specialTransfer as $from => $toStats) {
    foreach ($toStats as $to => $count) {
        if ($count >= 2) {
            echo "  特码$from → 特码$to: $count次\n";
        }
    }
}

// 基于特码区间预测
$specialRangePredictionHit = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $currentRange = getRange($current['num7']);

    // 找出该区间最可能转移到的两个区间
    $transferStats = $specialRangeTransfer[$currentRange];
    arsort($transferStats);
    $topRanges = array_keys(array_slice($transferStats, 0, 2, true));

    $nextRange = getRange($next['num7']);
    if (in_array($nextRange, $topRanges)) {
        $specialRangePredictionHit++;
    }
}
echo "\n基于特码区间转移预测（前2区间）命中率: " . round($specialRangePredictionHit / (count($history) - 1) * 100, 2) . "% ($specialRangePredictionHit/" . (count($history)-1) . ")\n";

echo "\n";

// ============ 综合分析：寻找40%以上命中率的规律 ============
echo "=== 综合分析：寻找40%以上命中率的规律 ===\n\n";

// 综合预测模型
echo "【综合预测模型测试】\n\n";

$combinedHits = [
    '平均值±10范围' => $hitCount,
    '正码±3范围' => $predictionHit,
    '和值尾数±2尾数范围' => $tailPredictionHit,
    '覆盖区间预测' => $hitInCoveredRange,
    '主导波色预测' => $dominantPredictionHit,
    '双波色预测' => $expandedColorPredictionHit,
    '特码区间转移' => $specialRangePredictionHit
];

echo "各维度预测命中率汇总:\n";
$totalPredictions = count($history) - 1;
foreach ($combinedHits as $name => $hit) {
    $percent = round($hit / $totalPredictions * 100, 2);
    $status = $percent >= 40 ? '【达标】' : '';
    echo "  $name: $percent% ($hit/$totalPredictions) $status\n";
}

// 组合预测测试
echo "\n组合预测测试:\n";

$comboHits = 0;
$comboPlusHits = 0;

for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];

    // 方法1：平均值±15范围
    $avg = array_sum($nums) / 6;
    $avgRange = range(max(1, floor($avg) - 15), min(49, floor($avg) + 15));

    // 方法2：正码±5范围
    $numRange = [];
    foreach ($nums as $num) {
        for ($p = max(1, $num - 5); $p <= min(49, $num + 5); $p++) {
            $numRange[$p] = true;
        }
    }

    // 方法3：和值尾数±3范围的所有数字
    $sum = array_sum($nums);
    $sumTail = getTail($sum);
    $tailRange = [];
    for ($t = $sumTail - 3; $t <= $sumTail + 3; $t++) {
        $actualTail = ($t + 10) % 10;
        for ($n = 1; $n <= 49; $n++) {
            if (getTail($n) == $actualTail) {
                $tailRange[$n] = true;
            }
        }
    }

    // 组合1：平均值范围 OR 正码范围
    if (in_array($next['num7'], $avgRange) || isset($numRange[$next['num7']])) {
        $comboHits++;
    }

    // 组合2：平均值范围 OR 正码范围 OR 尾数范围
    if (in_array($next['num7'], $avgRange) || isset($numRange[$next['num7']]) || isset($tailRange[$next['num7']])) {
        $comboPlusHits++;
    }
}

echo "组合1（平均值±15 OR 正码±5）命中率: " . round($comboHits / $totalPredictions * 100, 2) . "% ($comboHits/$totalPredictions)\n";
echo "组合2（平均值±15 OR 正码±5 OR 尾数±3）命中率: " . round($comboPlusHits / $totalPredictions * 100, 2) . "% ($comboPlusHits/$totalPredictions)\n";

// 波色+区间组合
echo "\n波色+区间组合预测:\n";
$colorRangeComboHit = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];

    // 获取上期正码覆盖的波色和区间
    $colors = ['红' => 0, '蓝' => 0, '绿' => 0];
    $ranges = ['小号(1-10)' => 0, '中号(11-30)' => 0, '大号(31-49)' => 0];
    foreach ($nums as $num) {
        $colors[$colorMap[$num]]++;
        $ranges[getRange($num)]++;
    }

    // 选择最可能的波色和区间
    arsort($colors);
    arsort($ranges);
    $topColor = array_keys($colors)[0];
    $topRange = array_keys($ranges)[0];

    // 预测：该波色+该区间的交集
    $predictNums = [];
    foreach (range(1, 49) as $n) {
        if ($colorMap[$n] == $topColor && getRange($n) == $topRange) {
            $predictNums[$n] = true;
        }
    }

    if (isset($predictNums[$next['num7']])) {
        $colorRangeComboHit++;
    }
}
echo "波色+区间交集预测命中率: " . round($colorRangeComboHit / $totalPredictions * 100, 2) . "% ($colorRangeComboHit/$totalPredictions)\n";
echo "预测范围大小: " . count($predictNums) . "个数字\n";

// 扩展波色+区间（前2波色+前2区间）
$colorRangeCombo2Hit = 0;
for ($i = 0; $i < count($history) - 1; $i++) {
    $current = $history[$i];
    $next = $history[$i + 1];

    $nums = [$current['num1'], $current['num2'], $current['num3'],
             $current['num4'], $current['num5'], $current['num6']];

    $colors = ['红' => 0, '蓝' => 0, '绿' => 0];
    $ranges = ['小号(1-10)' => 0, '中号(11-30)' => 0, '大号(31-49)' => 0];
    foreach ($nums as $num) {
        $colors[$colorMap[$num]]++;
        $ranges[getRange($num)]++;
    }

    arsort($colors);
    arsort($ranges);
    $top2Colors = array_keys(array_slice($colors, 0, 2, true));
    $top2Ranges = array_keys(array_slice($ranges, 0, 2, true));

    $predictNums2 = [];
    foreach (range(1, 49) as $n) {
        if (in_array($colorMap[$n], $top2Colors) && in_array(getRange($n), $top2Ranges)) {
            $predictNums2[$n] = true;
        }
    }

    if (isset($predictNums2[$next['num7']])) {
        $colorRangeCombo2Hit++;
    }
}
echo "前2波色+前2区间交集预测命中率: " . round($colorRangeCombo2Hit / $totalPredictions * 100, 2) . "% ($colorRangeCombo2Hit/$totalPredictions)\n";

echo "\n";

// ============ 总结：40%以上命中率的规律 ============
echo "=== 总结：达到40%以上命中率的规律 ===\n\n";

$highHitRules = [];
foreach ($combinedHits as $name => $hit) {
    $percent = round($hit / $totalPredictions * 100, 2);
    if ($percent >= 40) {
        $highHitRules[] = [
            'name' => $name,
            'percent' => $percent,
            'hit' => $hit,
            'total' => $totalPredictions
        ];
    }
}

if (count($highHitRules) > 0) {
    foreach ($highHitRules as $rule) {
        echo "【{$rule['name']}】命中率: {$rule['percent']}% ({$rule['hit']}/{$rule['total']})\n";
    }
} else {
    echo "单维度分析中没有达到40%以上命中率的规律\n";
}

// 组合规律
echo "\n组合规律命中率:\n";
echo "组合1（平均值±15 OR 正码±5）: " . round($comboHits / $totalPredictions * 100, 2) . "%\n";
echo "组合2（平均值±15 OR 正码±5 OR 尾数±3）: " . round($comboPlusHits / $totalPredictions * 100, 2) . "%\n";
echo "前2波色+前2区间交集: " . round($colorRangeCombo2Hit / $totalPredictions * 100, 2) . "%\n";

echo "\n分析完成！\n";