zoukankan      html  css  js  c++  java
  • 数学 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;
            }
    };
    

      

  • 相关阅读:
    【c# 学习笔记】使用virtual和override关键字实现方法重写
    【c# 学习笔记】多态
    【c# 学习笔记】子类的初始化顺序
    mybatis入门截图二
    解析xml文件,遍历输出xml文件中的所有节点, 最终模仿实现struts2框架自动封装参数的功能
    mybatis入门截图总结
    springMVC入门截图
    OA项目总结3
    修改struts2自定义标签的源代码,在原有基础上增加功能(用于OA项目权限判断,是否显示某个权限)
    ongl表达式中得到对象,调用对象方法(OA项目权限显示模块)
  • 原文地址:https://www.cnblogs.com/Running-Time/p/5504385.html
Copyright © 2011-2022 走看看