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 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)
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
/** hdu 5288||2015多校联合第一场1001题 题目大意:给定一个区间。找出f(i,j)的和 解题思路:http://blog.sina.com.cn/s/blog_15139f1a10102vnx5.html 和官方题解想的一样 */ #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; typedef __int64 LL; const int MAXN = 1e5+10; const LL MOD = 1e9+7; int a[MAXN],l,r,n; vector<int> f[10010]; vector<int> vec[10010]; typedef vector<int>::iterator it; int main() { for(int i=1; i<=10000; i++) for(int j=1; j*i<=10000; j++) f[i*j].push_back(i); while(~scanf("%d",&n)) { for(int i=0; i<=10000; i++) vec[i].clear(); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); vec[a[i]].push_back(i); } LL ans=0; for(int i=1; i<=n; i++) { l=1,r=n; for(it j=f[a[i]].begin(); j!=f[a[i]].end(); j++) { if(vec[*j].empty())continue; it t=lower_bound(vec[*j].begin(),vec[*j].end(),i); if(t==vec[*j].end()) { t--; l=max((*t)+1,l); continue; } if(*t==i) { if(t!=vec[*j].begin()) { t--; l=max((*t)+1,l); t++; } if(*t==i)t++; if(t!=vec[*j].end()) r=min((*t)-1,r); } else { r=min((*t)-1,r); if(t!=vec[*j].begin()) { t--; l=max((*t)+1,l); } } } ans = (ans + ((long long)(i - l+1) * (r - i+1) % MOD) ) % MOD; } printf("%I64d ",ans); } return 0; }