标签:
Output
Sample Input
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #define LL long long #define MAXN 100010 using namespace std; const LL MOD = 1e9 + 7; int a[MAXN]; LL l[MAXN], r[MAXN], pre[MAXN], last[MAXN]; int main() { int n; while(~scanf("%d", &n)) { for(int i = 1; i <= n; ++i) { scanf("%d", a + i); l[i] = 0, r[i] = n+1; } memset(pre, 0, sizeof(pre)); memset(last, 0, sizeof(last)); for(int i = 1; i <= n; ++i) { for(int j = a[i]; j <= 10000; j += a[i]) {
//通过这个循环,如果a[i]是之前一个数的因子,一定会在后边遇到 if(pre[j] != 0 && r[pre[j]] == n+1) {
//这个数字 j (j = x * a[i])之前已经出现,且右边界是最右端 r[pre[j]] = i;
//这时更新pre[j]的右端, pre[j] 表示的是 j 最后出现的位置 } } pre[a[i]] = i; //当前pre[a[i]] 最后出现的位置是 i } for(int i = n; i > 0; --i) { for(int j = a[i]; j <= 10000; j += a[i]) { if(last[j] != 0 && l[last[j]] == 0) { l[last[j]] = i; } } last[a[i]] = i; } LL ans = 0; for(int i = 1; i <= n; ++i) { ans = (LL) (ans % MOD + (LL)(((i-l[i])*(r[i]-i)%MOD)%MOD)); ans %= MOD; } cout << ans << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/ya-cpp/p/4667917.html