Description
Input
第一行n和k
第二行数组a
Output
输出一个整数表示答案
Sample Input
3 2
5 3 1
Sample Output
4
Solution
直接暴力!
枚举一个数的约数是根号级别的,那么用桶记录着,直接枚举每个数的约数
算答案时枚举右界,可以根据约束算出左界,然后统计答案即可
Code
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define N 101000
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define ll long long
using namespace std;
int n,K,a[N],mx=0,t[N],last[N],next[N],to[N],tot=0;
void putin(int x,int y)
{
next[++tot]=last[x];last[x]=tot;to[tot]=y;
}
int main()
{
freopen("drink.in","r",stdin);
freopen("drink.out","w",stdout);
scanf("%d%d",&n,&K);
fo(i,1,n) scanf("%d",&a[i]),mx=max(mx,a[i]);
memset(t,127,sizeof(t));
int wl=n+1;
fd(i,n,1)
{
int m=sqrt(a[i]),b=a[i]-K,mi=n+1;
if(b==0) if(wl<=n) putin(wl,i);
if(b<=0) continue;
fo(j,1,m)
if(b%j==0)
{
if(j!=1||K==0) mi=min(mi,t[j]);
mi=min(mi,t[b/j]);
}
if(mi<=n) putin(mi,i);
t[a[i]]=i;wl=i;
}
ll i=1,ans=0;
for(ll j=1;j<=n;j++)
{
for(int k=last[j];k;k=next[k]) i=max(i,(ll)to[k]+1);
ans+=j-i+1;
}
printf("%lld",ans);
}