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. 【COGS 1873】 [国家集训队2011]happiness(吴确) 最小割

    这是一种最小割模型,就是对称三角,中间双向边,我们必须满足其最小割就是满足题目条件的互斥关系的最小舍弃,在这道题里面我们S表示文T表示理,中间一排点是每个人,每个人向两边连其选文或者选理的价值,中间每 ...

  2. vector 进阶

    http://classfoo.com/ccby/article/jnevK #include <iostream> #include <vector> #include &l ...

  3. 如何抓取开了gzip的网页

    有时候用 file_get_contents() 函数抓取网页会发生乱码现象.有两个原因会导致乱码,一个是编码问题,一个是目标页面开了Gzip. 编码问题好办,把抓取到的内容转下编码即可($conte ...

  4. 1040: [ZJOI2008]骑士~基环外向树dp

    Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在和平环境中 ...

  5. Every Programmer Should Know These Latency Numbers

    Every Programmer Should Know These Latency Numbers 1秒=1000毫秒(ms) 1秒=1,000,000 微秒(μs) 1秒=1,000,000,00 ...

  6. ERROR: Found lingering reference file hdfs

    Found lingering reference异常 ERROR: Found lingering reference file hdfs://jiujiang1:9000/hbase/month_ ...

  7. delegate, event - 里面涉及的参数类型必须完全一致,子类是不行的

    public void TestF() { Test += Fun; } public void Fun(Person p) { }  // 如 Person变成 SubPerson,则报错..pub ...

  8. es6+最佳入门实践(12)

    12.class基础用法和继承 12.1.class基础语法 在es5中,面向对象我们通常写成这样 function Person(name,age) { this.name = name; this ...

  9. set .net principle

    var ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(FormsA ...

  10. Mac git

    多次提交代码,与合并的工作量成反比. in terminal window git status:查看git的状态 git add -A: 把所有不再track里的文件加入进去/ git add -u ...