hdu4961 Boring Sum

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:16777216")
#define eps 1e-6
#define ll long long
using namespace std;

const int maxn=100010;
vector<int> v[maxn];
int a[maxn],b[maxn],s[maxn],h[maxn];

int main()
{
    int i,j,n;
    for(i=1;i<maxn;i++)//预处理每个数的约数 注意这里不能取等 不然超了
        for(j=i;j<maxn;j+=i)
            v[j].push_back(i);
    while(~scanf("%d",&n)&&n)
    {
        for(i=1;i<=n;i++)
            scanf("%d",s+i);
        memset(h,-1,sizeof h);
        for(i=1;i<=n;i++)
        {
            if(h[s[i]]!=-1)
                b[i]=s[h[s[i]]];
            else b[i]=s[i];
            for(j=0;j<v[s[i]].size();j++)
                h[v[s[i]][j]]=i;
        }

        memset(h,-1,sizeof h);
        for(i=n;i>0;i--)
        {
            if(h[s[i]]!=-1)
                a[i]=s[h[s[i]]];
            else a[i]=s[i];
            for(j=0;j<v[s[i]].size();j++)
                h[v[s[i]][j]]=i;
        }
        ll ans=0;
        for(i=1;i<=n;i++)
            ans+=(ll)a[i]*b[i];
        cout<<ans<<endl;
    }
    return 0;
}

hdu4961 Boring Sum,布布扣,bubuko.com

时间: 2024-10-20 23:07:53

hdu4961 Boring Sum的相关文章

HDU-4961 Boring Sum STL模拟

给出一个序列A,对于A里面的每个元素,左边最近的能被它整除的元素记为B序列对应位置的,右边最近的是它的整数倍的元素记为C序列对应位置,找不到的记它本身,最后算出对应位置B*C的总和. 容器模拟,按顺序扫一遍,每次如果有符合条件的取出来,即为最近的.最后把它的下标放到对应位置的容器中,然后倒序求一遍,最后求和. #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #

HDU4961:Boring Sum

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

Boring Sum(hdu4961)hash

Boring Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 814 Accepted Submission(s): 390 Problem Description Number theory is interesting, while this problem is boring. Here is the problem. Giv

HDU - 4961 Boring Sum

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

hdu 4961 Boring Sum(高效)

题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; 构造bi, k=max(j|0<j<i,aj%ai=0), bi=ak; 构造ci, k=min(j|i<j≤n,aj%ai=0), ci=ak; 求∑i=1nbi?ci 解题思路:因为ai≤105,所以预先处理好每个数的因子,然后在处理bi,ci数组的时候,每次遍历一个数,就将其所有的因子更新,对于bi维护最大值,对于ci维护最小值. #include <cstdio> #include <c

HDU 4961 Boring Sum 暴力

题意:对于所有的A[I],同时找到左边和右边离它最近且是它的倍数的数相乘最后加起来求和. 解题思路:n*sqrt(n)的算法,开始以为过不了,wa了两发因为lld I64d对拍一个小时发现一个小时前交的代码没错只是没变I64d,..具体思路是枚举每个a[i]的因子,找离它最近的那个更新,如果已经没更新就不用更新了.用两个辅助数组记录最近的就行. 解题代码: 1 // File Name: 1002.cpp 2 // Author: darkdream 3 // Created Time: 201

HDU 4961 Boring Sum 构造题

用一个数组c, c[i]表示i这个数出现的最近数字是几. 那么当加入一个6,则 c[1] = c[2] = c[3] = c[6] = 6; ==最近怎么都要开挂啊.. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 100005; inl

hdu 4961 Boring Sum(数学题)

题目链接: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

HDU 4961(杭电多校#9 1002题)Boring Sum(瞎搞)

题目地址:HDU 4961 看来这题的测试数据是随机的.不然出了极限数据还真过不了...这题我的方法是建一个哈希结构体,记录两个变量,分别是num位置,然后是f,f==0表示这个数没出现过,f==1表示这个数出现过.然后分别从前面和后面扫一遍.每次扫的时候,对每一个出现的数都进行标记.然后对当前的数枚举该数的倍数,全部枚举完,取位置num最大的.然后找完之后,对哈希结构体进行更新.如果前面曾经出现过的话,就直接换掉,因为后面的数总比前面的更优.最后扫完两遍之后两个数组就能求出来了.计算就行了.