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. 简单shell执行脚本

    #!/bin/bash source /etc/profile APPLICATIONS_HOME="/opt/cpic_analy" APPLICATION_NAME=" ...

  2. select a.no,a.name,b.subid,b.subname,c.score

    select a.no,a.name,b.subid,b.subname,c.score from a,b,c  where a.no = c.no and b.subid = c.subid ;

  3. 讲课笔记1——meta标签、表格标签

    图片属性:src(source): 图片的来源(路径),可以放置本地图片,也可以放网上的图片的url地址 [相对路径:        ./:当前目录            ../:跳出当前目录,到上一 ...

  4. WPF知识点全攻略02- WPF体系结构

    WPF体系结构图: PersentationFramework.dll包含WPF顶层的类型,包括哪些表示窗口.面板以及其他类型控件的类型.他还实现了高层编程抽象,如样式.开发人员直接使用的大部分类都来 ...

  5. druid 配置WebStatFilter 网络统计以及监控

    WebStatFilter用于采集web-jdbc关联监控的数据. web.xml配置 <filter> <filter-name>DruidWebStatFilter< ...

  6. 一款App的开发成本是多少?

    答一: 接触过上万名创业者,开发上线过超过30款App,没有比我更适合回答这个问题的了.. 本文对想做好一款App项目的人来说这是一篇价值百万的回答!因为这是我们花了几百万试错成本试出来的经验! &l ...

  7. Java创建图片文件缩略图

    public static void uploadImg(InputStream file, String filePath, String fileName, int widthdist, int ...

  8. ios之UIButoon

    第一.UIButton的定义 UIButton *button=[[UIButton buttonWithType:(UIButtonType); 能够定义的button类型有以下6种, typede ...

  9. 基于ubuntu 14.04 kvm虚拟化部署

    1. 宿主机环境(dell备份服务器) Ubuntu 14.04 LTS 64位 内存:16G 硬盘:2T 2. 确认CPU是否支持硬件虚拟化 root@shwilling:~# egrep -o ' ...

  10. mysql8忘记root密码修改密码(mac)

    0.在/etc/my.cnf修改验证方式 [mysqld] default_authentication_plugin=mysql_native_password 1.切换root权限: sudo s ...