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
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.
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.
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.
/**
给出一个数列: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的更多相关文章
- 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 (思维 哈希 扫描)
题目链接 题意:给你一个数组,让你生成两个新的数组,A要求每个数如果能在它的前面找个最近的一个是它倍数的数,那就变成那个数,否则是自己,C是往后找,输出交叉相乘的和 分析: 这个题这种做法是O(n*s ...
- HDOJ 4961 Boring Sum
Discription Number theory is interesting, while this problem is boring. Here is the problem. Given a ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDU 1244 Max Sum Plus Plus Plus
虽然这道题看起来和 HDU 1024 Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...
- hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)
题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...
- 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 ...
随机推荐
- C++Builder加载Png图片
有两种方法,一是把该对象的Transparent 的属性设为true,图片的白色代表即为父界面的颜色 而是在头文件加上#include <pngimage.hpp> Image1-> ...
- 在C#中使用LOG4NET(winform程序
http://www.csharpwin.com/csharpspace/678.shtml 1.下载log4net (Google log4net) 2.unzip log4net 3.运行VS,新 ...
- lower power的physical library
在一个cell library中,比较重要的是cell height,cell height由tracks来决定,track表示一个metal线的pitch. 一个cell通常被做成一定数量的trac ...
- android studio1.0 for Mac环境搭建与demo运行(手动下载gradle,科学上google) 转载
http://blog.csdn.net/allenffl/article/details/41957907 官网下载 http://developer.android.com/sdk/install ...
- python3使用csv模块读写csv文件
python3使用csv模块读写csv文件 读取csv文件: import csv #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() ...
- 【RoR win32】安装RoR
在配置好ruby的win 7 命令行下运行, gem install rails 安装成功之后会收到提示,下面就可以用rails建立项目了. 为了提高“rails new”时“bundle insta ...
- 【fedora】强制解除yum锁定
运行yum makecache时出现yum update时候出现Another app is currently holding the yum lock解决方法yum被锁定了. 可以通过执行 rm ...
- update表关联
第一种: update student set student.age =(select `user`.age from user where id=student.id ) where studen ...
- CSS Reset / Normalize 如何进行样式重置
CSS Reset 过于激进,所有样式全部消除没有必要. 关键是保持各种浏览器的兼容,包括Bootstrap的CSS Reset也是走的这个路线. 线面这个就是后面一种思路的成果: http://ne ...
- HTTP错误汇总(404、302、200……)
HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...