题目链接: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. 程序中非action获得spring容器

    java类: public class MySpringContext implements ApplicationContextAware{ private static ApplicationCo ...

  2. Flume 1.5日志收集和存款mongodb安装结构

    Flume该演示是不是说.你可以自己搜索. 但现在的互联网主要是Flume 1.4前版本号的信息.Flume 1.5在轰动的大变化.假设你准备尝试,我在这里给大家介绍一下程序最小化结构,和使用Mong ...

  3. Swift - 点击输入框外部屏幕关闭虚拟键盘

    我们如果把文本框的Return Key设置成Done,然后在storyboard中将文本框的Did End On Exit事件在代码里进行关联.同时关联代码里调用文本框的resignFirstResp ...

  4. 在ListCtrl控件中设置自定义光标

    ::SetCursor(::LoadCursor   (::AfxGetInstanceHandle(),   MAKEINTRESOURCE(IDB_BMP_MOUSE))); void   CMy ...

  5. Windows 8 和 Windows 8.1 中对插件和 ActiveX 的支持

    此文章将介绍页面在 Windows 8 适用于桌面版的 Internet Explorer 中与在新 Windows UI 的 Internet Explorer 中的不同表现. Windows 8 ...

  6. HTML中Id和Name的区别

    源地址:http://www.cnblogs.com/laodai/articles/2244215.html 在html中:name指的是用户名称,ID指的是用户注册是系统自动分配给用户的一个序列号 ...

  7. c#调用语音功能

    转自 http://www.cnblogs.com/Hans2Rose/p/WeatherSpeaker.html .Net里面自带了一个语音类库:System.Speech,调用系统的语音功能,就能 ...

  8. Lisp的永恒之道 好文

    http://www.cnblogs.com/weidagang2046/archive/2012/06/03/tao_of_lisp.html

  9. 阿录帮帮忙—spring mvc 的hello world

    一:web.xml配置 <!-- Spring MVC配置 --> <servlet> <servlet-name>Spring MVC Dispatcher Se ...

  10. docker 私有仓库查询

    docker:/etc/init.d# docker ps -a | grep reg c7d5592a3658 registry "docker-registry" 11 min ...