HDU 4961 Boring Sum

Boring Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 739    Accepted Submission(s): 363


Problem Description
Number theory is interesting, while this problem is boring.

Here is the problem. Given an integer sequence a1, a2, …, an, let S(i) = {j|1<=j<i, and aj is a multiple of ai}. 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 af(i). Similarly, let T(i) = {j|i<j<=n, and aj is a multiple of ai}. If T(i) is not empty, let g(i) be the minimum integer in T(i); otherwise, g(i) = i. Now we define ci as ag(i). The boring sum of this sequence is defined as b1 * c1 + b2 * c2 + … + bn * cn.

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 a1, a2, …, an (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.
 

Source
2014 Multi-University Training Contest 9


题目链接  :http://acm.hdu.edu.cn/showproblem.php?pid=4961


题目大意  :输入一个数列a[i],数列b[j]的组成为:如果在a[i]的前i-1项中有a[i]的倍数,取最接近i的a[i]值为b[i]值,如果没有那么b[i] = a[i]

数列c[i]的组成为:如果在a[i]的i+1到n项中有a[i]的倍数,取最接近i的a[i]值为c[i]值,如果没有那么c[i] = a[i]



题目分析 :左右扫一下算出b,c数组

#include <cstdio>
#include <cstring>
#define ll long long
int const MAX = 1e5 + 10;
int n, vis[MAX];
ll a[MAX], b[MAX], c[MAX], ans;

int main()
{
    while(scanf("%d", &n) != EOF && n)
    {
        ans = 0;
        memset(b, 0, sizeof(b));
        memset(c, 0, sizeof(c));
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++)
            scanf("%I64d", &a[i]);
        vis[ a[1] ] = 1;
        for(int i = 2; i <= n; i++)
        {
            for(ll j = 1; j * j <= a[i]; j++)
            {
                if(a[i] % j) 
                    continue;
                if(vis[j])
                {
                    b[ vis[j] ] = a[i];
                    vis[j] = 0;
                }
                ll tmp = a[i] / j;
                if(vis[tmp])
                {
                    b[ vis[tmp] ] = a[i];
                    vis[tmp] = 0;
                }
            }
            vis[ a[i] ] = i;
        }
        for(int i = 1; i <= n; i++)
            if(b[i] == 0) 
                b[i] = a[i];
        memset(vis, 0, sizeof(vis));
        vis[ a[n] ] = n;
        for(int i = n - 1; i >= 1; i--)
        {
            for(ll j = 1; j * j <= a[i]; j++)
            {
                if(a[i] % j) 
                    continue;
                if(vis[j])
                {
                    c[ vis[j] ] = a[i];
                    vis[j] = 0;
                }
                ll tmp = a[i] / j;
                if(vis[tmp])
                {
                    c[ vis[tmp] ] = a[i];
                    vis[tmp] = 0;
                }
            }
            vis[ a[i] ] = i;
        }
        for(int i = 1; i <= n; i++)
            if(c[i] == 0) 
                c[i] = a[i];
        for(int i = 1; i <= n; i++)
            ans += b[i] * c[i];
        printf("%I64d\n",ans);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值