博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 4961 Boring Sum(数学题)
阅读量:4951 次
发布时间:2019-06-11

本文共 2903 字,大约阅读时间需要 9 分钟。

题目链接:

Problem Description
Number theory is interesting, while this problem is boring.
Here is the problem. Given an integer sequence a
1, a
2, …, a
n, let S(i) = {j|1<=j<i, and a
j is a multiple of a
i}. 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 a
f(i). Similarly, let T(i) = {j|i<j<=n, and a
j is a multiple of a
i}. If T(i) is not empty, let g(i) be the minimum integer in T(i); otherwise, g(i) = i. Now we define c
i as a
g(i). The boring sum of this sequence is defined as b
1 * c
1 + b
2 * c
2 + … + b
n * c
n.
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 a
1, a
2, …, a
n (1<= a
i<=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 
#include
#include
#include
#define maxn 100000+17using namespace std;typedef __int64 LL;int vis[maxn];int a[maxn], b[maxn], c[maxn];vector
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;}

转载于:https://www.cnblogs.com/bhlsheji/p/4326726.html

你可能感兴趣的文章
Android应用程序与SurfaceFlinger服务的连接过程分析
查看>>
coco2dx服务器简单例子
查看>>
Java回顾之多线程
查看>>
sqlite
查看>>
机电行业如何进行信息化建设
查看>>
Windows Azure Platform Introduction (4) Windows Azure架构
查看>>
【转】chrome developer tool 调试技巧
查看>>
mahout运行测试与kmeans算法解析
查看>>
互相给一巴掌器
查看>>
Android SDK环境变量配置
查看>>
VM10虚拟机安装图解
查看>>
9、总线
查看>>
Git 笔记 - section 1
查看>>
java通过jsp+javaBean+servlet实现下载功能
查看>>
STM32 使用Cubemx 建一个USB(HID)设备下位机,实现数据收发
查看>>
异步表单提交
查看>>
[洛谷U871]building
查看>>
次小生成树
查看>>
Redis在windows下安装过程
查看>>
ip转城市接口,ip转省份接口,ip转城市PHP方法
查看>>