HDU 5288 OO's sequence (2015多校第一场 二分查找)

OO’s Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory
Limit: 131072/131072 K (Java/Others)

Total Submission(s): 955    Accepted Submission(s): 358

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

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

2015 Multi-University Training Contest 1

解题思路:

预处理出所有数的因数并且保存每个数出现的位置,对于每个A[i], 找最小的区间使得该区间内不包含A[i]的因数,通过向左向右进行两次二分查找来实现。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const int MAXN = 100000 + 10;
const int MOD = 1e9 + 7;
vector<int>G[MAXN];
vector<int>p[MAXN];
vector<int>rp[MAXN];
vector<int>M[MAXN];
int N;
int A[MAXN];
int main()
{
    for(int i=1; i<=10000; i++)
    {
        for(int j=i; j<=10000; j+=i)
            G[j].push_back(i);
    }
    while(scanf("%d", &N)!=EOF)
    {
        for(int i=1;i<=N;i++) p[i].clear();
        for(int i=1;i<=N;i++) rp[i].clear();
        for(int i=1; i<=N; i++)
            scanf("%d", &A[i]);
        for(int i=1; i<=N; i++)
        {
            p[A[i]].push_back(i);
        }
        for(int i=N;i>=1;i--)
        {
            rp[A[i]].push_back(-i);
        }
        long long ans = 0;
        for(int i=1; i<=N; i++)
        {
            int L = -1, R = N + 1;
            for(int j=0; j<G[A[i]].size(); j++)
            {
                int x = G[A[i]][j];
                int r = upper_bound(p[x].begin(), p[x].end(), i) - p[x].begin();
                int l = upper_bound(rp[x].begin(), rp[x].end(), -i) - rp[x].begin();
                if(r >= p[x].size()) r = N  + 1;
                else r = p[x][r];
                //cout << x << ": " << l << ' '<< r << endl;
                if(l >= rp[x].size()) l = 0;
                else l = -(rp[x][l]);
                //cout << x << ": " << l << ' '<< r << endl;
                L = max(l, L);
                R = min(R, r);
            }
            if(R == 0) R = N + 1;
            if(L < 0) L = 0;
            //cout << L << ' ' << R << endl;
            ans = (ans + (long long)(i - L) * (long long)(R - i)) % MOD;
        }
        printf("%I64d\n", ans );
    }
    return 0;
}

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

HDU 5288 OO's sequence (2015多校第一场 二分查找)

时间: 2024-10-27 03:32:24

HDU 5288 OO's sequence (2015多校第一场 二分查找)的相关文章

HDU 5288 OO’s Sequence(多校第一场1001)

OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 900    Accepted Submission(s): 339 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the nu

Hdu 5288 OO’s Sequence 2015多小联赛A题

OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1751    Accepted Submission(s): 632 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the n

HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 627    Accepted Submission(s): 318 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fr

hdu 5294 Tricks Device(2015多校第一场第7题)最大流+最短路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意:给你n个墓室,m条路径,一个人在1号墓室(起点),另一个人在n号墓室(终点),起点的那个人只有通过最短路径才能追上终点的那个人,而终点的那个人能切断任意路径. 第一问--终点那人要使起点那人不能追上的情况下可以切的最少的路径数,输出最少的路径数 第二问--起点那人能追上终点那人的情况下,终点那人能切断的最多的路径数,输出最多的路径数 思路:要使起点那人无法追上,只要使他的最短路径不存在就

HDU 5288 OO’s Sequence (暴力枚举因子)

题目链接:HDU 5288 OO's Sequence 题意:给出一个n,表示n个数的序列,函数f(l,r)定义,在l,r区间中存在多少个数,不能被其他数整除.求累加所有子区间的函数值 思路:从ai的小范围入手 1.a不能被b整除,即a的所有因子中不存在b,所以打表枚举所有的数的因子. 2.找到一个数(位置为i)满足条件时最左端l和最右端r,(i-l)*(r-i)就是对答案的贡献. AC代码: #include <stdio.h> #include <algorithm> #inc

hdu 5288 OO’s Sequence 分解因子

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 588    Accepted Submission(s): 209 Problem Description OO has got a array A of

HDU 5288 OO‘s sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 题面: OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 985    Accepted Submission(s): 375 Problem Description OO has got a array

hdu 5288 OO’s Sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 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=1->n)∑(j=i

HDU 5288 OO’s Sequence(数学啊 多校2015)

题目链接: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 kn