zoukankan      html  css  js  c++  java
  • Boring Sum(hdu4961)hash

    Boring Sum

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


    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.
     
     
    题意:

    给出n个数的数列a,bi的取值为在1 <= j < i之间如果存在aj % ai == 0,

    则取最大下标的值赋给bi,如果不存在,则bi = ai;ci的取值为在i < j <= n之间

    如果存在aj % ai == 0,则取最小下标值赋给bi,如果不存在,则ci = ai。

    求b1 * c1 + b2 * c2 + ... + bn * cn的和。

    思路:

    如果直接暴力的话一定会超时,所以我们可以开一个vis数组来记录每一个值

    所对应的最大的下标是多少。即每查找ai,分解出ai的质因子,更新vis数组

    #include<stdio.h>
    #include<string.h>
    #define ll __int64
    #define maxn 100000+5
    #define mem(x) memset(x,0,sizeof(x))
    
    
    ll a[maxn],b[maxn],c[maxn],sum,n;
    ll vis[maxn];//a[i]的下标i
    
    int main()
    {
        ll i,j,k,temp;
        while(scanf("%I64d",&n),n)
        {
            mem(b);
            mem(c);
            mem(vis);
            for(i=1;i<=n;i++)
                scanf("%I64d",&a[i]);
            vis[a[1]]=1;
            for(i=2;i<=n;i++)
            {
                for(j=1;j*j<=a[i];j++)
                {
                    if(a[i]%j!=0) continue;//取质因子
    
    //                printf("i=%I64d  j=%I64d  a[i]=%I64d  ",i,j,a[i]);
    
                    if(vis[j]!=0)//
                    {
                        b[vis[j]]=a[i];
    //                    printf("b[vis[j]]=%I64d  vis[j]=%I64d  ",b[vis[j]],vis[j]);
                        vis[j]=0;
                    }
                    temp=a[i]/j;
     //               printf("temp=%I64d  ",temp);
                    if(vis[temp]!=0)//更新
                    {
                        b[vis[temp]]=a[i];
      //                  printf("b[vis[temp]]=%I64d  vis[temp]=%I64d",b[vis[temp]],vis[temp]);
                        vis[temp]=0;
                    }
       //             printf("
    ");
                }
                vis[a[i]]=i;
            }
            for(i=1;i<=n;i++)
                if(b[i]==0)
                    b[i]=a[i];
            mem(vis);
            vis[a[n]]=n;
    
            for(i=n-1;i>=1;i--)
            {
                for(j=1;j*j<=a[i];j++)
                {
                    if(a[i]%j!=0) continue;//取质因子
                    if(vis[j]!=0)//
                    {
                        c[vis[j]]=a[i];
                        vis[j]=0;
                    }
                    temp=a[i]/j;
                    if(vis[temp]!=0)//更新
                    {
                        c[vis[temp]]=a[i];
                        vis[temp]=0;
                    }
                }
                vis[a[i]]=i;
            }
            for(i=1;i<=n;i++)
                if(c[i]==0)
                    c[i]=a[i];
      //      for(i=1;i<=n;i++)
      //      printf("b:%I64d	c:%I64d
    ",b[i],c[i]);
            sum=0;
            for(i=1;i<=n;i++)
                sum+=b[i]*c[i];
            printf("%I64d
    ",sum);
        }
        return 0;
    }
     
  • 相关阅读:
    LOJ #2196「SDOI2014」LIS
    LOJ#2249 Luogu P2305「NOI2014」购票
    LOJ #6268 分拆数
    Luogu P3700「CQOI2017」小Q的表格
    LOJ #2527 Luogu P4491「HAOI2018」染色
    knockout 学习使用笔记-----event绑定传参ko属性
    bootstrap select 学习使用笔记-------选中赋值及change监听丢失
    knockout 学习使用笔记------绑定值时赋值失败
    js 数组判断是否包含某元素 或 数组去重
    bootstrap-select 使用笔记 设置选中值及手动刷新
  • 原文地址:https://www.cnblogs.com/yuyixingkong/p/3930859.html
Copyright © 2011-2022 走看看