Boring Sum

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

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.

 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  4970 4968 4967 4966 4965 
 
题解:对于输入的数列,从前往后扫描一遍,对于每个数,都更新一下它的约数的左边最近倍数的值(b值);
同样地,从后往前扫描一遍,对于每个数,都更新一下它的约数的右边最近倍数的值(c值)。最后直接求所有b*c的和即可。
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100005
#define M 15
#define mod 1000000007
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
ll a[N],b[N],c[N];
int vis[N];
ll ans; int main()
{
int i;
// freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
ans=;
memset(b,,sizeof(b));
memset(c,,sizeof(c));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++){
scanf("%I64d",&a[i]);
} vis[ a[] ]=;
for(i=;i<=n;i++){
for(ll j=;j*j<=a[i];j++){
if(a[i]%j!=) continue;
if(vis[j]!=){
b[ vis[j] ]=a[i];
vis[j]=;
}
ll te=a[i]/j;
if(vis[te]!=){
b[ vis[te] ]=a[i];
vis[te]=;
}
}
vis[ a[i] ]=i;
} for(i=;i<=n;i++){
if(b[i]==) b[i]=a[i];
} memset(vis,,sizeof(vis));
vis[ a[n] ]=n;
for(i=n-;i>=;i--){
for(ll j=;j*j<=a[i];j++){
if(a[i]%j!=) continue;
if(vis[j]!=){
c[ vis[j] ]=a[i];
vis[j]=;
}
ll te=a[i]/j;
if(vis[te]!=){
c[ vis[te] ]=a[i];
vis[te]=;
}
}
vis[ a[i] ]=i;
} for(i=;i<=n;i++){
if(c[i]==) c[i]=a[i];
} for(i=;i<=n;i++){
ans+=b[i]*c[i];
}
printf("%I64d\n",ans); } return ;
}

hdu 4961 数论 o(nlogn)的更多相关文章

  1. hdu 4961 数论?

    http://acm.hdu.edu.cn/showproblem.php?pid=4961 给定ai数组; 构造bi, k=max(j | 0<j<i,a j%ai=0), bi=ak; ...

  2. hdu 4961 Boring Sum(高效)

    pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...

  3. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  4. hdu 4961 Boring Sum(数学题)

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

  5. HDU 4497 数论+组合数学

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 解题思路:将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y' ...

  6. hdu 4542 数论 + 约数个数相关 腾讯编程马拉松复赛

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4542 小明系列故事--未知剩余系 Time Limit: 500/200 MS (Java/Others) ...

  7. hdu 4352 数位dp+nlogn的LIS

    题意:求区间L到R之间的数A满足A的的数位的最长递增序列的长度为K的数的个数. 链接:点我 该题的关键是记录LIS的状态,学习过nlogn解法的同学都知道,我们每次加入的元素要和前面的比对替换,这里就 ...

  8. hdu 1664(数论+同余搜索+记录路径)

    Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

随机推荐

  1. 阿里Java架构师面试高频300题:集合+JVM+Redis+并发+算法+框架等

    前言 在过2个月即将进入9月了,然而面对今年的大环境而言,跳槽成功的难度比往年高了很多,很明显的感受就是:对于今年的java开发朋友跳槽面试,无论一面还是二面,都开始考验一个Java程序员的技术功底和 ...

  2. Hopfield 网络(上)

    讲的什么 这部分主要对 Hopfield 网络作一大概的介绍.写了其模型结构.能量函数和网络的动作方式.主要参考了网上搜到的一些相关 PPT.   概述 早在 1982 年,Hopfield 发表的文 ...

  3. 原 荐 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控

    原 荐 使用Spring Boot Actuator.Jolokia和[可视化]Grafana实现准实时监控.   监控系统:          日志- 基础处理 - 表格 - 可视化一体化解决方案. ...

  4. 业务系统中最核心的状态设计,异常 case. (系统设计)

    系统设计几方面 1. 具象: 几个角色 -- 用例 2. 具象: 边界模块 3. 具象: 实体模块 4. 抽象: 详细设计后,抽出公用的部分. 5. Status状态字段的设置和更改 系统设计中最核心 ...

  5. Linux菜鸟起飞之路【二】Linux基本常识

    一.Unix操作系统基本常识 1.什么是Unix? Unix是一个计算机操作系统,是一个用来协调.管理和控制计算机硬件与软件资源的控制程序. 2.Unix操作系统的特点? 多用户与多任务.多用户表示在 ...

  6. (原)pat1007素数猜想

    ---恢复内容开始--- 1007. 素数对猜想 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们 ...

  7. uboot的readme

    ## (C) Copyright 2000 - 2008# Wolfgang Denk, DENX Software Engineering, wd@denx.de.## See file CREDI ...

  8. python基础——12(包的概念)

    一.模块 1.模块的加载顺序 加载顺序:内存-->内置-->sys.path(一系列自定义模块) import sys sys.path  #环境变量:存放文件路径的列表 重点:默认列表的 ...

  9. POJ 1849 树的直径 Two

    如果一个点开始遍历一棵树再回到原点那么每条边走两次. 现在是两个人从同一点出发,那么最后遍历完以后两人离得越远越好. 最后两人所处位置的路径上的边走了一次,其他边走了两次. 要使总路程最小,两人最后停 ...

  10. Hive元数据启动失败

    Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocke ...