Boring Sum

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 814 Accepted Submission(s):
390

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.

 
 
题意:

给出n个数的数列a,bi的取值为在1 <= j < i之间如果存在aj % ai == 0,

则取最大下标的值赋给bi,如果不存在,则bi = ai;ci的取值为在i < j <= n之间

如果存在aj % ai == 0,则取最小下标值赋给bi,如果不存在,则ci = ai。

求b1 * c1 + b2 * c2 + ... + bn * cn的和。

思路:

如果直接暴力的话一定会超时,所以我们可以开一个vis数组来记录每一个值

所对应的最大的下标是多少。即每查找ai,分解出ai的质因子,更新vis数组

#include<stdio.h>
#include<string.h>
#define ll __int64
#define maxn 100000+5
#define mem(x) memset(x,0,sizeof(x)) ll a[maxn],b[maxn],c[maxn],sum,n;
ll vis[maxn];//a[i]的下标i int main()
{
ll i,j,k,temp;
while(scanf("%I64d",&n),n)
{
mem(b);
mem(c);
mem(vis);
for(i=;i<=n;i++)
scanf("%I64d",&a[i]);
vis[a[]]=;
for(i=;i<=n;i++)
{
for(j=;j*j<=a[i];j++)
{
if(a[i]%j!=) continue;//取质因子 // printf("i=%I64d j=%I64d a[i]=%I64d ",i,j,a[i]); if(vis[j]!=)//
{
b[vis[j]]=a[i];
// printf("b[vis[j]]=%I64d vis[j]=%I64d ",b[vis[j]],vis[j]);
vis[j]=;
}
temp=a[i]/j;
// printf("temp=%I64d ",temp);
if(vis[temp]!=)//更新
{
b[vis[temp]]=a[i];
// printf("b[vis[temp]]=%I64d vis[temp]=%I64d",b[vis[temp]],vis[temp]);
vis[temp]=;
}
// printf("\n");
}
vis[a[i]]=i;
}
for(i=;i<=n;i++)
if(b[i]==)
b[i]=a[i];
mem(vis);
vis[a[n]]=n; for(i=n-;i>=;i--)
{
for(j=;j*j<=a[i];j++)
{
if(a[i]%j!=) continue;//取质因子
if(vis[j]!=)//
{
c[vis[j]]=a[i];
vis[j]=;
}
temp=a[i]/j;
if(vis[temp]!=)//更新
{
c[vis[temp]]=a[i];
vis[temp]=;
}
}
vis[a[i]]=i;
}
for(i=;i<=n;i++)
if(c[i]==)
c[i]=a[i];
// for(i=1;i<=n;i++)
// printf("b:%I64d\tc:%I64d\n",b[i],c[i]);
sum=;
for(i=;i<=n;i++)
sum+=b[i]*c[i];
printf("%I64d\n",sum);
}
return ;
}
 

Boring Sum(hdu4961)hash的更多相关文章

  1. hdu 4961 Boring Sum(数学题)

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

  2. PHP内核探索之变量(3)- hash table

    在PHP中,除了zval, 另一个比较重要的数据结构非hash table莫属,例如我们最常见的数组,在底层便是hash table.除了数组,在线程安全(TSRM).GC.资源管理.Global变量 ...

  3. 哈希(1) hash的基本知识回顾

    好久没看数据结构了,现在也打不起精神来,翻了一下书,严蔚敏那本书.,以下是书的第9章,发现自己很多时候对知识的认识无法结构化和系统化,都是零散的,模糊的混乱的记忆,以后要有体系, 第9章 查找     ...

  4. Redis入门到高可用(七)——Hash

    一.结构 Mapmap结构: filed 不能相同,value可以相同. 二.重要指令 ♦️ HSET  ♦️ HGET  ♦️ HDEL ♦️ Hlen  ♦️ HEXISTS ♦️HGETALL  ...

  5. (面试)Hash表算法十道海量数据处理面试题

    Hash表算法处理海量数据处理面试题 主要针对遇到的海量数据处理问题进行分析,参考互联网上的面试题及相关处理方法,归纳为三种问题 (1)数据量大,内存小情况处理方式(分而治之+Hash映射) (2)判 ...

  6. redis数据类型(三)hash类型

    一.hash类型   hash是一个string类型的field和value的映射表.添加,删除操作都是O(1)(平均).   hash特别适合用于存储对象.相对于将对象的每个字段存成单个string ...

  7. 「LeetCode」0001-Two Sum(Ruby)

    题意与分析 题意直接给出来了:给定一个数,返回数组中和为该数(下为\(x\))的两个数的下标. 这里有一个显然的\(O(n)\)的实现:建立一个hash表,每次读入数(记作\(p\))的时候查询has ...

  8. leetcode 1 Two Sum(查找)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  9. HDU-1003:Max Sum(优化)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

随机推荐

  1. elasticsearch插件一head插件安装详解

    elasticsearch-head是一个用来浏览.与Elastic Search簇进行交互的web前端展示插件. elasticsearch-head插件主要用途: elasticsearch主要有 ...

  2. Influxdb简介与安装

    InfluxDB 是用Go语言编写的一个开源分布式时序.事件和指标数据库,无需外部依赖,类似的数据库有Elasticsearch.Graphite等 功能特色 基于时间序列,支持与时间有关的相关函数( ...

  3. ajax post 请求发送 json 字符串

    $.ajax({ // 请求方式 type:"post", // contentType contentType:"application/json", // ...

  4. jspm 简介

    借鉴:http://www.jianshu.com/p/4aba847b3e8c 功能 1. 支持加载JavaScript各种模块化的写法:AMD.CommonJS.标准化的ES6模块... 2. 包 ...

  5. Asp.net管线事件(页面请求周期)

    在处理该请求时将由 HttpApplication 类执行以下事件. 希望扩展 HttpApplication 类的开发人员尤其需要注意这些事件. . 对请求进行验证,将检查浏览器发送的信息,并确定其 ...

  6. gulp的安装以及使用详解,除了详细还是详细

    安装gulp: 1. 创建本地包管理环境: 使用npm init命令在本地生成一个package.json文件,package.json是用来记录你当前这个项目依赖了哪些包,以后别人拿到你这个项目后, ...

  7. CENTOS安装ElasticSearch(转)

    From: https://my.oschina.net/topeagle/blog/591451?fromerr=mzOr2qzZ CENTOS安装ElasticSearch ElasticSear ...

  8. 关于Python2和Python3之间的文本模型改变

    原文地址:http://python-notes.curiousefficiency.org/en/latest/python3/questions_and_answers.html#what-act ...

  9. dbvisulizer 存储过程

    --/ CREATE PROCEDURE test () BEGIN DECLARE v CHAR(10) DEFAULT 'Hello';SELECT CONCAT(v, ', ', current ...

  10. python使用(三)

    1.function_option.py2.function_code_option.py3.thread_option.py4.class_option.py5.threading_option.p ...