hdu5211Mutiple+数论

Description
WLD likes playing with a sequence . One day he is playing with a sequence of integers. For every index i, WLD wants to find the smallest index ( if exists ), that , and mod = 0. If there is no such an index , we set as 0.

Input
There are Multiple Cases.(At MOST )

For each case:

The first line contains one integers .

The second line contains integers ,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 for all

Sample Input

4
1 3 2 4

Sample Output

6

Hint

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

wld有一个序列a[1..n], 对于每个1≤i < n, 他希望你求出一个最小的j(以后用记号F(i)表示),满足i< j≤ n, 使aj为ai的倍数(即aj mod ai=0),若不存在这样的j,那么此时令F(i) = 0
做法1:直接(胆战心惊)暴力枚举,AC-826ms
做法2:从后往前扫,记录下位置,然后枚举a[i]的倍数,找到最小位置。AC-31ms
做法1:826ms

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f

int a[10005];
int At[10005];
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        int ans=0;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(a[j]%a[i]==0){
                    ans+=j;
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

做法2:31ms

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f

int a[10005];
int At[10005];
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=10005;i++) At[i]=inf;
        //cout<<At[1]<<endl;
        int ans=0;
        for(int i=n;i>=1;i--){
            int temp=inf;
            for(int j=a[i];j<10004;j+=a[i]) temp=min(temp,At[j]);
            if(temp!=inf) ans+=temp;
            At[a[i]]=i;
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值