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;
    }
    
    
  • 相关阅读:
    手机号码正则,座机正则,400正则
    Win10 开始运行不保存历史记录原因和解决方法
    Ubuntu 普通用户无法启动Google chrome
    在win10 64位系统安装 lxml (Python 3.5)
    SecureCRT窗口输出代码关键字高亮设置
    【转】win2008 中iis7设置404页面但返回状态200的问题解决办法
    ionic app开发遇到的问题
    Ubuntu 创建文件夹快捷方式
    Ubuntu配置PATH环境变量
    Ubuntu 升级python到3.7
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8325138.html
Copyright © 2011-2022 走看看