博客
关于我
魔法序列-upc
阅读量:282 次
发布时间:2019-03-01

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

为了解决这个问题,我们需要找到一个连续区间,使得这个区间内所有符卡的最大公约数乘以区间的长度最大的那个值。这个问题可以通过分治法来高效解决。

方法思路

  • 分治法分割问题:将数组分成左右两部分,分别计算左右子区间的最大贡献值。
  • 预处理前缀和后缀GCD数组:前缀GCD数组记录从数组起点到当前位置的所有数的最大公约数,后缀GCD数组记录从当前位置到数组终点的最大公约数。
  • 合并左右子区间:在合并左右子区间时,需要考虑中间区间的GCD是否可以与左边或右边的某些数结合,形成更大的贡献值。
  • 解决代码

    #include 
    #include
    #include
    using namespace std;struct Result { int max_power; int current_gcd; int start_pos;};int gcd(int a, int b) { while (b != 0) { int temp = a % b; a = b; b = temp; } return a;}vector
    compute_prefix_gcd(const vector
    & a) { vector
    prefix(a.size(), 0); prefix[0] = a[0]; for (int i = 1; i < a.size(); ++i) { prefix[i] = gcd(prefix[i-1], a[i]); } return prefix;}vector
    compute_suffix_gcd(const vector
    & a) { vector
    suffix(a.size(), 0); suffix[a.size()-1] = a[a.size()-1]; for (int i = a.size()-2; i >= 0; --i) { suffix[i] = gcd(suffix[i+1], a[i]); } return suffix;}Result max_power(int left, int right, const vector
    & a, const vector
    & prefix, const vector
    & suffix) { if (left > right) return {0, 0, left}; if (left == right) return {a[left], a[left], left}; int mid = (left + right) / 2; Result left_res = max_power(left, mid, a, prefix, suffix); Result right_res = max_power(mid+1, right, a, prefix, suffix); int combined_gcd = gcd(left_res.current_gcd, right_res.current_gcd); int combined_length = right - left + 1; int combined_power = combined_gcd * combined_length; if (combined_power > left_res.max_power && combined_power > right_res.max_power) { return {combined_power, combined_gcd, left}; } int mid_gcd = gcd(a[mid], a[mid+1]); if (mid_gcd != left_res.current_gcd && mid_gcd != right_res.current_gcd) { mid_gcd = gcd(mid_gcd, a[mid]); mid_gcd = gcd(mid_gcd, a[mid+1]); } int mid_length = mid - left + 1; int mid_power = mid_gcd * mid_length; if (mid_power > left_res.max_power) { left_res.max_power = mid_power; } mid_length = right - mid; mid_gcd = gcd(a[mid+1], a[mid]); mid_power = mid_gcd * mid_length; if (mid_power > right_res.max_power) { right_res.max_power = mid_power; } if (left_res.max_power > right_res.max_power) { return left_res; } else { return right_res; }}int main() { int n; read(n); vector
    a(n); for (int i = 0; i < n; ++i) { a[i] = read(); } vector
    prefix = compute_prefix_gcd(a); vector
    suffix = compute_suffix_gcd(a); Result res = max_power(0, n-1, a, prefix, suffix); cout << res.max_power << endl; return 0;}

    代码解释

  • 结构体Result:用于存储当前区间的最大贡献值、当前GCD以及区间起始位置。
  • GCD函数:计算两个数的最大公约数。
  • 前缀和后缀GCD数组:预处理数组的前缀和后缀GCD数组,用于快速查询区间内的GCD。
  • 分治函数max_power:递归地计算当前区间的最大贡献值,处理左右子区间并合并结果。
  • 合并处理:在合并左右子区间时,考虑中间区间的GCD是否可以与左边或右边的某些数结合,形成更大的贡献值。
  • 通过这种方法,我们可以高效地找到最大的魔力值。

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

    你可能感兴趣的文章
    python--基本代码规范
    查看>>
    Python--抽象类接口类
    查看>>
    PYTHON--欧拉习题二
    查看>>
    python--高级应用总结
    查看>>
    python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
    查看>>
    Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
    查看>>
    Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
    查看>>
    Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
    查看>>
    python出现 unknown encoding: idna 的处理方法
    查看>>
    Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
    查看>>
    Python-numpy(2)数组的操作
    查看>>
    Python-Numpy的tile函数用法
    查看>>
    python-open函数
    查看>>
    Python-Pandas-将特定函数应用于给定级别-多索引数据框架
    查看>>
    python-queue队列通信
    查看>>
    python-ros No module named PyKDL
    查看>>
    python-social-auth 是否需要会话
    查看>>
    Python再获年度编程语言,微软或成最大赢家
    查看>>
    python-week5
    查看>>
    Python写表情包搜索工具(非常详细)零基础入门到精通,收藏这一篇就够了
    查看>>