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 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=1n∑j=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

field=problem&key=2015+Multi-University+Training+Contest+1&source=1&searchmode=source" style="color:rgb(26,92,200); text-decoration:none">2015 Multi-University Training Contest 1

Recommend

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

pid=5295" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5295

题意非常easy :求随意区间内满足条件的ai有多少个

假设依照题意 程序应该这样写:

四层循环 - - (不超时吃键盘)

尽管最后优化到 n^2/2也超时 数据太大了

#include<stdio.h>
int main()
{
    int n,i,j,k,kk,a[100050];
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1; i<=n; i++)
            scanf("%d",&a[i]);
        long long suma=0;
        for(i=1; i<=n; i++)
        {
            for(j=i; j<=n; j++)
            {

                for(k=i; k<=j; k++)//相当于i
                {
                    int flag=0;
                    for(kk=i; kk <= j ; kk++)//相当于j
                    {
                        if(a[k]%a[kk] == 0 && k!=kk)
                        {
                            flag=1;
                            break;
                        }
                    }
                    if(flag!=1)
                    {
                        suma++;
                        suma%=100000007;
                    }
                }
                printf("%d %d=%d %d=%d\n",i,j,a[i],a[j],suma);
            }
        }
        printf("%I64d\n",suma);
    }
}

脑洞大开:换个思路是不是题意求的是找那些区间能满足第ai个值存在呢?

也就是说看ai能提供几个答案

定义两个数组 l r 表示i数的左側和右側

最接近他的值且值是a[i]因子的数字的位置

那么第i个数字能提供的答案就是(r[i]-i) * (l[i]-i)

事实上这样的方法有漏洞 假设我给你 10w个1 程序就跪了 ╮(╯▽╰)╭  没有这数据 所以放心大胆的做吧

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int M = 10e5 + 5;
const long mod = 1e9+7;
int vis[M],a[M],l[M],r[M];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        memset(vis,0,sizeof(vis));

        for(int i = 1;i <= n; ++i)
        {
            scanf("%d",&a[i]);
            r[i] = n+1;
            for(int j = a[i];j <= 10000; j+=a[i]) //找到离他近期的因子
            {
                if(vis[j])
                {
                    r[vis[j]] = i;
                    vis[j] = 0;
                }
            }
            vis[a[i]] = i;
        }
        memset(vis,0,sizeof(vis));
        for(int i = n;i >= 1; --i)
        {
            for(int j = a[i];j <= 10000; j+=a[i])
            {
                if(vis[j])
                {
                    l[vis[j]] = i;
                    vis[j] = 0;
                }
            }
            vis[a[i]] = i;
        }

        long long ans = 0;
        for(int i = 1;i <= n; ++i)
        {
        ans = ((ans + (long long)(r[i]-i)*(long long)(i-l[i])%mod)%mod);
        }

        printf("%I64d\n",ans);
    }
}
时间: 2024-12-30 03:53:57

Hdu 5288 OO’s Sequence 2015多小联赛A题的相关文章

HDU 5288 OO&#39;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 nu

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(多校第一场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)

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

hdu 5288 OO’s Sequence(2015多校第一场第1题)枚举因子

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 题意:在闭区间[l,r]内有一个数a[i],a[i]不能整除 除去自身以外的其他的数,f(l,r)表示在这区间内a[i]这样的数的个数,,现给你n个数,求所有区间的f(l,r)的和. 思路:对于每个数a[i]求出他的左右侧最靠近他的且是他的因子的位置L.R,并记录,那么对于每个数a[i]都有了他的L,R,而对于每个a[i]在f(l,r)有价值的次数之和就是(i-L+1)*(R-i+1) 代码:

hdu 5288 OO’s Sequence(计数)

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=1n∑j=inf(i,j) mod (109+7). Input There are mul