2014 + schools 1002 -- hdu4961 -- boring sum

Source: Internet
Author: User
Boring sum

Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 80 accepted submission (s): 39


Problem descriptionnumber 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 B I 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.


 

Inputthe 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.


 

Outputoutput the answer in a line.


 

Sample Input
51 4 2 3 90
 


 

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.

 

Calculate the product of the two A [I] closest to I before and after the I number A [I] (if not, select a [I]). add up all multiplication accumulation.

Because random data is given, the probability of each number is the same. That is to say, most of the given a [I] will be in dozens, hundreds, or higher, traversing from front to back, hash the recent occurrence of each number. For each number to be judged, it increases from 0 times until maxn, and counts all occurrences of multiples, the point closest to the position of the number. If there is no point, record itself. The first multiple of all numbers is the one. In the same way, get the following multiple and multiply and accumulate.

If a [I] is 1, no time is saved. If a [I] is 10, 10000/10 is used at most. If a [I] is 100, 10000/100 is used,

 

#include <cstring>#include <cstdio>#include <math.h>#include <algorithm>using namespace std;#define LL __int64struct node{    LL num, f;}_hash[110000];LL a[110000], b[110000], c[110000];int main(){    LL n, i, j, sum, max1, mmax;    LL ans;    while(scanf("%I64d",&n)!=EOF&&n)    {        max1=-1;        for(i=1;i<=n;i++)        {            scanf("%I64d",&a[i]);            if(max1<a[i])                max1=a[i];        }        for(i=1;i<=max1;i++)            _hash[i].f=0;        for(i=1;i<=n;i++)        {            mmax=-1;            for(j=a[i];j<=max1;j+=a[i])            {                if(_hash[j].f)                {                    if(mmax<_hash[j].num)                    {                        mmax=_hash[j].num;                    }                }            }            _hash[a[i]].f=1;            _hash[a[i]].num=i;            if(mmax==-1)                b[i]=a[i];            else                b[i]=a[mmax];        }        for(i=1;i<=max1;i++)            _hash[i].f=0;        for(i=n;i>=1;i--)        {            mmax=1e7;;            for(j=a[i];j<=max1;j+=a[i])            {                if(_hash[j].f)                {                    if(mmax>_hash[j].num)                    {                        mmax=_hash[j].num;                    }                }            }            _hash[a[i]].f=1;            _hash[a[i]].num=i;            if(mmax==1e7)                c[i]=a[i];            else                c[i]=a[mmax];        }        ans=0;        for(i=1;i<=n;i++)        {            ans+=b[i]*c[i];            //printf("b==%d  c==%d\n",b[i],c[i]);        }        printf("%I64d\n",ans);    }    return 0;}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.