ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2022年上错题集——Complex Market Analysis

2022-03-19 12:58:45  阅读:175  来源: 互联网

标签:prime product number Analysis 错题 Complex test conditions 2which


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 2

output

Copy

2
0
4
0
5
0

Note

In the first example test case two pairs satisfy the conditions:

  1. i=2,k=1i=2,k=1, for which the product is:a2a5=2a2a5=2which is a prime number.
  2. 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:

  1. i=1,k=1i=1,k=1, for which the product is:a1a4=2a1a4=2which is a prime number.
  2. i=1,k=2i=1,k=2, for which the product is:a1a4a7=2a1a4a7=2which is a prime number.
  3. i=3,k=1i=3,k=1, for which the product is:a3a6=2a3a6=2which is a prime number.
  4. 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:

  1. i=1,k=1i=1,k=1, for which the product is:a1a2=2a1a2=2which is a prime number.
  2. i=1,k=2i=1,k=2, for which the product is:a1a2a3=2a1a2a3=2which is a prime number.
  3. i=1,k=3i=1,k=3, for which the product is:a1a2a3a4=2a1a2a3a4=2which is a prime number.
  4. i=2,k=1i=2,k=1, for which the product is:a2a3=2a2a3=2which is a prime number.
  5. 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有