码迷,mamicode.com
首页 > 其他好文 > 详细

Mutiple HDU5211(筛法)

时间:2015-04-28 09:50:22      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

 
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 221 Accepted Submission(s): 156


Problem 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)≤n, and aF(i) mod ai = 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≤N≤10000).

The second line contains N integers a1,a2,...,aN(1≤ai≤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≤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

筛法的应用

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cstdlib>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return 0;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)&&read(y);
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)&&read(y)&&read(z);
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=1e4+10;
const double inf=99999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<=(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod  100007

int num[maxn];
int pos[maxn];
int a[maxn];
int main()
{
    int n,m;
    while(read(n))
    {
        memset(num,0,sizeof(num));
        memset(pos,0,sizeof(pos));
        int mx=0;
        For(i,1,n)
        {
            int tmp;
            read(tmp);
            a[i]=tmp;
            num[tmp]++;//记录是否有这个数
            pos[tmp]=i;//记录这个数的位置
            mx=max(mx,tmp);//最大的值,查找的边界
        }
        int ans=0;

        for(int i=1;i<=n;i++)
        {

            int mn=inf;
            for(int j=a[i]+a[i];j<=mx;j+=a[i])
            {

                if(num[j]&&pos[a[i]]<pos[j]&&pos[j]<=n)
                {
                    mn=min(mn,pos[j]);
                }
            }
            if(mn!=inf)ans+=mn;
        }


        writeln(ans);
    }
    return 0;
}




Mutiple HDU5211(筛法)

标签:

原文地址:http://blog.csdn.net/u013167299/article/details/45317481

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!