Title Description
Olivier and snow Lazard drinking.
Two people even drink $ 18 $ bottle, Olivier finally fell.
Olivier after taking the drug research sober church, because taking too much produce side effects, could not sleep the next day.
He had spent time with a boring way of counting, but after all he is a prince, is not counting sheep. He will count resolve this issue facet:
he wrote a first length $ n $ array $ a $. Count the array of $ \ {a_x, a_y \} $ is called a bad pair if and only if $ x <y $ and $ a_x \ mod a_y = K $ . So how many consecutive sub-array does not contain bad for it?
Input Format
Line contains two integers, $ n $ and $ K $.
The second line contains n-$ $ integers, represents an array $ a $.
Output Format
Output row contains the answer.
Sample
Sample input:
3 2
5 3 1
Sample output:
4
Data range and tips
Sample explained:
$ \ {5,3 \} $ is the array only a bad pair.
data range:
For $ 20 \% $ data, $ 1 \ leqslant n \ leqslant 100 $.
For another $ 30 \% $ data, $ K = 0 $.
For $ 100 \% $ data, $ 1 \ leqslant n \ leqslant {10} ^ 5,0 \ leqslant K \ leqslant {10} ^ 5,1 \ leqslant a_i \ leqslant {10} ^ 5 $
answer
It found that for a left point, right point their legitimate interval must be a continuous segment, but can not be intermittent.
So we consider to safeguard the legitimate range.
$a_x\mod a_y=K$即为$(a_x-K)\mod a_y=0$。
Then we can be $ a_x-K $ decomposition of the quality factor can be.
Time complexity: $ \ Theta (n) $.
Expectations score: $ 100 $ points.
Actual score: $ 100 $ points.
Code time
#include<bits/stdc++.h>
using namespace std;
long long n,K,ans;
long long a[100001];
long long cnt[100001];
int main()
{
memset(cnt,0x3f,sizeof(cnt));
scanf("%lld%lld",&n,&K);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
long long lft=n,rht=n+1;
for(int i=n;i;i--)
{
if(a[i]==K)rht=min(lft,rht);
else
for(int j=1;j*j<=a[i]-K;j++)
{
if((a[i]-K)%j)continue;
if(j>K)rht=min(cnt[j],rht);
if((a[i]-K)/j>K)rht=min(cnt[(a[i]-K)/j],rht);
}
if(a[i]>K)lft=i;
cnt[a[i]]=i;
ans+=rht-i;
}
printf("%lld",ans);
return 0;
}
rp ++