zoukankan      html  css  js  c++  java
  • 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 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.
     
    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
    ",sum);
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    使用EntityFramework之Code First开发与MySql数据库问题一例
    使用Emit创建DBContext对象
    一个实用的Metro滚屏效果示例
    合理使用EntityFramework数据验证的异常错误提示信息
    Metro app中使用内存流压缩和解压
    64位的windows操作系统上运行32位asp.net web应用程序的时候,出现试图加载格式不正确的程序的解决方法
    VSTO中自定义XML功能区实现Ribbon控件互相更新
    基于任务并行库实现多线程下载示例
    Android系统刷机成功后网络信号显示“无服务”修正
    Solution Explorer中显示依赖文件和链接文件
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3926398.html
Copyright © 2011-2022 走看看