题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961

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 b&#31;i 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 &#31;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.
 
Author
SYSU
 
Source

题意:

给出一个数列:a[i],然后

b[i]:表示在 i 前面的项,假设有a[i]的倍数(要最靠近i的),那么b[i]就等于这个数,假设没有那么b[i] = a[i];

c[i]:表示在 i 后面的项,假设有a[i]的倍数(要最靠近i的),那么c[i] 就等于这个数,假设没有那么c[i] = a[i];

思路:

//先打表,把每一个数的约数存在vector里;

//然后从前往后扫一遍,结果存在b[i],

//Ps:假设不清楚为什么从前往后扫一遍就是最靠近的那个数可调试一下(案例:9 6 3 2 1);

//然后从后往前扫一遍,结果存在c[i],

//Ps:假设不清楚为什么从后往前扫一遍就是最靠近的那个数可调试一下(案例:1 2 3 6 9);

//最后计算b[i]*c[i]的和就可以。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define maxn 100000+17
using namespace std;
typedef __int64 LL;
int vis[maxn];
int a[maxn], b[maxn], c[maxn];
vector<int>V[maxn];
void init()
{
for(int i = 0; i < maxn; i++)
V[i].clear();
for(int i = 1; i <= maxn; i++)
{
for(int j = 1; j*i <= maxn; j++)//每一个数对应的约数
{
V[i*j].push_back(i);//i是哪些数的约数
}
}
}
int main()
{
int n;
init();
while(scanf("%d",&n) && n)
{
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
}
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; i++)
{
if(vis[a[i]] == 0)
b[i]=a[i];
else
b[i]=vis[a[i]];//a[i]的倍数
for(int j = 0; j < V[a[i]].size(); j++)
vis[V[a[i]][j]] = a[i];//V[a[i]][j]为a[i]的约数
}
memset(vis,0,sizeof(vis));
for(int i = n; i >= 1; i--)
{
if(vis[a[i]] == 0)
c[i] = a[i];
else
c[i] = vis[a[i]];
for(int j = 0; j < V[a[i]].size(); j++)
vis[V[a[i]][j]] = a[i];
}
LL sum=0;
for(int i = 1; i <= n; i++)
{
sum += (LL)b[i]*(LL)c[i];
}
printf("%I64d\n",sum);
}
return 0;
}

hdu 4961 Boring Sum(数学题)的更多相关文章

  1. hdu 4961 Boring Sum(高效)

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

  2. hdu 4961 Boring Sum

    Boring Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  3. hdu 4961 Boring Sum (思维 哈希 扫描)

    题目链接 题意:给你一个数组,让你生成两个新的数组,A要求每个数如果能在它的前面找个最近的一个是它倍数的数,那就变成那个数,否则是自己,C是往后找,输出交叉相乘的和 分析: 这个题这种做法是O(n*s ...

  4. HDOJ 4961 Boring Sum

    Discription Number theory is interesting, while this problem is boring. Here is the problem. Given a ...

  5. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  6. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  7. HDU 1244 Max Sum Plus Plus Plus

    虽然这道题看起来和 HDU 1024  Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...

  8. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

  9. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

随机推荐

  1. Eequal sum sets

    Let us consider sets of positive integers less than or equal to n. Note that all elements of a set a ...

  2. Oracle Tablespace Transportation

    前提:进行表空间传输需要用户有SYSDBA的系统权限,被移动的表空间是自包含的表空间,不应有依赖于表空间外部对象的对象存在.确定是否自包含可使用系统包DBMS_TTS中的TRANSPORT_SET_C ...

  3. 仿ios版微信应用源代码

    仿微信基本功能. 基于XMPPserver的即时通信以及交友client. ----第一期代码的功能例如以下----      1.新用户注冊      2.登陆并连接XMPPserver 握手 保持 ...

  4. 解决Myeclipse在port占用,导致tomcat无法启动。(Linux)

    本文来源于:http://blog.csdn.net/svitter 引文:http://www.2cto.com/os/201305/209285.html { ubuntu查看占用某port的程序 ...

  5. gulp多张图片自动合成雪碧图

    相信做前端的同学都做过这样的事情,为优化图片,减少请求会把拿到切好的图标图片,通过ps(或者其他工具)把图片合并到一张图里面,再通过css定位把对于的样式写出来引用的html里面.对于一些图片较多的项 ...

  6. 居然还有FindFirstChangeNotification函数

    http://download.csdn.net/download/sololie/5966243

  7. 学习android内核 -- 内存管理相关

    Android内存管理: 1.当应用程序关闭以后,后台对应的进程并没有真正的退出(处于休眠状态,一般不占用系统CPU的资源),这是为了下次再启动的时候能快速启动. 2.当系统内存不够时,AmS会主动根 ...

  8. hdu 2594 Simpsons’ Hidden Talents 【KMP】

    题目链接:http://acm.acmcoder.com/showproblem.php?pid=2594 题意:求最长的串 同一时候是s1的前缀又是s2的后缀.输出子串和长度. 思路:kmp 代码: ...

  9. kill命令"-1"这个参数到底是杀进程还是reload?(转)

    kill-1:重新读取一次参数的配置文件 (类似 reload) 这句话给我的感觉是把进程杀掉后重启进程,即 reload.而我查了下 man kill,-1 对应的 signal 是 SIGHUP, ...

  10. 上mongodb创建一些吸取的经验教训指数

    想来接触mongodb它已经快一年了,对于其指数已经积累了很多的经验,知识,以这个夜黑风高的优势,放mongodb总结一番吧. 一,索引介绍 mongodb具有两类索引,分别为单键索引和复合索引. 1 ...