[HDOJ5288]OO's Sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288

转换成求从1到n可以整除A[i]的区间的数量。
找离最近的A[i]最近的可以整除A[i]的l和r,因为再往左或者右必然包含l和r
假如[l,i]有x个数字,[i,r]有y个数字,那么A[i]的贡献即(x+1)*(y+1)
求A[i]对ans的贡献综合
即求离A[i]最近的可以被A[i]整除的数

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <stack>
10 #include <list>
11 #include <vector>
12
13 using namespace std;
14
15 inline int max(int a, int b) {
16     return a > b ? a : b;
17 }
18
19 const int maxn = 100010;
20 const int mod = 1000000007;
21 int a[maxn];
22 int s[maxn];
23 int l[maxn];
24 int r[maxn];
25 int n;
26 long long ans;
27
28 void left() {
29     int m = 0;
30     memset(s, 0, sizeof(s));
31     for(int i = n; i >= 1 ; i--) {
32         m = max(m, a[i]);
33         for(int j = a[i]; j <= m; j+=a[i]) {
34             if(s[j] && l[s[j]] < i) {
35                 l[s[j]] = i;
36             }
37             s[a[i]] = i;
38         }
39     }
40 }
41
42 void right() {
43     int m = 0;
44     memset(s, 0, sizeof(s));
45     for(int i = 1; i <= n; i++) {
46         m = max(m, a[i]);
47         for(int j = a[i]; j <= m; j+=a[i]) {
48             if(s[j] && r[s[j]] > i) {
49                 r[s[j]] = i;
50             }
51             s[a[i]] = i;
52         }
53     }
54 }
55 int main() {
56     freopen("in", "r", stdin);
57     while(~scanf("%d", &n)) {
58         ans = 0;
59         for(int i = 1; i <= n; i++) {
60             scanf("%d", &a[i]);
61             l[i] = 0;
62             r[i] = n + 1;
63         }
64         right();
65         left();
66         // for(int i = 1; i <= n; i++) {
67         //     printf("%d %d %d\n", l[i], a[i], r[i]);
68         // }
69         for(int i = 1; i <= n; i++) {
70             ans = (ans + ((i - l[i]) % mod * (r[i] - i)) % mod) % mod;
71         }
72         printf("%I64d\n", ans);
73     }
74 }

[HDOJ5288]OO's Sequence

时间: 2024-10-10 10:19:04

[HDOJ5288]OO's Sequence的相关文章

HDOJ 5288 OO’s Sequence 水

预处理出每个数字的左右两边可以整除它的最近的数的位置 OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1880    Accepted Submission(s): 672 Problem Description OO has got a array A of size n ,defined a func

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

hdu5288(2015多校1)OO’s Sequence

OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 353    Accepted Submission(s): 117 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 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

OO’s Sequence

OO’s Sequence 题目抽象:给你一个整数序列.  对于它的每个非空的子集区间,有多少个整数不能整除其它数.求每个非空子集区间的这些数的总和. 分析:用L[i],R[i]表示a[i]中最近的约数. 超时代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include

【HDOJ 5288】OO’s Sequence

[HDOJ 5288]OO's Sequence 枚举 题目给了个函数f(l,r) 求区间[l,r]有多少个数满足区间内任何一个数都不为他的约数 算出i [1,n] 每个位置的数的最大满足范围 即该位置的数对结果的贡献 两个函数 l[] r[] 存储每个位置的数贡献范围 譬如: 5 4 3 2 1 2 4 五个数的贡献范围分别为 (0,2) (0,4) (0,4) (3,6) (4,6) 计算区间范围 开一个数组pre[]存放遍历到此时每个数字在之前出现的位置 i(1->n) 每遍历一个数 更新

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&#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