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. 数据倾斜是多么痛?spark作业调优秘籍

    目录视图 摘要视图 订阅 [观点]物联网与大数据将助推工业应用的崛起,你认同么?      CSDN日报20170703——<从高考到程序员——我一直在寻找答案>      [直播]探究L ...

  2. C#导入有道词典单词本到扇贝

    由于扇贝查词没有有道方便,所以很多时候添加生词都是在使用有道词典,然后顺手就保存到了有道单词本,不过在扇贝记单词可以打卡,记单词更方便,进入扇贝页面后发现扇贝单词批量导入居然一次只支持10个,查了扇贝 ...

  3. |chromosomal walk |zoo blot|鉴定疾病gene|

    5.6基于外显子的保守性鉴定真核生物编码蛋白质的基因 鉴定功能性基因的流程是:1.连锁分析找到该基因的染色体的特定区域:2.在这段序列中选择一条短序列,寻找满足两个条件的基因(条件一:因为功能性基因是 ...

  4. ios之UIImageView

    UIImageView,顾名思义,是用来放置图片的.使用Interface Builder设计界面时,当然可以直接将控件拖进去并设置相关属性,这就不说了,这里讲的是用代码. 1.创建一个UIImage ...

  5. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  6. mysqlfailover测试

    mysqlfailover是mysql官方用python语言写的一款工具,包含在mysql utilities工具集中.主要作用是保障mysql高可用.他会定时检测节点状态,当master节点不可用时 ...

  7. (12)zabbix agent 类型所有key

    zabbix服务器端通过与zabbix agent通信来获取客户端服务器的数据,agent分为两个版本,其中一个是主动一个是被动,在配置主机我们可以看到一个是agent,另一个是agent(activ ...

  8. python入门(一)作业

    一,流程控制之if...else 1. 如果:女人的年龄>30岁,那么:叫阿姨,否则:叫小妞 age_of_girl = 21 if age_of_girl >30: print('阿姨' ...

  9. google F12

    谷歌浏览器(Google Chrome)开发调试详细介绍 博客分类: 前端 浏览器chromegoogle调试开发  很多Web前台开发者都喜欢这种浏览器自带的开发者工具,这对前台设计.代码调试很大帮 ...

  10. C#上位机开发(二)—— Hello,World

    上一篇大致了解了一下单片机实际项目开发中上位机开发部分的内容以及VS下载与安装,按照编程惯例,接下来就是“Hello,World!” 1.新建C#项目工程 首先选择新建Windows窗体应用(.NET ...