hdu 5288

       题意:给n个数,让你输出所有的f(l,r)之和,1<=l<r<=n,f(i,j)表示区间[i,j]内有多少个不能被其它数整除的数。

       分析:换个方向思考问题,实质上是在求对第i个数,有多少个区间使得它不能被其它数整除。这样,最后只要把所有数的满足条件的区间数加起来就行了。为此,我们可以开两个数组l[i],r[i],l[i]记录左边离第i个数最近的且是其因子的数的位置,r[i]则记录右边。于是,第i个数满足条件的区间数就为(i-l[i])*(r[i]-i)。

   代码如下:

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lrt rt << 1
#define rrt rt << 1|1
#define root 1,m,1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF=1e9;
using namespace std;
const double eps = 1e-5;
const int MAXN = 300 + 10;
const int MOD = 1e9+7;
const int M=3100;
const int N=101000;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
int n,m,a[N];
LL l[N],r[N],hs[N];
void init()
{
    int i,j;
    for (i=1;i<=n;i++) {  // get l[i]
        int t=sqrt(a[i]);
        for (j=t;j>=1;j--) if (a[i]%j==0) {   // j是a[i]的因子
            if (hs[j]) {               //  j在前面的数里是否出现过
                l[i]=max(l[i],hs[j]);   // 注意,hs[j]这里记录的是j出现的离i最近的位置,即使有多个相同的j时
            }
            if (hs[a[i]/j]) {
                l[i]=max(l[i],hs[a[i]/j]);
            }
        }
        hs[a[i]]=i;
    }
    MS(hs,0);
    for (i=n;i>0;i--) {  // get r[i]
        int t=sqrt(a[i]);
        for (j=t;j>0;j--) if (a[i]%j==0) {   // j是a[i]的因子
            if (hs[j]) {
                r[i]=min(r[i],hs[j]);
            }
            if (hs[a[i]/j]) {
                r[i]=min(r[i],hs[a[i]/j]);
            }
        }
        hs[a[i]]=i;
    }
}
int main()
{
    LL i,j;
    while(~scanf("%d",&n))
    {
        MS(hs,0);
        for (i=1;i<=n;i++) {
            scanf("%d",a+i);
            l[i]=0; r[i]=n+1;
        }
        init();
        LL ans=0;
        for (i=1;i<=n;i++) {
            ans=(ans+(i-l[i])%MOD*(r[i]-i)%MOD)%MOD;  //  这里写成  <span style="font-family: Arial, Helvetica, sans-serif;">ans=((i-l[i])%MOD*(r[i]-i)%MOD)%MOD;  wrong了好多次,悲剧(>﹏<)</span>
        }
        printf("%lld\n",ans);
    }
}

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值