HDU 4961 Boring Sum
来源:互联网 发布:php 做代理服务器登录 编辑:程序博客网 时间:2024/04/27 22:19
Boring Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 739 Accepted Submission(s): 363
Total Submission(s): 739 Accepted Submission(s): 363
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.
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.
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
51 4 2 3 90
Sample Output
136HintIn 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
2014 Multi-University Training Contest 9
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=4961
题目大意 :输入一个数列a[i],数列b[j]的组成为:如果在a[i]的前i-1项中有a[i]的倍数,取最接近i的a[i]值为b[i]值,如果没有那么b[i] = a[i]
题目分析 :左右扫一下算出b,c数组
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=4961
题目大意 :输入一个数列a[i],数列b[j]的组成为:如果在a[i]的前i-1项中有a[i]的倍数,取最接近i的a[i]值为b[i]值,如果没有那么b[i] = a[i]
数列c[i]的组成为:如果在a[i]的i+1到n项中有a[i]的倍数,取最接近i的a[i]值为c[i]值,如果没有那么c[i] = a[i]
题目分析 :左右扫一下算出b,c数组
#include <cstdio>#include <cstring>#define ll long longint const MAX = 1e5 + 10;int n, vis[MAX];ll a[MAX], b[MAX], c[MAX], ans;int main(){ while(scanf("%d", &n) != EOF && n) { ans = 0; memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) scanf("%I64d", &a[i]); vis[ a[1] ] = 1; for(int i = 2; i <= n; i++) { for(ll j = 1; j * j <= a[i]; j++) { if(a[i] % j) continue; if(vis[j]) { b[ vis[j] ] = a[i]; vis[j] = 0; } ll tmp = a[i] / j; if(vis[tmp]) { b[ vis[tmp] ] = a[i]; vis[tmp] = 0; } } vis[ a[i] ] = i; } for(int i = 1; i <= n; i++) if(b[i] == 0) b[i] = a[i]; memset(vis, 0, sizeof(vis)); vis[ a[n] ] = n; for(int i = n - 1; i >= 1; i--) { for(ll j = 1; j * j <= a[i]; j++) { if(a[i] % j) continue; if(vis[j]) { c[ vis[j] ] = a[i]; vis[j] = 0; } ll tmp = a[i] / j; if(vis[tmp]) { c[ vis[tmp] ] = a[i]; vis[tmp] = 0; } } vis[ a[i] ] = i; } for(int i = 1; i <= n; i++) if(c[i] == 0) c[i] = a[i]; for(int i = 1; i <= n; i++) ans += b[i] * c[i]; printf("%I64d\n",ans); }}
0 0
- HDU 4961 Boring Sum
- HDU - 4961 Boring Sum
- hdu 4961 Boring Sum
- hdu 4961 Boring Sum
- hdu 4961 Boring Sum
- HDU 4961 Boring Sum
- hdu 4961 Boring Sum
- HDU 4961 Boring Sum
- hdu 4961 Boring Sum 多校九
- hdu 4961 Boring Sum(高效)
- 【瞎搞】HDU 4961 Boring Sum
- HDU 4961 Boring Sum 构造题
- HDU 4961 Boring Sum(分解因子)
- hdu 4961 Boring Sum【构造题】
- hdu 4961 Boring Sum(数学题)
- HDU 4961 Boring Sum(构造题)
- HDU 4961 Boring Sum 因数分解
- HDU 4961 Boring Sum 打表、更新
- Extjs Grid翻页后再刷新无法回到第一页
- uva 474 - Heads / Tails Probability(递推)
- 单例模式
- android系统信息(内存、cpu、sd卡、电量、版本)获取
- openvpn(windows服务端+win/Linux客户端)使用总结
- HDU 4961 Boring Sum
- 数据库原理中的1NF,2NF,3NF区别和联系
- 【索引】Numerical Methods::Exercises: Intermediate
- POJ 3621-- Sightseeing Cows
- 日志分析:quickfix-logviewer
- Java学习网站
- 亲戚
- UI界面不显示
- Build Android Project with Gradle