HDU 5288 - OO’s Sequence (统计贡献)

Sep 5, 2015


## 题意 ##

给一个数组,问在每个区间内,对于一个数x,它不能整除任何一个该区间内的数。这样的数的总共个数。

## 思路 ##

统计每个数的贡献。

lft[i], rit[i]表示距离i位置的数左边和右边最近的约数。所以它的贡献就是(i-l[i])(r-l[i])。

维护那两个数组的时候有优化,只要记录一下每个数sqrt(n)之内的约数就行,另一个数直接除法求出。不然会TLE。

## 代码 ##

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <fstream>
#include <iterator>
#include <iostream>
#include <sstream>
#include <vector>
#include <bitset>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#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(p, num) memset(p, num, sizeof(p))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1e5 + 10;
const int MOD = 1000000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef std::pair<int, int> pii;
 
int pos[10000+10], arr[MAXN];
int vis[10000+10], lft[MAXN], rit[MAXN];
vector<int> fac[10010];
 
void init()
{
    for (int i = 1; i <= 10000; i++)
    {
        int m = sqrt(i+.5);
        for (int j = 1; j <= m; j++) if (i % j == 0)
            fac[i].PB(j);
    }
}
 
int main()
{
    //ROP;
    init();
    int n;
    while (~scanf("%d", &n))
    {
        MS(pos, 0);
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &arr[i]);
            lft[i] = 0, rit[i] = n+1;
            for (auto j : fac[arr[i]]) lft[i] = max(lft[i], max(pos[arr[i]/j], pos[j]));
            pos[arr[i]] = i;
        }
        LL ans = 0;
        for (int i = 1; i <= 10000; i++) pos[i] = n+1;
        for (int i = n; i >= 1; i--)
        {
            for (auto j : fac[arr[i]]) rit[i] = min(rit[i], min(pos[arr[i]/j], pos[j]));
            pos[arr[i]] = i;
            ans = (ans + (LL)(i-lft[i]) * (rit[i]-i)) % MOD;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}