pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum

题目大意:给定ai数组;

  • 构造bi, k=max(j|0<j<i,aj%ai=0), bi=ak;
  • 构造ci, k=min(j|i<j≤n,aj%ai=0), ci=ak;

    求∑i=1nbi∗ci

解题思路:由于ai≤105,所以预先处理好每一个数的因子,然后在处理bi,ci数组的时候,每次遍历一个数。就将其全部的因子更新,对于bi维护最大值,对于ci维护最小值。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn = 1e5;
const int INF = 0x3f3f3f3f; int n, arr[maxn+5], b[maxn+5], c[maxn+5], v[maxn+5];
vector<int> g[maxn+5]; void get_factor (int n) {
for (int i = 1; i <= n; i++)
g[i].clear(); for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i)
g[j].push_back(i);
}
} void init () { memset(v, 0, sizeof(v));
for (int i = 1; i <= n; i++) {
int u = arr[i];
int k = (v[u] == 0 ? i :v[u]);
b[i] = arr[k]; for (int j = 0; j < g[u].size(); j++)
v[g[u][j]] = max(v[g[u][j]], i);
} memset(v, INF, sizeof(v));
for (int i = n; i >= 1; i--) {
int u = arr[i];
int k = (v[u] == INF ? i : v[u]);
c[i] = arr[k]; for (int j = 0; j < g[u].size(); j++)
v[g[u][j]] = min(v[g[u][j]], i);
}
} int main () {
get_factor(maxn); while (scanf("%d", &n) == 1 && n) {
for (int i = 1; i <= n; i++)
scanf("%d", &arr[i]);
init(); ll ans = 0;
for (int i = 1; i <= n; i++)
ans = ans + b[i] * 1LL * c[i];
printf("%I64d\n", ans);
}
return 0;
}

hdu 4961 Boring Sum(高效)的更多相关文章

  1. hdu 4961 Boring Sum(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...

  2. hdu 4961 Boring Sum

    Boring Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  3. hdu 4961 Boring Sum (思维 哈希 扫描)

    题目链接 题意:给你一个数组,让你生成两个新的数组,A要求每个数如果能在它的前面找个最近的一个是它倍数的数,那就变成那个数,否则是自己,C是往后找,输出交叉相乘的和 分析: 这个题这种做法是O(n*s ...

  4. HDOJ 4961 Boring Sum

    Discription Number theory is interesting, while this problem is boring. Here is the problem. Given a ...

  5. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  6. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  7. HDU 1244 Max Sum Plus Plus Plus

    虽然这道题看起来和 HDU 1024  Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...

  8. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

  9. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

随机推荐

  1. vue-router路由知识补充

    1.render函数 var app = new Vue({ el: '#app', router, render: h => h(App) //新添加的函数操作 }) 我们新加了render: ...

  2. MongoDB Sort op eration used more than the maximum 33554432 bytes of RAM. Add an index, or speci fy a smaller limit.

    最近在获取mongodb某个集合的数据过程中,在进行排序的过程中报错,具体报错信息如下: Error: error: { , "errmsg" : "Executor e ...

  3. php中120个内置函数

    php中实现事件模式 https://yq.aliyun.com/ziliao/162660 <?php class Event{ private $events = []; public fu ...

  4. Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2016-07-20 16:27:34.873, end time: 2016-07-20 16:27:39.895, client elapsed: 0 ms

    方案一: 重启dubbo连接 zookeeper 方案二: 经压测,greys跟踪得知,是dubbo的monitor的问题.主要超时的方法是dubbo的getIP方法,monitor每次收集数据的时候 ...

  5. ccc数据库的水平分割和垂直分割

    在数据库操作中,我们常常会听说这两个词语:水平分割和垂直分割.那么到底什么是数据库的水平分割,什么是数据库的垂直分割呢?本文我们就来介绍一下这部分内容. 1.水平分割: 按记录进分分割,不同的记录可以 ...

  6. VS2017桌面应用程序打包成.msi或者.exe

    百度很难搜索到相关内容,分享下,需要的盆友拿去,不谢. http://xm2013.com/#/d/12 youtube地址:https://www.youtube.com/watch?v=z0v6h ...

  7. Swift2.1keyword @noescape介绍

    @noescape优化编译器编译,swift 2.1引入. 函数假设有一个一个參数是closure的话,能够在前面加一个关键字@noescape表示在函数return前closure生命周期结束,能够 ...

  8. element-ui 源码架构

    1.项目结构 2.src下的入口文件 https://github.com/ElemeFE/element/blob/dev/src/index.js 入口文件实现的功能为: (1)国际化配置 (2) ...

  9. 解决 Maven was cached in the local repository, resolution will not be reattempted until the update interv

    问题原因 Maven默认会使用本地缓存的库来编译工程,对于上次下载失败的库,maven会在~/.m2/repository/<group>/<artifact>/<ver ...

  10. HDU 3974 Assign the task(dfs时间戳+线段树成段更新)

    题意:给定点的上下级关系,规定假设给i分配任务a.那么他的全部下属.都停下手上的工作,開始做a. 操作 T x y 分配x任务y,C x询问x的当前任务: Sample Input 1 5 4 3 3 ...