题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961
Boring Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 610 Accepted Submission(s): 301
Problem Description
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.
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<= a i<=100000).
The input is terminated by n = 0.
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<= a i<=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
136HintIn 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.
Author
SYSU
Source
思路:(1)如果这道题用O(n^2)来解,那么肯定会超时~
(2)只能用一种时间复杂度介于O(n)和O(n^2)之间的算法;先从左向右遍历一遍,每出现一个数,就用一个数组cnt[a[i]]来记录这个数a[i]的下标~求b[]
然后再反着遍历一遍,也用cnt[a[i]]来记录数a[i]出现的下标,求c[]
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <cstdio>
const int maxn=101000;
using namespace std;
int a[maxn];
int b[maxn];
int c[maxn];
int f[maxn];
int cnt[maxn];
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n)
{
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
memset(cnt,-1,sizeof(cnt));
for(int i=1;i<=n;i++)
{
if(i==1)f[i]=i;
else
{
f[i]=-1;
for(int j=a[i];j<maxn;j+=a[i])
if(cnt[j]!=-1)f[i]=max(f[i],cnt[j]);
if(f[i]==-1)f[i]=i;
}
cnt[a[i]]=i;
}
for(int i=1;i<=n;i++)
{
b[i]=a[f[i]];
}
memset(cnt,-1,sizeof(cnt));
for(int i=n;i>=1;i--)
{
if(i==n)f[i]=i;
else
{
f[i]=99999999;
for(int j=a[i];j<maxn;j+=a[i])
if(cnt[j]!=-1)f[i]=min(f[i],cnt[j]);
if(f[i]==99999999)f[i]=i;
}
cnt[a[i]]=i;
}
for(int i=1;i<=n;i++)
{
c[i]=a[f[i]];
}
long long ct=0;
for(int i=1;i<=n;i++)
ct+=(long long )b[i]*c[i];
printf("%I64d\n",ct);
}
return 0;
}