HDU 4961 Boring Sum 打表、更新

來源:互聯網
上載者:User

標籤:des   style   http   color   java   os   io   strong   

點擊開啟連結Boring SumTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 715    Accepted Submission(s): 351


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 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. 
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. 
 
AuthorSYSU 
Source2014 Multi-University Training Contest 9 對於輸入的數列,從前往後掃一遍,對於每個數都要更新為距離它左邊最近的倍數的值,如果沒有則為次數。同樣從後往前掃一遍,對於每個數都要更新為距離它右邊最近的倍數的值,如果沒有也為次數。將所有數的約數打個表存起來,然後掃兩遍分別記錄b[]和c[],掃的過程中要隨時更新。
//203MS8904K#include<stdio.h>#include<string.h>#include<vector>using namespace std;vector<int>v[100007];int a[100007],b[100007],c[100007],vis[100007];int main(){    int n;    for(int i=2;i<100007;i++)    {        for(int j=i;j<100007;j+=i)            v[j].push_back(i);    }    while(scanf("%d",&n),n)    {        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        for(int i=1;i<=n;i++)        {             if(!vis[a[i]])b[i]=a[i];             else b[i]=vis[a[i]];             vis[1]=a[i];          //因為打表的時候沒有算上約數1,所以要加上             for(int j=0;j<v[a[i]].size();j++)//更新a[i]的約數                 vis[v[a[i]][j]]=a[i];        }        memset(vis,0,sizeof(vis));        for(int i=n;i>=1;i--)        {             if(!vis[a[i]])c[i]=a[i];             else c[i]=vis[a[i]];             vis[1]=a[i];             for(int j=0;j<v[a[i]].size();j++)                 vis[v[a[i]][j]]=a[i];        }        __int64 ans=0;        for(int i=1;i<=n;i++)            ans+=(__int64)b[i]*(__int64)c[i];        printf("%I64d\n",ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.