题目链接: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. 关于WCF在IIS8注册的问题

    原文 http://social.microsoft.com/Forums/id-ID/30cf8a24-2719-4c1c-b035-3d186fbfc09c/wcfiis8?forum=wcfzh ...

  2. Shell 输入/输出重定向

    大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端.命令通常从标准输入的地方读取输入,默认是终端.标准输出,默认情况下是终端. 重定向命令列表如下: 命令 说明 comm ...

  3. Swift - 几种使用数组的数据存储模型

    在iOS游戏开发中,比如2048游戏.有时会需要存储N×N数组的数据模型(如3×3,4×4等).这里我们演示了三种实现方式,分别是:一维数组.仿二维数组.自定义二维数组(即矩阵结构). 功能是根据传入 ...

  4. Linux通配符摘要

    参考<鸟哥linux私房菜> * - 通配符,代表随机字符(0对于许多) ? - 通配符,它代表一个字符 # - 凝视 / - 跳转符号,将特殊字符或通配符还原成一般符号 | - 分隔两个 ...

  5. &lt;ctype.h&gt; C语言标准库

    ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于測试字符是否属于特定的字符类别.如字母字符.控制字 ...

  6. 用百度API实现热(WIFI)、GPS、基站定位

    直接在代码.. .嘎嘎 /** * 百度基站定位错误返回码 */ // 61 : GPS所在地结果 // 62 : 扫描整合的基础上有针对性的失败.在这一点上的定位结果无效. // 63 : 网络异常 ...

  7. Wayland中的跨进程过程调用浅析

    原文地址:http://blog.csdn.net/jinzhuojun/article/details/40264449 Wayland协议主要提供了Client端应用与Server端Composi ...

  8. python 多线程一(lock)

    ''' Created on Jun 17, 2013 @author: smp ''' #-*- coding:utf-8 -*- import threading import time coun ...

  9. Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)

    uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...

  10. mfc 导出数据保存成excel和txt格式

    最近做了一些东西,项目到了收尾的工作.不过这次我没有参与到控件机器的功能的那一部分,都是主管自己写的.不过,所有的控件重写都是由我来做的.还有数据库这一方面是我和主管共同完成的.不过还不错,主管写一部 ...