博客
关于我
hdu_oj2176取(m堆)石子游戏(nim博弈)
阅读量:297 次
发布时间:2019-03-01

本文共 1350 字,大约阅读时间需要 4 分钟。

为了解决这个问题,我们需要分析Nim博弈的规则,确定先手是否能获胜,并提供获胜策略。Nim博弈的关键在于异或和的计算,如果异或和为0,先手必输;否则先手必赢。

方法思路

  • 输入处理:读取输入数据,每组数据由一个整数m和后面跟着m个非零正整数组成,直到m=0时结束。
  • 计算异或和:对于每一堆石子,计算所有堆石子数量的异或和k。如果k为0,先手必输,输出"No"。
  • 确定获胜策略:如果k不为0,先手必赢。对于每一堆石子,计算p = a_i ^ k。如果p小于a_i,表示从这堆石子中取出a_i - p个石子是一个有效策略。
  • 输出结果:输出"Yes"并列出所有有效策略。
  • 解决代码

    #include 
    #include
    #include
    #include
    #include
    using namespace std;int main() { vector
    a; while (true) { string line; getline(cin, line); if (line.empty()) continue; istringstream iss(line); int m; iss >> m; if (m == 0) break; vector
    parts; for (int i = 0; i < m; ++i) { int num; iss >> num; parts.push_back(num); } vector
    heaps(parts.begin(), parts.end()); ll k = 0; for (int num : heaps) { k ^= num; } if (k == 0) { cout << "No" << endl; } else { vector
    take_list; for (int num : heaps) { int p = num ^ k; if (p < num) { take_list.push_back(num); take_list.push_back(p); } } cout << "Yes"; for (int num : take_list) { cout << ' ' << num; } cout << endl; } } return 0;}

    代码解释

  • 输入处理:使用istringstream读取每一行输入,处理每组的m和后续的m个石子数量。
  • 异或和计算:遍历每一堆石子,计算所有石子数量的异或和k。
  • 策略判断:如果k为0,输出"No"。否则,遍历每一堆石子,计算p并判断是否需要采取策略。
  • 输出结果:根据策略结果输出"Yes"和相应的取法。
  • 这种方法确保了我们能够高效地解决问题,并在O(m)时间复杂度内完成所有计算。

    转载地址:http://kzlo.baihongyu.com/

    你可能感兴趣的文章
    sql查询中 查询字段数据类型 int 与 String 出现问题
    查看>>
    org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
    查看>>
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>
    org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>