码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 4961 Boring Sum

时间:2018-01-21 20:40:29      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:can   bsp   tip   mem   nat   处理   name   sum   pos   

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=0;
vector<int> son[maxn];
int n,m,a[maxn],f[maxn];
int mult[maxn],g[maxn],to;

inline void init(){
    for(int i=1;i<=100000;i++)
        for(int j=i;j<=100000;j+=i) son[j].pb(i);
}

int main(){
    init();
    
    while(scanf("%d",&n)==1&&n){
        memset(mult,0,sizeof(mult));
        for(int i=1;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()-1;j>=0;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[0]) g[i]=i;
            for(int j=son[a[i]].size()-1;j>=0;j--){
                to=son[a[i]][j];
                mult[to]=min(mult[to],i);
            }            
        }
        
        tot=0;
        for(int i=1;i<=n;i++) tot+=(ll)a[f[i]]*(ll)a[g[i]];
        printf("%lld\n",tot);
    }
    
    return 0;
}

 

HDOJ 4961 Boring Sum

标签:can   bsp   tip   mem   nat   处理   name   sum   pos   

原文地址:https://www.cnblogs.com/JYYHH/p/8325138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!