Discription
Number theory is interesting, while this problem is boring.

Here is the problem. Given an integer sequence a 1, a 2, …, a n, let S(i) = {j|1<=j<i, and a j is a multiple of a i}. If S(i) is not empty, let f(i) be the maximum integer in S(i); otherwise, f(i) = i. Now we define bi as a f(i). Similarly, let T(i) = {j|i<j<=n, and a j is a multiple of a i}. If T(i) is not empty, let g(i) be the minimum integer in T(i); otherwise, g(i) = i. Now we define c i as a g(i). The boring sum of this sequence is defined as b 1 * c 1 + b 2 * c 2 + … + b n * c n.

Given an integer sequence, your task is to calculate its boring sum.

Input

The input contains multiple test cases.

Each case consists of two lines. The first line contains an integer n (1<=n<=100000). The second line contains n integers a 1, a 2, …, a n (1<= ai<=100000).

The input is terminated by n = 0.

Output

Output the answer in a line.

Sample Input

5
1 4 2 3 9
0

Sample Output

136

Hint

In the sample, b1=1, c1=4, b2=4, c2=4, b3=4, c3=2, b4=3, c4=9, b5=9, c5=9, so b1 * c1 + b2 * c2 + … + b5 * c5 = 136.

预处理一下每个数的约数,直接暴力做就行了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#define ll long long
#define maxn 100005
#define pb push_back
using namespace std;
ll tot=;
vector<int> son[maxn];
int n,m,a[maxn],f[maxn];
int mult[maxn],g[maxn],to; inline void init(){
for(int i=;i<=;i++)
for(int j=i;j<=;j+=i) son[j].pb(i);
} int main(){
init(); while(scanf("%d",&n)==&&n){
memset(mult,,sizeof(mult));
for(int i=;i<=n;i++){
scanf("%d",a+i);
f[i]=mult[a[i]];
if(!f[i]) f[i]=i;
for(int j=son[a[i]].size()-;j>=;j--){
to=son[a[i]][j];
mult[to]=max(mult[to],i);
}
} memset(mult,0x3f,sizeof(mult));
for(int i=n;i;i--){
g[i]=mult[a[i]];
if(g[i]==mult[]) g[i]=i;
for(int j=son[a[i]].size()-;j>=;j--){
to=son[a[i]][j];
mult[to]=min(mult[to],i);
}
} tot=;
for(int i=;i<=n;i++) tot+=(ll)a[f[i]]*(ll)a[g[i]];
printf("%lld\n",tot);
} return ;
}
												

HDOJ 4961 Boring Sum的更多相关文章

  1. hdu 4961 Boring Sum(高效)

    pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...

  2. hdu 4961 Boring Sum(数学题)

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

  3. hdu 4961 Boring Sum

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

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

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

  5. Boring Sum(hdu4961)hash

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

  6. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  7. HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)

    因为是circle sequence,可以在序列最后+序列前n项(或前k项);利用前缀和思想,预处理出前i个数的和为sum[i],则i~j的和就为sum[j]-sum[i-1],对于每个j,取最小的s ...

  8. 最大子序列和 HDOJ 1003 Max Sum

    题目传送门 题意:求MCS(最大连续子序列和)及两个端点分析:第一种办法:dp[i] = max (dp[i-1] + a[i], a[i]) 可以不开数组,用一个sum表示前i个数字的MCS,其实是 ...

  9. HDOJ 1024 Max Sum Plus Plus -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...

随机推荐

  1. input checkbox 多选 验证

    # input checkbox 多选 验证``` <ul class="orderMsg_radio"> <!--单选--> <input valu ...

  2. JavaScript的lazyload延迟加载是如何实现的

    懒加载技术(简称lazyload)并不是新技术, 它是js程序员对网页性能优化的一种方案.lazyload的核心是按需加载.在大型网站中都有lazyload的身影,例如谷歌的图片搜索页,迅雷首页,淘宝 ...

  3. Equal Sums (map的基本应用) 多学骚操作

    C. Equal Sums time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. 笔记本电脑上面安装linux网络配置以及ping通问题

    ping不同,XShell连接不上linux: 360全部关闭,即可. 具体参考: http://blog.csdn.net/xiezhaoxuan/article/details/52673236 ...

  5. bzoj 1201[HNOI2005]数三角形 1202 [HNOI2005]狡猾的商人 暴力 权值并查集

    [HNOI2005]数三角形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 349  Solved: 234[Submit][Status][Disc ...

  6. vue之axios使用

    axios是vue-resource后出现的Vue请求数据的插件.vue更新到2.0之后,作者尤大就宣告不再对vue-resource更新,而是推荐的axios. 下面我们来使用axios npm i ...

  7. TDS开启log TDS开启SSL

    参考: http://www.ibm.com/developerworks/tivoli/library/t-tds-perf/ 1. 编辑ldif文件如下 dn: cn=Audit, cn=Log ...

  8. 【hdu1251-统计难题】Trie

    http://acm.hust.edu.cn/vjudge/problem/16379 题意:给定多个单词,多次询问符合某前缀的单词有多少个. 题解:tire.数组开了5*10^6才A,不然就RE. ...

  9. 【BZOJ3624】【APIO2008】免费道路 [生成树][贪心]

    免费道路 Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Input Output Sampl ...

  10. 【洛谷 P2346】四子连棋(状态压缩,搜索)

    其实这题可以直接二进制状压做,1表示黑棋,0表示白棋,另外记录下2个空点的位置就行了. 具体看代码(冗长): #include <iostream> #include <cstdio ...