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

hdu 5288||2015多校联合第一场1001题

时间:2015-07-21 22:21:22      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.hdu.edu.cn/showproblem.php?pid=5288

Problem Description
OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there‘s no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know
i=1nj=inf(i,j) mod 109+7.

 

Input
There are multiple test cases. Please process till EOF.
In each test case: 
First line: an integer n(n<=10^5) indicating the size of array
Second line:contain n numbers ai(0<ai<=10000)
 

Output
For each tests: ouput a line contain a number ans.
 

Sample Input
5 1 2 3 4 5
 

Sample Output
23
/**
hdu 5288||2015多校联合第一场1001题 
题目大意:给定一个区间,找出f(i,j)的和
解题思路:http://blog.sina.com.cn/s/blog_15139f1a10102vnx5.html 和官方题解想的一样
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef __int64 LL;
const int MAXN = 1e5+10;
const LL MOD = 1e9+7;
int a[MAXN],l,r,n;
vector<int> f[10010];
vector<int> vec[10010];
typedef vector<int>::iterator it;

int main()
{
    for(int i=1; i<=10000; i++)
        for(int j=1; j*i<=10000; j++)
            f[i*j].push_back(i);
    while(~scanf("%d",&n))
    {
        for(int i=0; i<=10000; i++)
            vec[i].clear();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            vec[a[i]].push_back(i);
        }
        LL ans=0;
        for(int i=1; i<=n; i++)
        {
            l=1,r=n;
            for(it j=f[a[i]].begin(); j!=f[a[i]].end(); j++)
            {
                if(vec[*j].empty())continue;
                it t=lower_bound(vec[*j].begin(),vec[*j].end(),i);
                if(t==vec[*j].end())
                {
                    t--;
                    l=max((*t)+1,l);
                    continue;
                }
                if(*t==i)
                {
                    if(t!=vec[*j].begin())
                    {
                        t--;
                        l=max((*t)+1,l);
                        t++;
                    }
                    if(*t==i)t++;
                    if(t!=vec[*j].end())
                        r=min((*t)-1,r);
                }
                else
                {
                    r=min((*t)-1,r);
                    if(t!=vec[*j].begin())
                    {
                        t--;
                        l=max((*t)+1,l);
                    }
                }
            }
            ans = (ans + ((long long)(i - l+1) * (r - i+1) % MOD) ) % MOD;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 5288||2015多校联合第一场1001题

标签:

原文地址:http://blog.csdn.net/lvshubao1314/article/details/46991943

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