题解:
对于 a,b两个数组,枚举两个间隔,设一个左指针和一个右指针,求出对于每一个右指针最远的左指针即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+10;
ll fast(ll x,int p){
ll ans = 1;
for(int i=1;i<=p;i++) ans*=x;
return ans;
}
ll solve(int *a,int *b,int n,int p,ll V,int f){
ll ans = 0;
for(int len=f;len<n;len++){
ll sum = 0;
int l = 1,i = 1,j = i+len;
while(j<=n){
sum += fast(abs(a[i]-b[j]),p);
if(sum>V){
while(sum>V){
sum-=fast(abs(a[l]-b[l+len]),p);
l++;
}
}
ans += i - l + 1;
i++,j++;
}
// printf("len = %d ans = %lld
", len,ans);
}
return ans;
}
int a[maxn],b[maxn];
int main(){
int T;
scanf("%d",&T);
while(T--){
ll n,V,p;
scanf("%lld%lld%lld",&n,&V,&p);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
ll ans = solve(a,b,n,p,V,1)+solve(b,a,n,p,V,0);
printf("%lld
", ans);
}
return 0;
}