分析
题目的意思就是要求不包含某些数对(坏对)连续的区间个数。
所以我们就应该先考虑如何快速找到一个坏对。
我们从后向前枚举,
一开始设右端点为n,枚举到一个数,我们用
n−−√
的时间来枚举这个数的约数,
通过一个桶来判断与这个数形成坏对的位置,
之后改变右端点。
总的时间复杂度
O(nn−−√)
code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define N 1000003
#define ll long long
using namespace std;
int n,k,a[N],l,r,h[N],t,mx,w;
ll ans;
char ch;
void write(ll x)
{
if(x>9) write(x/10);
putchar(x%10+'0');
}
void read(int &n)
{
n=0;
ch=getchar();
while((ch<'0' || ch>'9') && ch!='-')ch=getchar();
int w=1;
if(ch=='-')w=-1,ch=getchar();
while('0'<=ch && ch<='9')n=n*10+ch-'0',ch=getchar();
n*=w;
}
int main()
{
freopen("drink.in","r",stdin);
freopen("drink.out","w",stdout);
memset(h,127,sizeof(h));
read(n);read(k);
for(int i=1;i<=n;i++)read(a[i]);
r=n;
for(int i=n;i;i--)
{
t=a[i]-k;
if(t>=0)
{
for(int j=1;j<=sqrt(t);j++)
if(t%j==0)
{
if(j>k && h[j]<=n)r=min(r,h[j]);
if(t/j>k && h[t/j]<=n)r=min(r,h[t/j]);
}
if(t==0 && mx>k)r=min(w-1,r);
}
h[a[i]]=i-1;
if(a[i]>k)
{
mx=a[i];
w=i;
}
ans=ans+r-i+1;
}
write(ans);
}