您的位置:首页 > 产品设计 > UI/UE

hdu 5288 OO’s Sequence

2015-10-10 14:45 363 查看


OO’s Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 2988    Accepted Submission(s): 1062


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

 

求所有子区间内  不被该区间内其他数整除的数的个数

标记    计算出每一个数能作为f()函数的解能在的最大区间  就能知道每一个数成为解的次数

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int has[100100];
int l[100100],r[100100];
int a[100100];
const int Mod=1e9+7;

void Factor(int n,int j)  //找左边的第一个因子
{
l[j]=max(has
,has[1]);
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
l[j]=max(l[j],has[i]);
l[j]=max(l[j],has[n/i]);
}
}
return ;
}

void factor(int n,int j)  //找右边的第一个因子
{
r[j]=min(has
,has[1]);
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
r[j]=min(r[j],has[i]),r[j]=min(r[j],has[n/i]);
}
return ;
}

int main()
{
int n;
while(~scanf("%d",&n))
{
memset(l,0,sizeof(l));
memset(has,0,sizeof(has));  //找左边因子时  所有数初始位置为0
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
Factor(a[i],i);
has[a[i]]=i;
}
for(int i=0;i<=n;i++) r[i]=n+1;
for(int i=0;i<=100000;i++) has[i]=n+1;  //找右边因子时  所有数初位置为n+1
for(int i=n;i>0;i--)
{
factor(a[i],i);
has[a[i]]=i;
}

int sum=0;

for(int i=1;i<=n;i++)
{
sum=(sum+(i-l[i])*(r[i]-i)%Mod)%Mod;  //计算每个数成为解的次数
}
printf("%d\n",sum);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: