博客
关于我
2020ccpc威海 L Clock Master(数论 + 分组背包)
阅读量:283 次
发布时间:2019-03-01

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

为了解决这个问题,我们需要设计一个高精度钟表的组合,使得在给定的预算内,指针方向组合的数量最大化。这个问题可以通过动态规划和数论中的质数幂次分解来解决。

方法思路

  • 质数幂次分解:生成所有可能的质数幂次,因为这些数在乘积中能够带来最大的组合数。例如,质数2的幂次包括2, 4, 8, 16等。
  • 动态规划:使用动态规划来解决这个问题。我们将预算分成不同的质数幂次,使得它们的总和不超过预算,并且乘积最大化。为了处理乘积的最大化,我们使用对数转换,将乘积转换为对数之和,这样可以避免数值溢出,并且更容易处理。
  • 解决代码

    #include 
    #include
    #include
    #include
    using namespace std;vector
    generate_powers(int max_b) { vector
    ts; for(int p = 2; p <= max_b; ++p) { if (p == 1) continue; int current = p; while (current <= max_b) { ts.push_back(current); current *= p; } } sort(ts.begin(), ts.end()); ts.erase(unique(ts.begin(), ts.end()), ts.end()); reverse(ts.begin(), ts.end()); return ts;}int main() { const int MAX_B = 30000; vector
    ts = generate_powers(MAX_B); int T; scanf("%d", &T); for(int _ = 0; _ < T; ++_) { int b; scanf("%d", &b); if (b == 0) { printf("0.0000000000\n"); continue; } vector
    dp(b + 1, -1e18); dp[0] = 0.0; for(int ti : ts) { if (ti > b) continue; for(int i = b; i >= ti; --i) { if (dp[i - ti] != -1e18) { double new_val = dp[i - ti] + log(ti); if (new_val > dp[i]) { dp[i] = new_val; } } } } printf("%.9f\n", dp[b]); } return 0;}

    代码解释

  • 质数幂次生成:函数generate_powers生成所有可能的质数幂次,直到它们不超过给定的最大预算值。这些值用于动态规划中的选择。
  • 动态规划初始化:我们初始化一个动态规划数组dp,其中dp[i]表示在预算i下,最大乘积的对数值。初始时,dp[0]设为0,其他值设为一个很小的负数表示不可达。
  • 动态规划更新:对于每个质数幂次ti,我们从预算b开始倒序遍历,更新dp[i]的值。每次更新时,检查是否可以将ti加入当前预算,得到更大的乘积值。
  • 结果输出:对于每个测试用例,输出预算b对应的最大乘积的对数值。
  • 这种方法确保了在给定的预算内,找到最优的指针组合,使得组合数最大化。

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

    你可能感兴趣的文章
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named 'pandads'
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No static resource favicon.ico.
    查看>>
    no such file or directory AndroidManifest.xml
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>