HDU 5211 Mutiple

Description

WLD likes playing with a sequence $a[1..N]$. One day he is playing with a sequence of $N$ integers. For every index i, WLD wants to find the smallest index $F(i)$ ( if exists ), that $i < F(i) \leq n$, and $a_F(i)$ mod $a_i$ = 0. If there is no such an index $F(i)$, we set $F(i)$ as 0.
 

Input

There are Multiple Cases.(At MOST $10$) 

For each case: 

The first line contains one integers $N (1 \leq N \leq 10000)$. 

The second line contains $N$ integers $a1,a2,...,aN(1 \leq ai \leq 10000)$,denoting the sequence WLD plays with. You can assume that all ai is distinct.
 

Output

For each case: 

Print one integer.It denotes the sum of all $F(i)$ for all $1 \leq i < n$
 

Sample Input

    
    
4 1 3 2 4
 

Sample Output

    
    
6

Hint

F(1)=2 F(2)=0 F(3)=4 F(4)=0 
 

有 a[1-N]    如果满足 i<F(i)≤n,aF(i) mod ai = 0  取最小的Fi 累加即可

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f


int a[10050];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        for(int i=1; i<=n; i++)//a[1-N]
            scanf("%d",&a[i]);
        int sum=0;
        for(int i=1; i<n; i++)
        {
            int MIN=INF;
            for(int j=i+1; j<=n; j++)//i<F(i)≤n,
            {
                if(a[j]%a[i]==0&&MIN>j)  MIN=j; //aF(i) mod ai = 0. or=0 取最小的Fi
            }
            if(MIN!=INF)  sum+=MIN;
        }
        printf("%d\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值