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
 
 
 /**
给出一个数列:a[i],然后
b[i]:表示在 i 前面的项,如果有a[i]的倍数(要最靠近i的),那么b[i]就等于这个数,如果没有那么b[i] = a[i];
c[i]:表示在 i 后面的项,如果有a[i]的倍数(要最靠近i的),那么c[i] 就等于这个数,如果没有那么c[i] = a[i];
**/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std; int a[],b[],c[];
vector<int>Q[];
int hash1[];
int main()
{
int n;
int MAX,MIN,k;
for(int i=;i<=;i++)
for(int j=i;j<=;j=j+i)
Q[i].push_back(j);
while(scanf("%d",&n)>)
{
if(n==)break;
for(int i=; i<=n; i++){
scanf("%d",&a[i]);
b[i] = c[i] = a[i];
}
memset(hash1,,sizeof(hash1));
hash1[a[]] = ;
for(int i=;i<=n;i++)
{
if(a[i]==){
b[i] = a[i-];
continue;
}
k = Q[a[i]].size();
MAX = -;
for(int j=;j<k;j++)
if(hash1[Q[a[i]][j]]!= && MAX<hash1[Q[a[i]][j]])
MAX = hash1[Q[a[i]][j]]; if(MAX == -);
else b[i] = a[MAX];
hash1[a[i]] = i;
}
memset(hash1,,sizeof(hash1));
hash1[a[n]] = n;
for(int i=n-;i>=;i--)
{
if(a[i]==) { c[i] = a[i+]; continue;}
MIN = ;
k = Q[a[i]].size();
for(int j=;j<k;j++)
if(hash1[Q[a[i]][j]]!= && MIN>hash1[Q[a[i]][j]])
MIN = hash1[Q[a[i]][j]];
if(MIN == );
else c[i] = a[MIN];
hash1[a[i]] = i;
}
__int64 sum = ;
for(int i=;i<=n;i++)
sum = sum+((__int64)b[i])*c[i];
printf("%I64d\n",sum);
}
return ;
}

hdu 4961 Boring Sum的更多相关文章

  1. hdu 4961 Boring Sum(高效)

    pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...

  2. hdu 4961 Boring Sum(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...

  3. hdu 4961 Boring Sum (思维 哈希 扫描)

    题目链接 题意:给你一个数组,让你生成两个新的数组,A要求每个数如果能在它的前面找个最近的一个是它倍数的数,那就变成那个数,否则是自己,C是往后找,输出交叉相乘的和 分析: 这个题这种做法是O(n*s ...

  4. HDOJ 4961 Boring Sum

    Discription Number theory is interesting, while this problem is boring. Here is the problem. Given a ...

  5. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  6. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  7. HDU 1244 Max Sum Plus Plus Plus

    虽然这道题看起来和 HDU 1024  Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...

  8. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

  9. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

随机推荐

  1. [原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) ModelDriven拦截器 paramter 拦截器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Array.prototype.each

    Array.prototype.each = function(closure){ //递归合并 return this.length ? [closure(this.slice(0,1))].con ...

  3. CCF真题之命令行选项

    201403-3 问题描述 请你写一个命令行分析程序,用以分析给定的命令行里包含哪些选项.每个命令行由若干个字符串组成,它们之间恰好由一个空格分隔.这些字符串中的第一个为该命令行工具的名字,由小写字母 ...

  4. 线性表基本维护[ACM]

    #include "iostream" #include "string" using namespace std; typedef struct node{ ...

  5. Javascript中的json操作

    <!doctype html> <html> <head> <title>extjs-json</title> <script typ ...

  6. Cortex-R5

    TCM:Tightly Coupled Memory,连接到RAM等memory中,但是CPU读写速度很快. ECC:Error Checking and Correction PMU:Perform ...

  7. PAT乙级 1021. 个位数统计 (15)

    1021. 个位数统计 (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个k位整数N = dk-1 ...

  8. 【py分析网页】可能有用的-re去除网页上的杂碎

    def remove_js_css (content): """ remove the the javascript and the stylesheet and the ...

  9. cvWaitKey();

    1.函数形式:int cvWaitKey(int delay=0 ): 函数功能:cvWaitKey()函数的功能是不断刷新图像,频率时间为delay,单位为ms. 参数:      delay——— ...

  10. Openstack的配额共功能的使用

    在一个云系统中,一个项目不能无限制的使用资源,必须对项目进行配额管理,在openstack中主要的命令是nova quota-update, 但是可能会提示的错误: DEBUG (shell:740) ...