hdu 4961 Boring Sum

Boring Sum

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


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.
 

 

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.
 

 

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.
 

 

Author
SYSU
 

 

Source
 
 
 1 /**

 2 给出一个数列:a[i],然后

 3 b[i]:表示在 i 前面的项,如果有a[i]的倍数(要最靠近i的),那么b[i]就等于这个数,如果没有那么b[i] = a[i];

 4 c[i]:表示在 i 后面的项,如果有a[i]的倍数(要最靠近i的),那么c[i] 就等于这个数,如果没有那么c[i] = a[i];

 5 **/

 6 #include<iostream>

 7 #include<stdio.h>

 8 #include<cstring>

 9 #include<cstdlib>

10 #include<vector>

11 using namespace std;

12 

13 int a[100002],b[100002],c[100002];

14 vector<int>Q[100002];

15 int hash1[100002];

16 int main()

17 {

18     int n;

19     int MAX,MIN,k;

20     for(int i=2;i<=100000;i++)

21         for(int j=i;j<=100000;j=j+i)

22         Q[i].push_back(j);

23     while(scanf("%d",&n)>0)

24     {

25         if(n==0)break;

26         for(int i=1; i<=n; i++){

27             scanf("%d",&a[i]);

28             b[i] = c[i] = a[i];

29         }

30         memset(hash1,0,sizeof(hash1));

31         hash1[a[1]] = 1;

32         for(int i=2;i<=n;i++)

33         {

34             if(a[i]==1){

35                 b[i] = a[i-1];

36                 continue;

37             }

38            k = Q[a[i]].size();

39            MAX = -1;

40            for(int j=0;j<k;j++)

41             if(hash1[Q[a[i]][j]]!=0 && MAX<hash1[Q[a[i]][j]])

42             MAX = hash1[Q[a[i]][j]];

43 

44             if(MAX == -1);

45             else b[i] = a[MAX];

46             hash1[a[i]] = i;

47         }

48         memset(hash1,0,sizeof(hash1));

49         hash1[a[n]] = n;

50         for(int i=n-1;i>=1;i--)

51         {

52             if(a[i]==1) { c[i] = a[i+1]; continue;}

53             MIN = 111111111;

54             k = Q[a[i]].size();

55             for(int j=0;j<k;j++)

56                 if(hash1[Q[a[i]][j]]!=0 && MIN>hash1[Q[a[i]][j]])

57                 MIN = hash1[Q[a[i]][j]];

58             if(MIN ==111111111 );

59             else c[i] = a[MIN];

60             hash1[a[i]] = i;

61         }

62         __int64 sum = 0;

63         for(int i=1;i<=n;i++)

64             sum = sum+((__int64)b[i])*c[i];

65         printf("%I64d\n",sum);

66     }

67     return 0;

68 }

 

你可能感兴趣的:(HDU)