分析外星信号
程序员文章站
2024-01-19 14:46:22
...
题意:给出长度为N的信号纪录时,编写程序计算总和等于k的子序列个数
通过公式来生成输入值
通过在线算法来编程,代码如下:
#include <bits/stdc++.h>
using namespace std;
struct RNG
{
unsigned seed;
RNG():seed(1983) {}
unsigned next()
{
unsigned ret=seed;
seed=((seed*214013u)+2531011u);
return ret%10000+1;
}
};
int countRartialSums(int k,int n)
{
RNG rng;
int ret=0;
long long pusm=0;
queue<long long>pusms;
for(int i=0; i<n; i++)
{
pusm+=rng.next();
pusms.push(pusm);
while(pusms.front()+k<pusm)
pusms.pop();
if(pusms.front()+k==pusm)
++ret;
}
return ret;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int k,n;
scanf("%d %d",&k,&n);
int sum=countRartialSums(k,n);
printf("%d\n",sum);
}
return 0;
}