[博弈][打表]Alice and Bob 2021牛客多校第1场 A

题目描述:


Alice and Bob like playing games. There are two piles of stones with numbers n  and m. Alice and Bob take turns to operate, each operation can take away k ( k > 0 ) stones from one pile and take away s × k ( s ≥ 0 )  stones from another pile. Alice plays first. The person who cannot perform the operation loses the game.

Please determine who will win the game if both Alice and Bob play the game optimally.

输入描述:


The first line contains an integer T(1≤T≤10^4 ) denotes the total number of test cases.Each test case contains two integers n,m(1≤n,m≤5×10^3 ) in a line, indicating the number of two piles of stones.

输出描述:


For each test case, print “Alice” if Alice will win the game, otherwise print “Bob”.

样例
样例一
输入
5
2 3
3 5
5 7
7 5
7 7
1
2
3
4
5
6
输出
Bob
Alice
Bob
Bob
Alice

题意: 有两堆石子,Alice和Bob轮流取石子,每次可以选择从一堆中取k个石子(k > 0),并且从另一堆取s*k个石子(s >= 0),问最终谁获胜。

分析: 必胜态就是存在一种操作使得当前状态变为必败态,必败态就是任何一种操作都使状态变为必胜态,根据样例可以知道2,3和5,7是两个必败态,如果找出所有的必败态就可以确定任何状态的输赢了,另外可以确定的是必败态最多也就5000个,因为设某一状态为(x, y),若(x, y)是必败态,则(x, y+1),(x, y+2)......都是必胜态,因为存在一种操作直接转为必败态(x, y)。

在得知必败态个数很少后,可以考虑暴力打出必败态的表,暴力代码见注释部分。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string> 
using namespace std; 

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

signed main()
{
//	l[++cnt] = 2;
//	r[cnt] = 3;
//	for(int i = 3; i <= 5000; i++)
//		for(int j = i+1; j <= 5000; j++){
//			bool flag = false;//是否为必胜态 
//			for(int k = 1; k <= cnt; k++){
//				int dl = i-l[k], dr = j-r[k];
//				if(dl > 0 && dr > 0){
//					int _min = min(dl, dr);
//					int _max = max(dl, dr);
//					if(_max%_min == 0){
//						flag = true;
//						break;
//					}
//				}
//				else if(dl == 0 && dr != 0 || dr == 0 && dl != 0){
//					flag = true;
//					break;
//				}
//				dl = i-r[k], dr = j-l[k];
//				if(dl > 0 && dr > 0){
//					int _min = min(dl, dr);
//					int _max = max(dl, dr);
//					if(_max%_min == 0){
//						flag = true;
//						break;
//					}
//				}
//				else if(dl == 0 && dr != 0 || dr == 0 && dl != 0){
//					flag = true;
//					break;
//				}
//			}
//			if(!flag){
//				l[++cnt] = i;
//				r[cnt] = j;
//			}
//		}
//	for(int i = 1; i <= cnt; i++){
//		printf("%d,%d,", l[i], r[i]);
//	}
	int len = sizeof(a)/sizeof(int);
	for(int i = 0; i < len; i+=2)
		vis[a[i]][a[i+1]] = true;
	int T;
	cin >> T;
	while(T--){
		int x, y;
		scanf("%d%d", &x, &y);
		if(x > y) swap(x, y);
		if(vis[x][y]) puts("Bob");
		else puts("Alice");
	}
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值