数学 SRM 690 Div1 WolfCardGame 300

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;
        }
};

  

时间: 2024-11-02 21:35:57

数学 SRM 690 Div1 WolfCardGame 300的相关文章

Topcoder SRM 643 Div1 250&lt;peter_pan&gt;

Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*......*pn,我们假设p0,p1,...,pn是单调不降的,那么v里存储的是下标为偶数 的N的质因数p0,p2,p4,...,p(2k).现在要求写一个程序,返回一个vector<long long>ans; ans里存储的是p0,p1,p2,...,pn. Limits Time Limit(m

SRM 618 DIV1 500

非常棒的组合问题,看了好一会,无想法.... 有很多做法,我发现不考虑顺序的最好理解,也最好写. 结果一定是两种形式 A....A   dp[n-1] A...A...A sgma(dp[j]*dp[n-j-1])( 1<=j<=n-2) 最后乘上n! 什么时候才能自己在比赛中做出一个500分来啊!!! class LongWordsDiv1 { public : int count(int n) { int i,j; fact[0] = 1; for(i = 1; i <= n; i

SRM 631 DIV1

SRM 631 DIV1 A:最多肯定只需要两步,中间的两行,一行黑,一行白就可以了,这样的话,只需要考虑一开始就满足,和枚举一行去染色满足的情况就可以了,暴力即可 B:贪心,一个记录当前有猫的位置和当前超过一只猫的位置,然后位置排序从左往右找,如果当前能移动到之前超过两只的位置,就全部移动过去,不增加,如果不行,那么考虑当前这个能不能铺成一条,如果可以,相应更新位置,如果不行,就让猫全部堆到右边右边去,然后堆数多1 代码: A: #include <iostream> #include &l

SRM 622 div1 250

Problem Statement    In the Republic of Nlogonia there are N cities. For convenience, the cities are numbered 0 through N-1. For each two different cities i and j, there is a direct one-way road from i to j. You are given the lengths of those roads a

SRM 587 DIV1

550 结论:同一层的交点共线. 很容易猜到,也可以跑几组数据验证. 利用结论就可以按层算,再利用对称性简化计算. 1 using namespace std; 2 #define maxn 70100 3 class TriangleXor { 4 public: 5 int theArea(int); 6 }; 7 double l[maxn] , x[maxn] , y[maxn] , H; 8 int idx; 9 10 void addpoint(double k,double w)

Topcoder SRM 648 Div1 250

Problem 给一个长度为N的"AB"字符串S,S只含有两种字符'A' 或 'B',设pair(i,j)(0=<i<j<N)表示一对 i,j 使得S[i]='A',S[j]='B'.现给定一个K,求字符串S,使得pair(i,j)的个数恰好为K.若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [2, 50] K: [0 , N*(N-1)/2 ] Solution 若K>(N/2

Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

Problem Statement      The Happy Letter game is played as follows: At the beginning, several players enter the field. Each player has a lowercase English letter on their back. The game is played in turns. In each turn, you select two players with dif

Topcoder SRM 283 Div1 300

题意:二维空间中给n个点,求一条直线(直线只可平行于x轴或y轴或两条对角线),使得最多的点到该直线距离不超过D,返回最大数量值.n不超过50 解法:设直线为ax+by+c=0,将每个点和两两点的中点分别作为关键点,枚举每个关键点,再枚举四条过关键点的直线,求出到该直线距离不超过D的点数量.维护数量最大值即可.复杂是O(n3) Code #include<cmath> #include<cstdlib> #include<cstring> #include<algo

Topcoder SRM 525 Div1 300

题意:给一个n*m的矩形,每个方格要不为空,要不有金币,每次你可以将矩形所有金币选择一个方向(上下左右)移动一格,如果移动后有金币出矩形了,则该金币消失.问最少步骤使得方格金币恰好为K (1≤n,m≤30) 解法:枚举每个子矩形,如果该子矩形含有金币数量恰好为K,则贪心算出得到该子矩形的代价,即上下移动算一次代价,左右移动算一次代价,两次代价都分别等于 移动次数最小值*2+移动次数最大值 Code #include<cstdio> #include<algorithm> #incl