HDOJ 4961 Boring Sum
Here is the problem. Given an integer sequence a 1, a 2, …, a n, let S(i) = {j|1<=j<i, and a j is a multiple of a i}. 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 a f(i). Similarly, let T(i) = {j|i<j<=n, and a j is a multiple of a i}. If T(i) is not empty, let g(i) be the minimum integer in T(i); otherwise, g(i) = i. Now we define c i as a g(i). The boring sum of this sequence is defined as b 1 * c 1 + b 2 * c 2 + … + b n * c n.
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 a 1, a 2, …, a n (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. 预处理一下每个数的约数,直接暴力做就行了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#define ll long long
#define maxn 100005
#define pb push_back
using namespace std;
ll tot=;
vector<int> son[maxn];
int n,m,a[maxn],f[maxn];
int mult[maxn],g[maxn],to; inline void init(){
for(int i=;i<=;i++)
for(int j=i;j<=;j+=i) son[j].pb(i);
} int main(){
init(); while(scanf("%d",&n)==&&n){
memset(mult,,sizeof(mult));
for(int i=;i<=n;i++){
scanf("%d",a+i);
f[i]=mult[a[i]];
if(!f[i]) f[i]=i;
for(int j=son[a[i]].size()-;j>=;j--){
to=son[a[i]][j];
mult[to]=max(mult[to],i);
}
} memset(mult,0x3f,sizeof(mult));
for(int i=n;i;i--){
g[i]=mult[a[i]];
if(g[i]==mult[]) g[i]=i;
for(int j=son[a[i]].size()-;j>=;j--){
to=son[a[i]][j];
mult[to]=min(mult[to],i);
}
} tot=;
for(int i=;i<=n;i++) tot+=(ll)a[f[i]]*(ll)a[g[i]];
printf("%lld\n",tot);
} return ;
}
HDOJ 4961 Boring Sum的更多相关文章
- hdu 4961 Boring Sum(高效)
		
pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...
 - hdu 4961 Boring Sum(数学题)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...
 - hdu 4961 Boring Sum
		
Boring Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tota ...
 - hdu 4961 Boring Sum (思维 哈希 扫描)
		
题目链接 题意:给你一个数组,让你生成两个新的数组,A要求每个数如果能在它的前面找个最近的一个是它倍数的数,那就变成那个数,否则是自己,C是往后找,输出交叉相乘的和 分析: 这个题这种做法是O(n*s ...
 - Boring Sum(hdu4961)hash
		
Boring Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
 - HDOJ(HDU).1258 Sum It Up (DFS)
		
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
 - HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)
		
因为是circle sequence,可以在序列最后+序列前n项(或前k项);利用前缀和思想,预处理出前i个数的和为sum[i],则i~j的和就为sum[j]-sum[i-1],对于每个j,取最小的s ...
 - 最大子序列和 HDOJ 1003 Max Sum
		
题目传送门 题意:求MCS(最大连续子序列和)及两个端点分析:第一种办法:dp[i] = max (dp[i-1] + a[i], a[i]) 可以不开数组,用一个sum表示前i个数字的MCS,其实是 ...
 - HDOJ 1024 Max Sum Plus Plus -- 动态规划
		
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...
 
随机推荐
- ext radiogroup如何取值和设值
			
var radios = Ext.create('Ext.form.Panel', { title: 'RadioGroup Example', width: 300, height: 125, bo ...
 - 使用adobe pdf去除PDF文档中的批量雷同文本
			
一.问题的提出 MgoSoft tiff to pdf软件没有提供中国地区的非VISA用户的购买渠道,中国通常都是银联标识走天下,卡不是VISA买不了这样的软件, 那么, MgoSoft tiff t ...
 - lwIP内存管理机制
			
lwip的内存管理机制,我们以enet_lwip这个例程为例. 在使用lwip的时候,我们可以使用两种形式的内存,一种是heap(mem.c文件-mem_malloc()),一种是pool(memp. ...
 - 转:Android 的一些比较好的开源代码项目
			
转自:http://blog.csdn.net/zengyangtech/article/details/7019439 Android PDF 阅读器 http://sourceforge.net/ ...
 - Linux下的两个经典宏定义【转】
			
转自:http://www.linuxidc.com/Linux/2015-07/120014.htm 本文首先介绍Linux下的经典宏定义,感受极客的智慧,然后根据该经典定义为下篇文章作铺垫. of ...
 - 工作技能===开发不改bug?给你支个招
			
在测试过程中,不免会遇到开发人员因为一些原因不想修改个别bug的情况.那一般遇到这种问题时,我们该如何去推进开发修改bug呢? 我们先来分析下到底会有哪些原因会导致开发不修改bug 1. 开发与测试对 ...
 - kthread_create与kernel_thread的区别【栈】
			
转自:http://blog.chinaunix.net/uid-25513153-id-2888903.html kthread_create与kernel_thread的区别 kernel thr ...
 - python读取doc
			
import os, time, fnmatch from docx import Document class search: def __init__(self, path, search_str ...
 - mybatis获取表信息,以及遍历ResultSet
			
@RunWith(SpringRunner.class) @SpringBootTest public class BravolinksCrmServerApplicationTests { @Aut ...
 - [ 总结 ] nginx 编译参数中文详解
			
贴出来,方便查找和学习. nginx 编译参数: --prefix=PATH 指向安装目录--sbin-path=PATH 指向(执行)程序文件--conf-path=PATH 指向 ...