数学 SRM 690 Div1 WolfCardGame 300

本文提供了一种在特定游戏条件下帮助玩家赢得比赛的方法,通过选择特定的数字组合,确保最终分数无法达到目标值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Problem Statement

    Wolf Sothe and Cat Snuke are playing a card game. The game is played with exactly 100 cards. The cards are numbered from 1 to 100. The game is played as follows:
  1. First, Cat Snuke chooses the goal: an integer N between 1 and 100, inclusive.
  2. Then, Wolf Sothe chooses exactly K of the 100 cards and gives the chosen cards to Snuke.
  3. Next, Cat Snuke may throw some of those K cards away. He may choose any subset of cards he was given, possibly none or all of them.
  4. Finally, Cat Snuke may write minus signs onto any subset of the cards he still holds. For example, if he currently has the cards {1,3,4,7}, he may alter them to {-1,3,4,-7}.
At the end of the game, Snuke computes the sum of the numbers on his cards (with the added minus signs). Snuke wins the game if the sum is exactly equal to the goal number N. Otherwise, Sothe wins.



Your task is to help Wolf Sothe win the game. We are now in step 2 of the game. You are given the int N chosen by Snuke and the int K that specifies the number of cards you have to give to Snuke. Choose those K cards in such a way that Snuke will be unable to win the game. If you can do that, return a vector <int> with K elements: the numbers on the chosen cards. If there are multiple solutions, you may return any of them. If there is no solution, return an empty vector <int> instead.

Definition

    
Class:WolfCardGame
Method:createAnswer
Parameters:int, int
Returns:vector <int>
Method signature:vector <int> createAnswer(int N, int K)
(be sure your method is public)

Limits

    
Time limit (s):2.000
Memory limit (MB):256
Stack limit (MB):256

Constraints

-N will be between 1 and 100, inclusive.
-K will be between 1 and 15, inclusive.

Examples

0) 
    
20
4
Returns: {1, 2, 3, 4 }
If we give Snuke cards with numbers 1, 2, 3, and 4 on them, the largest sum he can form is 1+2+3+4 = 10. Thus, he cannot reach N=20 and we win.
1) 
    
40
1
Returns: {39 }
 
2) 
    
97
6
Returns: {7, 68, 9, 10, 62, 58 }
 
3) 
    
2
12
Returns: {33, 69, 42, 45, 96, 15, 57, 12, 93, 9, 54, 99 }
 

 

题意:从1到100挑出K个数字,挑出的数字可正可负,求一种总和不为N的方案.

分析:如果N为奇数,那么选前K个最小的偶数一定不会组成奇数.类似的,如果N不是3的倍数,那么选择前K个最小的3的倍数的数字;4,5,6同理,但7的时候,如果K=15,最后一个数字是105不在100内,满足该特殊情况的数字是60,那么前面添加一个1,结果是{1,7,14,21,28,35,42,49,56,63,70,77,84,91,98}

官方题解

class WolfCardGame {
    public:
        vector <int> createAnswer( int N, int K ) {
            vector<int> ret;
            if (N == 60 && K == 15) {
            	ret.push_back (1);
            	K = 14;
            }
            for (int i=2; i<=7; ++i) {
            	if (N % i != 0) {
            		for (int j=0, v=i; j<K; ++j, v+=i) {
            			ret.push_back (v);
            		}
                    break;
            	}
            }
            return ret;
        }
};

  

 

转载于:https://www.cnblogs.com/Running-Time/p/5504385.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值