• 数学 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;
            }
    };
    

      

  • 相关阅读:
    Hibernate映射之数组属性, 集合属性(list、set和map)
    org.hibernate.type.SerializationException: could not deserialize 反序列化失败
    linux下同一个tomcat部署多个项目
    Nexus私服 Maven
    maven 父子项目 聚合与继承 生命周期 打包插件 依赖打包 jar pom war
    IDEA中创建Web聚合项目(Maven多模块项目)
    为什么要在linux命令前加上 ./ 什么时候才需要在命令前加上./
    web.xml 介绍 and pom.xml 介绍
    有趣的linux命令
    SpringMVC 中常见注释
  • 原文地址:https://www.cnblogs.com/Running-Time/p/5504385.html
Copyright © 2020-2023  润新知