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. socketio server推送

    假设面试官问你:要把server端的数据时时显示在浏览器上怎么实现?我想有非常多人会回答使用Ajax技术定时去訪问一个资源,没错,使用Ajax的确能实现.但面试官要的绝对不是这个答案. 由于使用Aja ...

  2. System.ComponentModel.Component : MarshalByRefObject, IComponent, IDisposable

    #region 程序集 System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Windows\ ...

  3. B. Ohana Cleans Up(Codeforces Round #309 (Div. 2))

    B. Ohana Cleans Up   Ohana Matsumae is trying to clean a room, which is divided up into an n by n gr ...

  4. #lspci | grep Eth

    该命令作用:将lspci的输出当做输入,从中找出包含Eth的行.在我的Fedora机器上运行结果为 [root@localhost etc]# lspci | grep Eth00:04.0 Ethe ...

  5. CIF、QCIF

    分辨率: 每个像素的存储方式都是YUV   QQCIF:88*72 QCIF:176*144 CIF:352*288 2CIF:704*288 DCIF:584*384 4CIF:704*576   ...

  6. 如何:在 DHTML 代码和客户端应用程序代码之间实现双向通信

    https://msdn.microsoft.com/zh-cn/library/a0746166 可以使用 WebBrowser 控件向 Windows 窗体客户端应用程序添加现有的动态 HTML ...

  7. PyQt5教程——对话框(6)

    PyQt5中的对话框 对话框窗口或对话框是大多数主流GUI应用不可缺少的部分.对话是两个或更多人之间的会话.在计算机应用中,对话框是一个用来和应用对话的窗口.对话框可以用来输入数据,修改数据,改变应用 ...

  8. Java动态加载属性文件.properties

    当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties prop ...

  9. red5源代码编译并打包公布

    编译环境:ubuntu14.04/JDK7 步骤: 1.svn检出源代码(两种方式) svn co --depth empty https://github.com/Red5/red5-server ...

  10. 统计一个文件中出现字符'a'的次数

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #统计一个文件中出现字符'a'的次数 #http://www.cnblogs.com/hongten/p/ho ...