2022年上错题集——Complex Market Analysis
作者:互联网
While performing complex market analysis William encountered the following problem:
For a given arrayaaof sizennand a natural numberee, calculate the number of pairs of natural numbers(i,k)(i,k)which satisfy the following conditions:
- 1≤i,k1≤i,k
- i+ek≤ni+ek≤n.
- Productaiai+eai+2e…ai+keaiai+eai+2e…ai+keis a prime number.
A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
Input
Each test contains multiple test cases. The first line contains the number of test casestt(1≤t≤100001≤t≤10000). Description of the test cases follows.
The first line of each test case contains two integersnnandee(1≤e≤n≤2105)(1≤e≤n≤2105), the number of items in the array and numberee, respectively.
The second line containsnnintegersa1,a2,…,ana1,a2,…,an(1≤ai≤106)(1≤ai≤106), the contents of the array.
It is guaranteed that the sum ofnnover all test cases does not exceed21052105.
Output
For each test case output the answer in the following format:
Output one line containing the number of pairs of numbers(i,k)(i,k)which satisfy the conditions.
Example
input
Copy
6
7 3
10 2 1 3 1 19 3
3 2
1 13 1
9 3
2 4 2 1 1 1 1 4 2
3 1
1 1 1
4 1
1 2 1 1
2 2
1 2output
Copy
2
0
4
0
5
0Note
In the first example test case two pairs satisfy the conditions:
- i=2,k=1i=2,k=1, for which the product is:a2a5=2a2a5=2which is a prime number.
- i=3,k=1i=3,k=1, for which the product is:a3a6=19a3a6=19which is a prime number.
In the second example test case there are no pairs that satisfy the conditions.
In the third example test case four pairs satisfy the conditions:
- i=1,k=1i=1,k=1, for which the product is:a1a4=2a1a4=2which is a prime number.
- i=1,k=2i=1,k=2, for which the product is:a1a4a7=2a1a4a7=2which is a prime number.
- i=3,k=1i=3,k=1, for which the product is:a3a6=2a3a6=2which is a prime number.
- i=6,k=1i=6,k=1, for which the product is:a6a9=2a6a9=2which is a prime number.
In the fourth example test case there are no pairs that satisfy the conditions.
In the fifth example test case five pairs satisfy the conditions:
- i=1,k=1i=1,k=1, for which the product is:a1a2=2a1a2=2which is a prime number.
- i=1,k=2i=1,k=2, for which the product is:a1a2a3=2a1a2a3=2which is a prime number.
- i=1,k=3i=1,k=3, for which the product is:a1a2a3a4=2a1a2a3a4=2which is a prime number.
- i=2,k=1i=2,k=1, for which the product is:a2a3=2a2a3=2which is a prime number.
- i=2,k=2i=2,k=2, for which the product is:a2a3a4=2a2a3a4=2which is a prime number.
In the sixth example test case there are no pairs that satisfy the conditions.
题目类型:素数,模拟
题目大意:一维数组, 给出了数组长度和元素与递增步长,求能找到多少对(i, k)满足ai……ai+k*e,以i为起点,e为步长的子序列并且元素总和为素数。
解题思路:1.先筛素数,用线性筛。
2.遍历数组,若本位是 素数,则分别向前后和向后检索1,总数+t1+t2, 再把两个方向增加的方案算上,即总数+t1*t2。
3.基于乘积为素数,必然要求有且只有一个元素为素数,其他的数必为1,才能满足条件。因此, 只在遇到素数时,检索 其前后出现过的1的个数,并进行组合。
AC代码:
#include <bits/stdc++.h>
#define rep(x, a, b) for(int x = a; x <= b; x++)
#define pre(x, a, b) for(int x = b; x >= a; x--)
#define ll long long
#define endl '
'
#define PII pair<int, int>
using namespace std;
const int N = 2e5+10, M = 1e6+10;
int a[N], prime[M], idx;
bool isp[M];
int n, e;
void initP()
{
isp[0] = true;
isp[1] = true;
for(int i = 2; i< M; i++)
{
if(!isp[i]) prime[idx++] = i;
for(int j = 0; j<idx && i*prime[j] <= M; j++)
{
isp[i*prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
}
void solve()
{
cin>>n>>e;
for(int i = 1; i<= n; i++) cin>>a[i];
ll ans = 0;
for(int i = 1; i<= n; i++)
{
if(!isp[a[i]])
{
ll t1 = 0, t2 = 0;
for(int j = i+e; j<= n; j+= e)
{
if(a[j] == 1)t1++;
else break;
}
for(int j = i-e; j>=1; j-=e)
{
if(a[j] == 1) t2++;
else break;
}
ans += t1 + t2 + t1 * t2 ;//分别与往后t1 和往前t2单向组合,再t1*t2 双向组合
}
}
cout<<ans<<endl;
}
int main()
{
int t = 1;
bool is = true;
if(is) cin>>t;
//getchar();
initP();
while(t -- )
{
solve();
}
return 0;
}
标签:prime,product,number,Analysis,错题,Complex,test,conditions,2which 来源: https://blog.csdn.net/web15286201346/article/details/123592614