zoukankan      html  css  js  c++  java
  • HDOJ 4961 Boring Sum

    Discription
    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<= 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.



    预处理一下每个数的约数,直接暴力做就行了。
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    #define ll long long
    #define maxn 100005
    #define pb push_back
    using namespace std;
    ll tot=0;
    vector<int> son[maxn];
    int n,m,a[maxn],f[maxn];
    int mult[maxn],g[maxn],to;
    
    inline void init(){
        for(int i=1;i<=100000;i++)
            for(int j=i;j<=100000;j+=i) son[j].pb(i);
    }
    
    int main(){
        init();
        
        while(scanf("%d",&n)==1&&n){
            memset(mult,0,sizeof(mult));
            for(int i=1;i<=n;i++){
                scanf("%d",a+i);
                f[i]=mult[a[i]];
                if(!f[i]) f[i]=i;
                for(int j=son[a[i]].size()-1;j>=0;j--){
                    to=son[a[i]][j];
                    mult[to]=max(mult[to],i);
                }
            }
    
            memset(mult,0x3f,sizeof(mult));
            for(int i=n;i;i--){
                g[i]=mult[a[i]];
                if(g[i]==mult[0]) g[i]=i;
                for(int j=son[a[i]].size()-1;j>=0;j--){
                    to=son[a[i]][j];
                    mult[to]=min(mult[to],i);
                }            
            }
            
            tot=0;
            for(int i=1;i<=n;i++) tot+=(ll)a[f[i]]*(ll)a[g[i]];
            printf("%lld
    ",tot);
        }
        
        return 0;
    }
    
    
  • 相关阅读:
    Linux--sed命令
    Linux--cut命令
    Android--aapt命令
    Shell--基础知识
    Linux--vim编辑器和文件恢复
    Linux--基本命令
    Linux--添加用户
    Linux--网络配置
    SpringCloud--Ribbon负载均衡
    第一阶段冲刺4
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8325138.html
Copyright © 2011-2022 走看看