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 <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <sstream>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 0x4ffffff;
15 const double EXP = 1E-5;
16 const LL mod = 1e9+7;
17 const int MS= 1E5+5;
18 const int MS2 = 1e4+1;
19
20 int a[MS];
21 int L[MS];
22 int R[MS];
23
24 int main()
25 {
26       int n;
27       while(scanf("%d",&n)!=EOF)
28       {
29             for(int i = 1;i<=n;i++)
30                   scanf("%d",&a[i]);
31             a[0] = 1;
32             a[n+1] = 1;
33
34             for(int i = 1;i<=n;i++)
35             {
36                   for(int j = i-1;j>=0;j--)
37                   {
38                         if(a[i] % a[j] == 0)
39                         {
40                               L[i] = j;
41                               break;
42                         }
43                   }
44
45                   for(int j = i+1;j<=n+1;j++)
46                   {
47                         if(a[i] % a[j] == 0)
48                         {
49                               R[i] = j;
50                               break;
51                         }
52                   }
53             }
54             LL ans=  0;
55             for(int i =1;i<=n;i++)
56             {
57                   ans += (i - L[i]) * (R[i] - i);
58                   ans %=mod;
59             }
60             printf("%lld\n",ans);
61       }
62       return 0;
63 }

优化: 由于每个整数不大于10000,我们可以预处理出每个数的约数。在处理L[i]和R[i]中,用pre数组来表示前面出现的整数。见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <sstream>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 0x4ffffff;
15 const double EXP = 1E-5;
16 const LL mod = 1e9+7;
17 const int MS= 1E5+5;
18 const int MS2 = 1e4+1;
19
20 int a[MS];
21 int L[MS];
22 int R[MS];
23 int pre[MS];
24 vector<int> vec[MS];
25
26 void init()
27 {
28       for(int i = 1;i<MS2;i++)
29       {
30             for(int j = i;j <MS2;j+=i)
31             {
32                   vec[j].push_back(i);
33             }
34       }
35 }
36
37 int main()
38 {
39       init();
40       int n;
41       while(scanf("%d",&n)!=EOF)
42       {
43             for(int i =1;i<=n;i++)
44                   scanf("%d",&a[i]);
45             memset(pre,0,sizeof(pre));
46             for(int i = 1;i<=n;i++)
47             {
48                   int t = 0;
49                   for(int j =0;j<vec[a[i]].size();j++)
50                   {
51                         if(pre[vec[a[i]][j]])
52                               t = max(t,pre[vec[a[i]][j]]);
53                   }
54                   L[i] = t;
55                   pre[a[i]] = i;
56             }
57
58             memset(pre,0,sizeof(pre));
59             for(int i = n;i>0;i--)
60             {
61                   int t = n+1;
62                   for(int j = 0;j<vec[a[i]].size();j++)
63                   {
64                         if(pre[vec[a[i]][j]])
65                         {
66                               t = min(t,pre[vec[a[i]][j]]);
67                         }
68                   }
69                   R[i] = t;
70                   pre[a[i]] = i;
71             }
72             LL ans= 0;
73             for(int i = 1;i<=n;i++)
74             {
75                   ans += (i - L[i]) * (R[i] - i);
76                   ans %= mod;
77             }
78             printf("%lld\n",ans);
79       }
80       return 0;
81 }
时间: 2024-10-10 01:27:28

OO’s Sequence的相关文章

[HDOJ5288]OO&#39;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 #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

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

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

【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