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

多校I 1001

时间:2015-07-22 16:10:41      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:acm   hdu   

OO’s Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 888    Accepted Submission(s): 335


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
 

Author
FZUACM
 

Source
 

Recommend
We have carefully selected several similar problems for you:  5299 5298 5297 5296 5295 
 

#include <cstdio>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;
#define maxn 100000 + 10
#define MOD 1000000007

int a[maxn], n;
int ans;
vector<int> m[maxn];

///思路: 某个数存在于多少个区间与多少个区间含有这个数是等价的

void solve()
{
    ans = 0;
    for(int i = 1; i <= n; i++)
    {
        int l = 0, r = n + 1;
        int t = sqrt(a[i] + 0.5);
        for(int j = 1; j <= t; j++)
        {
            if(a[i] % j == 0) ///因子
            {
                for(int k=0; k<m[j].size(); k++)
                {
                    if(m[j][k] < i) l = max(m[j][k], l);
                    else if(m[j][k] > i) r = min(m[j][k], r);
                }
                int tmp = a[i] / j;
                if(j != a[i] / j)
                    for(int k=0; k<m[tmp].size(); k++)
                {
                    if(m[tmp][k] < i) l = max(m[tmp][k], l);
                    else if(m[tmp][k] > i) r = min(m[tmp][k], r);
                }
            }
        }
        ans = (ans + (i - l) * (r - i)) % MOD;
    }
    printf("%d\n", ans);
}

int main()
{
    while(~scanf("%d", &n))
    {
        for(int i=1; i<maxn; i++) m[i].clear();
        for(int i=1; i<=n; i++)
        {
            scanf("%d", &a[i]);
            m[a[i]].push_back(i);
        }
        solve();
    }
}

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

多校I 1001

标签:acm   hdu   

原文地址:http://blog.csdn.net/dojintian/article/details/47004147

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