Boring Sum(hdu4961)hash
Boring Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 814 Accepted Submission(s):
390
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.
给出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的更多相关文章
- hdu 4961 Boring Sum(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...
- PHP内核探索之变量(3)- hash table
在PHP中,除了zval, 另一个比较重要的数据结构非hash table莫属,例如我们最常见的数组,在底层便是hash table.除了数组,在线程安全(TSRM).GC.资源管理.Global变量 ...
- 哈希(1) hash的基本知识回顾
好久没看数据结构了,现在也打不起精神来,翻了一下书,严蔚敏那本书.,以下是书的第9章,发现自己很多时候对知识的认识无法结构化和系统化,都是零散的,模糊的混乱的记忆,以后要有体系, 第9章 查找 ...
- Redis入门到高可用(七)——Hash
一.结构 Mapmap结构: filed 不能相同,value可以相同. 二.重要指令 ♦️ HSET ♦️ HGET ♦️ HDEL ♦️ Hlen ♦️ HEXISTS ♦️HGETALL ...
- (面试)Hash表算法十道海量数据处理面试题
Hash表算法处理海量数据处理面试题 主要针对遇到的海量数据处理问题进行分析,参考互联网上的面试题及相关处理方法,归纳为三种问题 (1)数据量大,内存小情况处理方式(分而治之+Hash映射) (2)判 ...
- redis数据类型(三)hash类型
一.hash类型 hash是一个string类型的field和value的映射表.添加,删除操作都是O(1)(平均). hash特别适合用于存储对象.相对于将对象的每个字段存成单个string ...
- 「LeetCode」0001-Two Sum(Ruby)
题意与分析 题意直接给出来了:给定一个数,返回数组中和为该数(下为\(x\))的两个数的下标. 这里有一个显然的\(O(n)\)的实现:建立一个hash表,每次读入数(记作\(p\))的时候查询has ...
- leetcode 1 Two Sum(查找)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- HDU-1003:Max Sum(优化)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
随机推荐
- SpringBoot2 配置
一.Properties与Yaml SpringBoot支持properties与yaml两种配置文件application.properties/application.yml yaml简单使用 1 ...
- com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
出现上述bug的原因如下: 在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为. 禁止方式如下:在application.propertie ...
- 关于axios及其在vue中的配置
什么是axios?官方解释:axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 具有以下特点: 从浏览器中创建 XMLHttpRequests 从 nod ...
- WebRTC开发基础(WebRTC入门系列2:RTCPeerConnection)
RTCPeerConnection的作用是在浏览器之间建立数据的“点对点”(peer to peer)通信. 使用WebRTC的编解码器和协议做了大量的工作,方便了开发者,使实时通信成为可能,甚至在不 ...
- [原创]K8 Struts2 Exp 20170310 S2-045(Struts2综合漏洞利用工具)
工具: K8 Struts2 Exploit组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.blog.163.com发布: 2014/7/31 10:24 ...
- 【并发】3、LockSupport阻塞与唤醒,相较与wait和notify
我们可以使用wait和notify分别对象线程进行阻塞或者唤醒,但是我们也可以使用LockSupport实现一样的功能,并且在实际使用的时候,个人感觉LockSupport会更加顺手 范例1,wait ...
- 重启tomcat服务操作
1. 进入linux系统下tomcat的bin目录,比如:cd /usr/local/apache-tomcat-7.0.42/bin 2. 关闭一下tomcat服务,特别是已经启动的情况下,只不过有 ...
- 解决Maven的Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart
eclipse配置好了Maven,创建maven-archetype-quickstart项目报错如下: Could not resolve archetype org.apache.maven.ar ...
- 工具-CrashMonkey4IOS,Monkey测试方案
在TesterHome看到了CrashMonkey4IOS,顿时觉得之前用instrument在做monkey测试,非常的弱智!crash后啥都看不到,无crashlog,无crash步骤,并且也不能 ...
- PHP多进程系列笔记(一)
本系列文章将向大家讲解pcntl_*系列函数,从而更深入的理解进程相关知识. PCNTL在PHP中进程控制支持默认是关闭的.您需要使用 --enable-pcntl 配置选项重新编译PHP的 CGI或 ...