博客
关于我
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/

    你可能感兴趣的文章
    notepad++最详情汇总
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>