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. 单片机项目开发中的Muliple Inialliaztion和Mutilple Definition解决:在.c中定义全局变量或者用extern加以声明

    c/c++语言中有很多地方要用到extern,但是如果没有真正的了解它的意义,会给编程带来很大的麻烦,为了使大家少走弯路,特祥细的说明一下.      对于比较小的程序,一般只有一个c文件和一个头文件 ...

  2. linux tomcat 配置

    http://www.cnblogs.com/xdp-gacl/p/4097608.html

  3. spark history-server的使用

    为什么需要historyServer? 在运行Spark Application的时候,Spark会提供一个WEBUI列出应用程序的运行时信息:但该WEBUI随着Application的完成(成功/失 ...

  4. MVC4下拉少数名族

    List<SelectListItem> nationlist = new List<SelectListItem>() { new SelectListItem(){Valu ...

  5. ASP.NET MVC(三)

    ASP.NET Routing 模块的责任是将传入的浏览器请求映射为特有的MVC controller actions. 请求 URL 当我们不使用路由的时候 请求 http://server/app ...

  6. Linode 优惠码

    Linode 是最好的vps $10的优惠码 Linode10,推荐码:bc7852453e280eee5a8ef045c5ab54ca091c8021 链接https://www.linode.co ...

  7. shell expr 文件扩展名判断 整数判断

    expr "text.sh" : ".*\.sh" &>/dev/null && echo "yes" ||e ...

  8. linux主机vps简单性能测试

    第一,CPU.内存.硬盘检测 cat /proc/cpuinfo (查看CPU信息) cat /proc/meminfo (查看内存信息) df -lh (查看硬盘信息) 这个命令可以看到我们购买的V ...

  9. innodb buffer pool flush机制

    参考印风的博客: FLUSH操作的类型,总的来说,有三种刷新类型   BUF_FLUSH_LRU:表示从Buffer Pool的LRU上扫描并刷新 BUF_FLUSH_LIST:表示从Buffer P ...

  10. 【python cookbook】【数据结构与算法】13.通过公共键对字典列表排序

    问题:想根据一个或多个字典中的值来对列表排序 解决方案:利用operator模块中的itemgetter()函数对这类结构进行排序是非常简单的. # Sort a list of a dicts on ...