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

  

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

  1. topcoder srm 690 div1 -3

    1.给定一个数字$N$,从1到100的100个数字中选出$K$个数字(设为集合$S$),然后对$S$进行如下运算: (1)删除$S$中的某些数字:(可以删除0个数字) (2)将$S$中的某些数字取为它 ...

  2. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  3. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  4. 图论 SRM 674 Div1 VampireTree 250

    Problem Statement      You are a genealogist specializing in family trees of vampires. Vampire famil ...

  5. SRM 583 DIV1

    A 裸最短路. class TravelOnMars { public: int minTimes(vector <int>, int, int); }; vector<int> ...

  6. SRM 598 DIV1

    A 只有3种情况:200以上的单独装,3个100的装一起,某两个u,v装一起且u+v<=300, 所以做法是从大到小判断每个大小的最大能与它装一起的是谁,最后剩下100的特判. B 第一轮如果未 ...

  7. SRM 590 DIV1

    转载请注明出处,谢谢viewmode=contents">http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlov ...

  8. topcoder srm 490 div1

    problem1 link 首先每$n*m$一定是一个循环,所以只需要考虑时间$[0,n*m-1]$即可.这个期间一共出现了$n$个,第i个的出现时间为$m*i$,离开的时间为$\left \lcei ...

  9. topcoder srm 440 div1

    problem1 link 二分答案,然后计算总时间.跟$T$比较确定要增大答案还是减小答案. problem2 link 可以看作是以‘*’所在位置为根的树.所以每个非根节点都有一个父节点. 那么每 ...

随机推荐

  1. js整数补零

    /* * * 整数前面补零 * * 质朴长存法 * num 要补灵的整数 * n个数,比整数位数多前面自动补零 * **/ function pad(num, n) { var len = num.t ...

  2. git push如何至两个git仓库

    分别有仓库 A(github),B(JAE 的 git),本机为C. 假设以 a 仓库作为最终的使用仓库, b为发布仓库.分支都为 dev 第一步,增加远程仓库 git remote add orig ...

  3. Eclipse Maven3新建web项目

    环境: Eclipse Neon JDK1.8 先决条件: 本机下载apache-tomcat-8,解压,在Eclipse->preferences->server里配置安装目录.并在ec ...

  4. 【ESRI论坛6周年征文】ArcEngine注记(Anno/ Label/Element等)处理专题 -入门篇

    原发表于ESRI中国社区,转过来.我的社区帐号:jhlong http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=122097 ----------- ...

  5. 四、Shell输入、输出功能和字符颜色设置

    一.Shell输入功能 1.键盘输入   方式一: [root@Salve four]# cat test.sh #!/bin/bash #-e 参数可以解析语句中的转义字符 echo -e &quo ...

  6. [UML]UML系列——用例图Use Case

    用例图的概念 用例图是描述用例.参与者以及它们之间关系的图. 用例图的作用 用例图是从用户的角度来描述对信息系统的需求,分析产品的功能和行为. 用例图定义和描述了系统的外部可见行为,是分析.设计直至组 ...

  7. 【转载】使用Pandas对数据进行筛选和排序

    使用Pandas对数据进行筛选和排序 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas对数据进行筛选和排序 目录: sort() 对单列数据进行排序 对多列数据进行排序 获取金额最小前10项 ...

  8. 表设置了自增后往里面插入不自增的id时的处理方法

    SET IDENTITY_INSERT 表名 ON 中间写insert语句,但是这里必须把列名更上 SET IDENTITY_INSERT 表名 OFF

  9. JavaScript操作JSON的方法总结,JSON字符串转换为JSON对象

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  10. T-SQL实用查询之分析数据库表的大小

    IF OBJECT_ID('tempdb..#TB_TEMP_SPACE') IS NOT NULL DROP TABLE #TB_TEMP_SPACE GO CREATE TABLE #TB_TEM ...