样例输入:
5
2 3
3 5
5 7
7 5
7 7
Bob
Alice
Bob
Bob
Alice
题意:有两堆石子,石子数目分别为n和m,Alice和Bob轮流进行以下操作:
从一堆石子中取出k个石子,k不能为0,从另一堆石子中取出k*s个石子,s是一个非负数。
谁最后无法操作谁输,Alice是先手。
分析:这是一道博弈论的题目,先来说一下必胜态和必败态的概念,玩家对当前状态经过操作后使得对手陷入必败态,那么当前状态就是该玩家的必胜态,当前玩家的必败态是指无论当前玩家怎么操作,对手经过合理操作都能使得当前玩家重新陷入必败态。
特点:必败态无论进行任何操作都只能转为必胜态,而必胜态可以转为必败态也可以转为必胜态,所有的局面都是必胜态或者必败态之一。
所以我们就可以利用必败态进行更新,找出当前必败态可以一步到达的所有必胜态,如果发现某个状态是当前所有必败态一步到达不了的状态,那么当前状态就是必败态,按照这个方法我们可以对这道题目进行打表。设置数组vis[i][j]=true/false分别表示两堆石子分别为i和j时的状态为必胜态/必败态。我们从小到大遍历i和j,如果发现当前状态(i,j)无法由已经发现的必败态一步操作得到,那么当前状态就是一个必败态,持续上面操作即可完成更新,然后把表打出来即可O(1)得出每个询问的答案。
下面是代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=5e3+10;
typedef pair<int,int>PII;
bool vis[N][N];
int a[]={2,5,9,11,14,17,19,24,26,28,29,31,37,42,44,45,47,49,50,55,57,64,67,72,74,77,81,82,83,85,89,90,94,95,97,103,105,106,108,112,115,120,122,125,131,132,133,135,138,139,141,142,143,148,149,151,152,154,158,163,168,170,171,172,176,179,184,185,188,189,192,194,201,205,207,208,210,212,213,214,217,220,221,223,226,231,234,238,243,244,245,248,250,252,255,258,261,263,270,273,276,283,285,288,291,293,295,296,300,303,311,312,319,324,329,331,334,336,337,338,342,343,345,348,349,352,354,355,356,357,361,364,366,369,374,377,381,382,384,388,393,395,397,399,400,402,404,405,407,410,413,417,419,420,421,423,425,427,429,430,431,434,437,439,440,442,443,448,451,452,453,458,459,460,463,466,469,472,473,476,477,481,491,492,498,499,503,504,507,508,513,514,515,516,523,534,535,539,540,543,547,548,551,555,562,563,564,567,572,576,579,582,583,585,592,593,594,595,598,599,605,606,607,609,612,616,617,619,622,626,628,630,634,637,641,643,644,655,656,657,663,668,669,675,676,678,679,680,682,685,687,689,695,699,700,704,706,708,710,713,714,718,721,725,728,730,732,734,739,742,743,746,747,750,751,754,756,757,759,763,765,770,774,782,784,790,791,793,794,796,797,802,803,807,808,813,814,815,816,820,824,830,833,835,838,839,841,844,847,848,851,852,853,859,861,862,863,864,868,873,874,875,881,882,885,888,890,895,898,901,902,906,908,909,914,915,922,923,926,927,931,933,939,940,942,943,950,952,954,959,960,962,963,966,967,971,972,973,975,978,980,981,984,988,993,994,998,999,1002,1004,1005,1006,1009,1012,1015,1016,1019,1022,1025,1027,1031,1034,1035,1042,1043,1045,1048,1050,1051,1057,1058,1060,1064,1067,1068,1069,1074,1075,1078,1084,1087,1088,1089,1092,1094,1096,1100,1104,1109,1112,1113,1115,1116,1124,1125,1127,1129,1133,1134,1136,1137,1139,1140,1142,1143,1144,1147,1148,1149,1152,1153,1155,1158,1159,1161,1163,1169,1171,1173,1174,1177,1178,1182,1183,1185,1188,1196,1197,1199,1200,1204,1210,1216,1217,1221,1223,1224,1229,1233,1235,1236,1237,1238,1239,1247,1250,1251,1253,1256,1257,1258,1260,1263,1264,1267,1271,1272,1276,1278,1281,1283,1287,1289,1290,1294,1299,1300,1301,1304,1305,1309,1310,1313,1316,1319,1320,1324,1325,1327,1333,1339,1340,1342,1343,1344,1350,1351,1352,1354,1356,1357,1360,1365,1368,1369,1370,1374,1375,1380,1383,1386,1392,1396,1399,1402,1407,1410,1412,1416,1418,1422,1424,1427,1430,1434,1436,1437,1440,1441,1442,1445,1448,1453,1460,1463,1467,1472,1474,1475,1479,1480,1483,1486,1487,1489,1490,1494,1495,1497,1501,1504,1505,1507,1508,1512,1513,1518,1520,1523,1524,1531,1545,1546,1549,1550,1551,1553,1554,1555,1557,1559,1565,1566,1568,1571,1573,1574,1579,1583,1584,1586,1587,1588,1589,1594,1596,1597,1601,1603,1604,1608,1609,1610,1614,1616,1621,1624,1628,1630,1632,1635,1638,1642,1645,1646,1647,1648,1650,1655,1657,1659,1663,1667,1668,1669,1670,1672,1673,1676,1681,1686,1689,1690,1691,1697,1698,1699,1700,1701,1704,1706,1712,1715,1716,1719,1723,1724,1726,1728,1732,1734,1735,1738,1740,1741,1743,1744,1745,1748,1749,1751,1753,1760,1761,1764,1771,1776,1779,1780,1781,1784,1789,1790,1792,1794,1797,1798,1801,1804,1805,1807,1808,1810,1814,1816,1817,1818,1827,1828,1830,1834,1835,1840,1841,1844,1849,1852,1856,1857,1858,1861,1862,1865,1868,1871,1873,1875,1876,1881,1886,1890,1891,1893,1894,1896,1897,1899,1900,1903,1908,1909,1911,1913,1916,1917,1921,1925,1926,1927,1931,1932,1933,1934,1937,1938,1940,1944,1951,1952,1955,1956,1958,1959,1961,1963,1968,1969,1974,1977,1980,1983,1985,1988,1989,1991,1996,1999,2006,2009,2013,2015,2016,2020,2021,2023,2025,2028,2035,2038,2039,2044,2045,2047,2048,2053,2055,2058,2061,2062,2064,2066,2069,2072,2074,2079,2082,2083,2084,2087,2090,2091,2092,2102,2109,2110,2111,2113,2114,2118,2119,2121,2122,2124,2125,2126,2128,2131,2139,2144,2147,2150,2151,2154,2155,2157,2158,2160,2165,2167,2170,2172,2173,2178,2179,2186,2187,2189,2197,2200,2207,2208,2211,2212,2214,2219,2221,2226,2227,2228,2232,2233,2236,2241,2242,2244,2248,2249,2251,2252,2253,2258,2259,2260,2263,2265,2267,2270,2273,2274,2275,2279,2280,2285,2290,2292,2295,2300,2301,2304,2305,2306,2308,2312,2314,2316,2320,2321,2324,2326,2328,2331,2334,2340,2343,2344,2349,2352,2353,2354,2360,2361,2364,2371,2372,2373,2374,2378,2379,2380,2383,2384,2387,2392,2397,2408,2409,2413,2417,2419,2423,2425,2429,2431,2433,2434,2441,2442,2443,2446,2448,2451,2452,2457,2461,2465,2467,2472,2475,2477,2478,2479,2481,2482,2484,2487,2488,2489,2497,2498,2499,2501,2504,2507,2508,2518,2519,2520,2521,2527,2529,2532,2533,2535,2537,2540,2541,2542,2543,2544,2553,2554,2555,2559,2560,2561,2563,2566,2569,2574,2578,2579,2580,2581,2582,2584,2585,2586,2587,2590,2594,2595,2599,2602,2606,2609,2615,2618,2619,2620,2621,2625,2626,2629,2630,2636,2637,2638,2644,2649,2650,2651,2652,2654,2656,2659,2663,2664,2667,2677,2678,2679,2680,2693,2694,2696,2698,2699,2706,2711,2712,2714,2715,2717,2718,2722,2723,2725,2726,2728,2731,2732,2735,2736,2737,2740,2741,2742,2745,2753,2756,2757,2763,2765,2767,2770,2771,2775,2777,2782,2784,2787,2791,2794,2795,2797,2800,2801,2802,2808,2812,2813,2818,2820,2823,2825,2828,2829,2838,2839,2842,2845,2848,2849,2852,2853,2855,2858,2859,2860,2861,2866,2867,2871,2872,2875,2876,2878,2886,2889,2893,2895,2897,2898,2901,2904,2907,2909,2912,2914,2923,2924,2929,2930,2933,2934,2936,2940,2941,2945,2948,2950,2952,2959,2962,2963,2964,2966,2967,2968,2976,2977,2978,2983,2988,2989,2990,2991,2995,2996,2999,3000,3003,3009,3010,3011,3014,3017,3019,3023,3028,3029,3030,3034,3035,3041,3043,3045,3050,3051,3053,3055,3059,3060,3063,3066,3069,3072,3074,3076,3080,3081,3086,3087,3091,3092,3093,3094,3096,3105,3108,3112,3115,3123,3127,3132,3133,3136,3137,3140,3143,3147,3150,3152,3153,3158,3162,3165,3167,3168,3171,3174,3175,3181,3184,3187,3189,3192,3195,3199,3204,3209,3211,3212,3215,3216,3220,3221,3223,3224,3227,3228,3231,3234,3237,3239,3243,3249,3256,3257,3260,3264,3265,3268,3271,3272,3275,3277,3278,3280,3283,3285,3291,3293,3296,3301,3306,3308,3309,3310,3316,3320,3322,3329,3331,3337,3347,3348,3349,3354,3357,3361,3364,3366,3370,3371,3383,3387,3389,3390,3398,3399,3400,3402,3404,3407,3411,3415,3417,3421,3427,3428,3429,3430,3432,3435,3436,3438,3440,3443,3446,3447,3448,3452,3453,3457,3458,3462,3469,3473,3474,3477,3482,3483,3485,3486,3490,3491,3492,3494,3497,3505,3514,3515,3517,3521,3524,3527,3528,3531,3532,3540,3542,3551,3554,3555,3557,3562,3567,3568,3574,3576,3577,3580,3582,3583,3588,3591,3594,3595,3596,3603,3607,3609,3612,3613,3616,3617,3623,3625,3631,3633,3634,3637,3642,3645,3650,3654,3655,3661,3662,3663,3669,3675,3680,3686,3692,3717,3726,3742,3747,3750,3753,3817};
int b[]={3,7,12,15,20,22,33,32,35,58,40,38,53,52,75,60,65,70,62,68,79,87,86,92,99,101,174,118,110,113,123,116,129,127,126,136,199,146,145,166,246,161,160,164,309,182,177,198,180,156,239,190,186,203,195,340,197,232,218,229,215,286,228,224,350,298,253,236,268,259,241,256,266,281,274,264,271,322,317,278,289,327,280,315,301,332,307,372,391,325,313,306,321,305,339,565,346,359,375,521,358,370,362,379,456,367,390,386,432,415,464,411,408,501,435,702,454,545,489,449,496,446,445,558,467,461,590,577,532,479,478,494,527,520,509,505,639,553,470,485,518,474,511,487,483,588,601,531,530,568,537,586,557,541,529,525,580,560,719,624,570,549,574,603,600,683,556,614,871,651,620,772,671,596,661,666,610,761,632,760,691,660,726,635,673,646,653,649,788,648,776,716,664,658,740,893,855,752,735,723,825,800,697,711,1202,771,693,737,744,766,780,986,768,748,877,811,805,722,1599,828,866,845,810,849,842,831,822,787,818,799,785,836,778,879,886,899,827,1046,918,876,870,989,929,985,857,912,904,896,1107,869,964,920,883,946,937,948,910,957,892,996,935,969,976,1358,1010,917,924,1167,1062,1036,1007,1065,1000,982,944,1020,1013,995,1854,991,1038,1150,1017,1032,1085,1082,1061,1090,956,1029,1023,1777,1072,1081,1079,1118,1117,1110,1040,1131,1070,1098,1053,1122,1201,1165,1322,1241,1225,1120,1190,1106,1076,1145,1206,1180,1130,1055,1156,1384,1212,1102,1194,1175,1164,1192,1186,1285,1227,2018,1211,1208,1261,1231,1245,1214,1394,1269,1363,1243,1274,1307,1405,1268,1381,1219,1265,1314,1254,1311,1297,1420,1295,1390,1279,1337,1302,1248,3859,1284,1547,1348,1317,1451,1378,1329,1812,1372,1336,1461,1400,1346,1366,1361,1465,1432,1419,1477,1376,1292,1633,1389,1371,1527,1334,1387,1458,2369,1331,1695,1414,1408,1446,1543,1521,1413,1514,1492,1428,1687,1499,1457,1397,1468,1455,1443,1425,1464,1481,1606,1539,1758,1484,1510,1449,1626,1516,1575,1509,1532,1502,1537,1438,1563,1470,1403,2604,1674,1581,1541,1536,1534,1569,1529,1525,1592,1561,1612,1619,1491,2077,1577,2623,1560,1665,1611,1906,1590,1653,1636,1679,2455,1736,1625,1640,1929,1717,1617,1730,1678,1651,1643,1622,1580,1605,1820,1746,1721,2215,1799,1661,1708,1769,1713,1795,2004,1707,1694,1692,1682,1664,1882,1702,1684,1866,1845,1756,1710,1825,1765,1832,1774,2810,1767,1811,1754,2388,1787,1729,2140,1822,1785,1850,1802,1782,2840,2288,1842,1831,1888,1762,1838,1879,1884,1878,1859,2836,2036,1824,1919,1904,1863,1901,1978,1966,1914,1877,2042,1847,1772,1935,2040,2981,1837,1975,1953,1923,2051,1994,1869,2209,1949,2007,1947,2011,2230,1942,2205,1922,2031,1941,2134,2657,2026,2107,2030,1992,1964,1997,2098,1986,1970,2003,1981,2191,2088,2096,1972,3303,2059,2076,2390,2056,2421,2094,2070,2129,2033,2029,2116,3159,2067,2050,2105,2001,2256,2161,2103,2338,2180,2148,2137,2100,2080,2168,3385,2085,2195,2152,2142,2329,2281,2203,2198,2176,2184,2669,2194,2145,3560,2302,2229,2224,2954,2298,2276,2255,2239,2310,2182,2217,2193,2640,2366,2356,2174,2293,2202,2347,2271,3335,2437,2237,2222,4572,2297,2268,2261,2234,2132,2287,2246,2607,2439,2341,2368,2322,2335,2283,2376,3235,2495,2427,2490,2163,2362,2350,2318,3156,2453,2254,2345,2426,2317,2406,2538,2381,2557,2411,2385,2449,2402,2358,2393,2404,2910,2375,3252,2516,2332,2435,2420,2473,2365,3712,2400,2398,2957,2469,2415,2634,2459,2751,2523,2493,2485,2879,2720,2551,2444,3273,2613,2513,3991,2505,2395,2830,2502,2576,2773,2525,2462,2743,2572,2709,2511,2600,2515,2567,4079,2509,2564,2530,2592,2575,2708,2550,2549,2492,4030,2700,2665,2642,2597,2570,2548,2661,2789,2689,2675,2647,2632,2616,2931,2761,2953,2691,2611,2673,2986,2645,2738,2687,2546,2707,2780,2704,3078,2682,2556,2729,2684,2938,2702,2779,2671,3048,4593,2832,2627,3121,2785,2749,2668,4805,2747,2733,3037,2759,2806,2754,2902,2816,2809,2850,3254,3207,2856,2798,2956,2768,2746,2891,3138,2864,2758,2883,2804,4706,2788,2925,2921,2899,2834,2826,2778,3334,2882,3811,2881,3039,2869,2970,2862,4055,2814,2919,2792,2943,2873,3020,2843,2927,2917,3478,2846,2985,3244,2915,3067,3036,3908,2905,3024,2946,3261,3229,3015,2997,3064,2972,2960,3061,3007,2974,3119,3001,3148,3089,3026,3110,3099,2979,3433,2993,3046,3006,3338,3032,3004,3589,3097,3031,3172,3333,3057,3193,3084,3460,3163,3125,3130,3118,3425,3128,3218,3082,3117,3250,3012,3113,3202,3570,3396,3145,3763,3519,3197,3101,3423,3217,4038,3585,3289,3247,3177,3731,3213,3106,3201,3169,3314,3182,3810,3359,3190,3179,3103,3512,3258,4507,3393,3116,3341,3225,3327,3317,3302,3269,4240,3281,3232,3351,3552,3206,3413,3246,3299,3864,3598,3287,3500,3345,3286,3498,3297,3294,4154,3495,3325,3384,3941,3358,3355,3441,3381,3374,3350,3471,3368,3412,3379,3534,3323,3838,3693,3544,3488,3405,3924,3395,3391,3592,3538,3241,3510,3502,3422,3699,3536,3467,3459,3454,3377,3684,3475,3343,3312,3722,3409,3372,3466,3558,3518,3464,4125,3628,3605,3601,3525,3739,3418,3578,3508,3667,3480,3463,3621,4089,3671,3547,3449,3367,3584,3569,4619,3506,3772,3748,3720,3658,3529,4018,3614,3565,3844,3638,3640,3977,3619,4162,3665,3643,3549,4815,3600,4479,3635,3814,3852,3727,3759,3689,3651,3832,3676,3647,3898,3917,3719,3673,4052,3698,3825,3974,3709,3705,4012,3681,3890,4608,3656,3862,3714,3957,3762,3733,3704,3769,3848,3787,3743,3854,3771,3756,4075,3724,3999,3793,4745,3888,3932,3886,4550,3824,3777,3955,3882,3781,3776,3809,3737,4390,4186,3907,3767,4044,3900,3880,4011,3835,4782,3801,3995,4336,3935,3822,3916,3971,4197,4109,3927,3872,4192,4135,3923,3841,3798,4152,3948,3944,4027,4138,4199,4160,3921,4082,3896,3795,4151,3982,3966,3998,4779,4231,4121,4081,4074,3965,4363,4060,4302,4237,4017,3952,4443,4010,4037,4103,4176,4141,3914,4107,4070,4111,4051,4128,4249,4016,4097,3961,4700,4049,4330,3877,4278,4150,4057,3821,4263,4033,4140,4100,4447,4234,4219,4120,4223,4324,4449,4503,4222,4119,4173,4429,4184,4195,4146,4262,4210,4209,4026,4253,4169,4243,4272,4182,4563,4361,4543,4683,4271,4300,4395,4068,4291,4296,4247,4348,4307,4338,4261,4172,4314,4277,4343,4319,4992,4506,4495,4446,4425,4373,4375,4305,4528,4454,4471,4345,4610,4581,4290,4547,4549,4310,4356,4539,4270,4165,4526,4341,4493,4473,4546,4678,4453,4452,4442,4384,4466,4288,4511,4416,4298,4317,4663,4635,4523,4744,4519,4383,4522,4525,4650,4552,4438,4399,4914,4622,4532,4468,4372,4559,4434,4489,4579,4810,4725,4631,4897,4691,4605,4478,4398,4690,4601,4487,4712,4571,4675,4670,4599,4567,4437,4803,4595,4945,4578,4892,4856,4708,4660,4628,4733,4621,4882,4717,4674,4809,4681,4847,4836,4742,4751,4657,4763,4876,4761,4969,4722,4789,4181,4773,5006,4850,4832,4705,4941,4644,4786,4921,4740,4768,4979,4778,4967,4928,4818,4772,4586,4839,4428,4801,4889,4652,4939,4871,4821,4765,4935,4926,4807,4845,4977,4814,5002,4959,4907,4972,4878,4771,4865,4888,4956,4775,4948,4903,4932,5000,5008,4732,4953,5004};
int main()
{
int T;
cin>>T;
vis[2][3]=true;
int n=sizeof(a)/sizeof(int);
for(int i=0;i<n;i++)
vis[a[i]][b[i]]=true;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
if(n>m) swap(n,m);
if(vis[n][m]) puts("Bob");
else puts("Alice");
}
//打表代码
// vector<PII>p;
// p.push_back({2,3});
// for(int i=2;i<N;i++)
// for(int j=i+1;j<N;j++)
// {
// bool flag=true;
// for(int k=0;k<p.size();k++)
// {
// int x=p[k].first,y=p[k].second;
// if(i>=x&&j>y&&(i-x)%(j-y)==0)
// {
// flag=false;
// break;
// }
// if(j>=y&&i>x&&(j-y)%(i-x)==0)
// {
// flag=false;
// break;
// }
// if(i>=y&&j>x&&(i-y)%(j-x)==0)
// {
// flag=false;
// break;
// }
// if(j>=x&&i>y&&(j-x)%(i-y)==0)
// {
// flag=false;
// break;
// }
// }
// if(flag)
// {
// p.push_back({i,j});
// cout<<i<<",";
// cout<<j<<",";
// }
// }
return 0;
}
这道题目还可以用动态规划来做,思路就是利用每个必败态去更新他一步所能到达的必胜态,如果当前状态是必胜态则可以直接跳过,所以总的复杂度并没有看起来那么高,而打表的那个代码是利用当前状态与之前发现的必败态进行比较,看看是否可以由之前的必败态一步得到,所以打表的那个代码复杂度会高些,而动态规划的这种复杂度不会很高,因为动态规划是用必败态去更新所能达到的必胜态,而必败态的数量并不是很多。
下面是代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=5e3+10;
//vis[i][j]=true/false表示状态(i,j)是必胜态/必败态
bool vis[N][N];
int main()
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
if(vis[i][j]) continue;
//第一堆石子放入k个,第二堆石子放入k*s个
for(int k=1;i+k<N;k++)
for(int s=0;j+k*s<N;s++)
vis[i+k][j+k*s]=true;
//第二堆石子放入k个,第一堆石子放入k*s个
for(int k=1;j+k<N;k++)
for(int s=0;i+k*s<N;s++)
vis[i+k*s][j+k]=true;
}
int T;
cin>>T;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
if(!vis[n][m]) puts("Bob");
else puts("Alice");
}
return 0;
}