尺取法求[i,j]区间,使得[i,j]区间size>=k且>=T的数恰好有k个,于是[1,j],[2,j],[3,j],….,[i,j]都满足题意,以j结尾有i种方案
#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))
using namespace std;
const int inf=1e9+7;
const int N = 1e7+5;
int num;//>=T的数个数
ll a[N];
ll slove(int n,int T,int k){
num=0;
int i,j;
for(i=1;num<k&&i<=n;++i){
num+=(a[i]>=T);
}
if(num<k){
return 0;
}
ll ans=0;
j=i-1;
i=1;
for(;j<=n;++j){
while((j-i+1>=k+1)&&(a[i]<T||(num-1>=k))){
num-=(a[i]>=T);
++i;
}
ans+=i;
num+=(a[j+1]>=T);
}
return ans;
}
int main(){
//freopen("/home/lu/code/r.txt","r",stdin);
//freopen("/home/lu/code/w.txt","w",stdout);
int n,k,T,a0,b,c,p;
while(~scanf("%d%d%d%d%d%d%d",&n,&k,&T,&a0,&b,&c,&p)){
a[0]=a0;
for(int i=1;i<=n;++i){
a[i]=(a[i-1]*b+c)%p;
}
printf("%lld\n",slove(n,T,k));
}
return 0;
}