Coins in a Line

本文介绍了一个简单的游戏博弈算法,通过递推的方式预判玩家的操作来决定胜负。算法使用一个布尔型数组记录不同回合下玩家是否能赢得游戏。

请判定 第一个玩家 是输还是赢?

n = 1, 返回 true.
n = 2, 返回 true.
n = 3, 返回 false.
n = 4, 返回 true.
n = 5, 返回 true.

var firstWillWin = function (n) {
    var dp = [];
    for (var i = 1; i <= n; i++) {
        if (i == 1 || i == 2) dp[i] = true;
        else dp[i] = !dp[i - 1] || !dp[i - 2];
    }
    return dp[n];
}

 

Monocarp is going to make a purchase with cost of exactly m m burles. He has two types of coins, in the following quantities: coins worth 1 1 burle: a 1 a 1 ​ regular coins and infinitely many fancy coins; coins worth k k burles: a k a k ​ regular coins and infinitely many fancy coins. Monocarp wants to make his purchase in such a way that there's no change — the total worth of provided coins is exactly m m. He can use both regular and fancy coins. However, he wants to spend as little fancy coins as possible. What's the smallest total number of fancy coins he can use to make a purchase? Input The first line contains a single integer t t ( 1 ≤ t ≤ 3 ⋅ 10 4 1≤t≤3⋅10 4 ) — the number of testcases. The only line of each testcase contains four integers m , k , a 1 m,k,a 1 ​ and a k a k ​ ( 1 ≤ m ≤ 10 8 1≤m≤10 8 ; 2 ≤ k ≤ 10 8 2≤k≤10 8 ; 0 ≤ a 1 , a k ≤ 10 8 0≤a 1 ​ ,a k ​ ≤10 8 ) — the cost of the purchase, the worth of the second type of coin and the amounts of regular coins of both types, respectively. Output For each testcase, print a single integer — the smallest total number of fancy coins Monocarp can use to make a purchase. Examples Inputcopy Outputcopy 4 11 3 0 0 11 3 20 20 11 3 6 1 100000000 2 0 0 5 0 1 50000000 Note In the first testcase, there are no regular coins of either type. Monocarp can use 2 2 fancy coins worth 1 1 burle and 3 3 fancy coins worth 3 3 (since k = 3 k=3) burles to get 11 11 total burles with 5 5 total fancy coins. In the second testcase, Monocarp has a lot of regular coins of both types. He can use 11 11 regular coins worth 1 1 burle, for example. Notice that Monocarp doesn't have to minimize the total number of used coins. That way he uses 0 0 fancy coins. In the third testcase, Monocarp can use 5 5 regular coins worth 1 1 burle and 1 1 regular coin worth 3 3 burles. That will get him to 8 8 total burles when he needs 11 11. So, 1 1 fancy coin worth 3 3 burles is enough.
03-12
memory limit per test256 megabytes    You have n coins with denominations a1,a2,…,an and a natural number k . You also have a bag, which is initially empty, where you can place coins. You need to perform exactly k actions. In each action, you take one coin from those you have left and put it in your bag. After that, you can no longer take that coin. At the same time, you have a cat that loves even numbers, so every time the sum of the denominations of the coins in your bag becomes even, your cat empties the bag, meaning it takes all the coins to a place known only to it, and the bag is empty again. Note that the bag is emptied every time the sum becomes even during the process of adding coins, not just at the very last moment. Let your score be the sum of the denominations of the coins in the bag. Your task is to perform k actions such that your final score is maximized. Find the answer for all 1≤k≤n . DeepL 翻译    您有 n 枚面值为 a1,a2,…,an 的硬币和一个自然数 k 。您还有一个最初是空的袋子,可以用来放置硬币。您需要进行 k 次操作。在每次行动中,你都要从剩下的硬币中取出一枚硬币放进袋子里。之后,您就不能再拿这枚硬币了。 同时,你有一只喜欢偶数的猫,所以每当你包里的硬币面值之和变成偶数时,你的猫就会清空包,也就是说它会把所有硬币带到一个只有它自己知道的地方,然后包里的硬币就又空了。请注意,在添加硬币的过程中,每次总和变为偶数时,袋子都会被清空,而不是在最后一刻才清空。 让你的分数成为袋子里硬币面值的总和。你的任务是进行 k 次操作,使你的最终得分最大。找出所有 1≤k≤n 的答案。    Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows. The first line of each test case contains a single integer n (1≤n≤2⋅105 ) — the number of coins you have. The second line of each test case contains n natural numbers a1,a2,…,an (1≤ai≤109 ) — the denominations of the coins. It is guaranteed that the sum of n across all test cases does not exceed 2⋅105 . DeepL 翻译    输入 每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤104 )。测试用例说明如下。 每个测试用例的第一行都包含一个整数 n ( 1≤n≤2⋅105 ) - 您拥有的硬币数量。 每个测试用例的第二行包含 n 个自然数 a1,a2,…,an ( 1≤ai≤109 )。( 1≤ai≤109 ) - 硬币的面值。 保证所有测试用例中 n 的总和不超过 2⋅105 。    Output For each test case, output n numbers — the maximum possible score that can be achieved by performing exactly k actions for all k from 1 to n . DeepL 翻译    输出 对于每个测试用例,输出 n 个数字,即从 1 到 n 的所有 k 中,执行 k 个操作所能获得的最大分数。 Example InputCopy 6 3 1 1 1 3 1 2 3 5 4 1 3 1 2 5 4 2 3 1 3 3 4 1 2 3 4 2 2 OutputCopy 1 0 1 3 5 0 3 7 9 7 9 3 7 9 7 9 1 5 7 0 0 0    Note In the first set of input data, you have coins with denominations [1,1,1 ]. k=1 : in this case, the sum of the denominations in the bag is 1 , regardless of the chosen coin. The final score is also 1 . k=2 : in this case, the sum of the denominations in the bag is initially 1 , regardless of the choice of coin. When choosing the second coin, the sum will be 2 , regardless of the choice of coin, and thus the bag will be emptied. The final score is 0 . k=3 : in this case, when choosing the first two coins, the sum will be 2 and the bag will be emptied, after which the sum will be 0 . When choosing the third coin, the sum will become 1 . In the second set of input data, you have coins with denominations [1,2,3 ]. k=1 : in this case, when choosing the coin with denomination 2 , the bag will be emptied and the final score will be 0 . When choosing the coin with denomination 1 or 3 , the final scores will be 1 and 3 , respectively. The maximum possible final score is 3 . k=2 : in this case, when choosing two coins 1 and 3 in any order, their sum will be 4 , after which the bag will be emptied and the final score will be 0 . However, when choosing 3 as the first coin, the score will be 3 . Then you can choose, for example, coin 2 , and the final score will become 5 . k=3 : in this case, the sum of all coins will be 6 , and since each time the bag is emptied it does not change the parity of the accumulated sum, the final score is 0 .
12-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值