HDU4961-Boring Sum(质因子)

点击打开链接

题意:给出n个数的数列a,bi的取值为在1 <= j < i之间如果存在aj % ai == 0,则取最大下标的值赋给bi,如果不存在,则bi = ai;ci的取值为在i < j <= n之间如果存在aj % ai == 0,则取最小下标值赋给bi,如果不存在,则ci = ai。求b1 * c1 + b2 * c2 + ... + bn * cn的和。

思路:如果直接暴力的话一定会超时,所以我们可以开一个vis数组来记录每一个值所对应的最大的下标是多少。即每查找ai,分解出ai的质因子,更新vis数组。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

typedef __int64 ll;
//typedef long long ll;
const int MAXN = 100005;

int a[MAXN], b[MAXN], c[MAXN], vis[MAXN];
int n;

int main() {
    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]])
                b[i] = a[vis[a[i]]];
            else
                b[i] = a[i];
            for (int j = 1; j <= (int)sqrt((double)a[i]) + 1; j++) {
                if (a[i] % j == 0) {
                    vis[j] = i;
                    vis[a[i] / j] = i;
                }
            }
        }

        memset(vis, 0, sizeof(vis));
        for (int i = n; i >= 1; i--) {
            if (vis[a[i]])
                c[i] = a[vis[a[i]]];
            else
                c[i] = a[i];
            for (int j = 1; j <= (int)sqrt((double)a[i]) + 1; j++) {
                if (a[i] % j == 0) {
                    vis[j] = i;
                    vis[a[i] / j] = i;
                }
            }
        }

        ll sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += (ll)b[i] * c[i];
        }
        printf("%I64d\n", sum);
    }
    return 0;
}

HDU4961-Boring Sum(质因子),布布扣,bubuko.com

时间: 2024-10-10 16:21:06

HDU4961-Boring Sum(质因子)的相关文章

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 0

HDU-4961 Boring Sum STL模拟

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

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

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

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

uva 10780 Again Prime? No Time. 质因子乱搞

求最大的k   使得 m^k 能被n!整除 m^k就是让m的每个质因子个数增加了k倍,只要求得n!的质因子能让m增加多少倍就够了.当然这里要取增加倍数最少的. 木桶装水的量取决于最短的木板. 预处理2-n每个数的质因子情况,由于n有10000,前10000个素数有1000+个,所以维护前缀和不划算. case只有500 所以干脆每次都算一遍. #include<stdio.h> #include<string.h> #include<iostream> #include

poj3993Not So Flat After All(筛法素数+分解质因子)

题目链接: 啊哈哈,点我点我 题意: 题意是给出两个数字,然后有由一分解定理得,每个数可以分解成若干质因数的乘积,这样就可以在一个n维的坐标系下表示出这个点...比如给出50和24 因为24=2^3*3^1*5^0  而50=2^1*3^0*5^2那么这两个点就可以在一个3维德坐标系下表示出这两个点..24=(3,1,0)  50=(1,0,2)  那么共同拥有的维度就是3  而两个点在n维坐标系下的距离就是|3-1|+|1-0|+|0-2|=5,这样题意完全理解... 思路: 先筛出10000