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. Python list 列表和tuple元组

    1 list是一种Python的数据类型--列表 list是一种有序的集合,可以进行增删改查 >>>name=[aa,bb,cc] >>>name ['aa','b ...

  2. js获取当前日期、前一天、后一天的日期的例子

    <script> function addByTransDate(dateParameter, num) { var translateDate = "", dateS ...

  3. [LUOGU] P1048 采药

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:" ...

  4. python入门(一)作业

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

  5. C++基本数据类型占字节数

    32位编译器 char :1个字节char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也就是4个字节.同理64位编译器)short int : 2个字节int:  4个 ...

  6. ListView虚拟模式封装

    public class ListViewAH : ListViewEx { #region 虚拟模式相关操作 ///<summary> /// 前台行集合 ///</summary ...

  7. CSS相对布局和绝对布局

    relative 相对布局,正常的,从上到下.绝对布局absolute,就像不占位置,透明了一样,会和别的重合

  8. iOS学习笔记15-序列化、偏好设置和归档

    一.本地持久化 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件( ...

  9. 【Luogu】P1896互不侵犯King(状压DP)

    题目链接 真是可恶,被数据范围坑了一把.想要一遍AC的希望破灭了…… 以后大家在做状压DP的时候一定要开long long…… 设f[i][j][k]表示考虑前i行,总共放了j个King,第i行状态为 ...

  10. Educational Codeforces Round 13——D. Iterated Linear Function(矩阵快速幂或普通快速幂水题)

      D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes input ...