hdu 5288 (小技巧)
2015-07-24 16:34
465 查看
OO’s Sequence
Problem DescriptionOO 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
题目大意: 给出n个数,求在任意区间内,i<>j 并且i%j!=0 的i的个数
思 路: 利用pre数组存储每个数出现的序号
借用此从左到右,便利出每个数据的因子,找到在数据范围最右面的那个因子所在的序号
同理可知道左面的。
具体看代码:
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int mod = 1e9+7; #define ll long long #define N 100005 ll left ,right ; int n,num ,pre ; int main() { while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++){ scanf("%d",&num[i]); left[i]=1; right[i]=n; } memset(pre,0,sizeof(pre)); for(int i=1;i<=n;i++){ for(int j=num[i];j<=10000;j+=num[i]){ if(pre[j]&&right[pre[j]]==n) right[pre[j]]=i-1; } pre[num[i]]=i; } memset(pre,0,sizeof(pre)); for(int i=n;i>=1;i--){ for(int j=num[i];j<=10000;j+=num[i]){ if(pre[j]&&left[pre[j]]==1) left[pre[j]]=i+1; } pre[num[i]]=i; } long long ans=0; for(int i=1;i<=n;i++) ans = (ans%mod+(long long)(i-left[i]+1)*(right[i]-i+1)%mod)%mod; printf("%lld\n",ans); } return 0; }
相关文章推荐
- Java、HTML、CSS、JSP、JavaScript注释语法
- [LeetCode]Integer to Roman
- 核典型关联分析(KCCA)算法原理
- Linux网络编程之使用TCP传输文件
- 如何取得Spring管理的bean (请用第3种方法):
- 【源代码】java.util.Stack & Queue
- uva 10594 Data Flow (最小费最大流+题目给的数据有错)
- Codeforces Round #313 (Div. 2) C Gerald's Hexagon 计数
- HTML DOM submit() 方法
- jquery ajax备用
- (转)反转单链表的几种方法
- 统计难题
- Espresso——Android测试工具Espresso简介
- zoj1715
- android布局-GridLayout
- KEIL/IAR 与 Doxygen 快速上手 - 嵌入式编程的注释管理小技巧
- 聊聊并发——HashMap、HashTable及ConcurrentHashMap
- JFinal的设计思想和理念是什么?有哪些创新点?
- 照相机的曝光和白平衡
- Uniyt : 场景LoadLevel时资源和对象的处理流程