UVA - 1404 Prime k-tuple【筛素数】

{p1, . . . , pk : p1 < p2 < . . . < pk} is called a prime k-tuple of distance s if p1, p2, . . . , pk are consecutive
prime numbers and pk − p1 = s. For example, with k = 4, s = 8, {11, 13, 17, 19} is a prime 4-tuple of
distance 8.
Given an interval [a, b], k, and s, your task is to write a program to find the number of prime
k-tuples of distance s in the interval [a, b].
Input
The input file consists of several data sets. The first line of the input file contains the number of data
sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.
For each data set, there is only one line containing 4 numbers, a, b, k and s (a, b < 2 ∗ 109
, k < 10,
s < 40).
Output
For each test case, write in one line the numbers of prime k-tuples of distance s.
Sample Input
1
100 200 4 8
Sample Output
2

题意:给出一个区间a,b和k,s,求区间内有k个相邻素数元素且pk-p1=s的区间个数

思路:数太大,二重筛找素数;

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
#define ll long long
const int maxn = 100005;
ll prime[maxn], vis[maxn], tot;
void init()
{
    memset(vis, 0, sizeof(0));
    vis[1] = vis[0] = 1;
    tot = 0;
    for (ll i = 2; i < maxn; i++)
        if (!vis[i])
        {
            prime[tot++] = i;
            for (ll j = i*i; j < maxn; j += i)
                vis[j] = 1;
        }
}

int check(ll n)
{
    if (n<maxn)
        return vis[n]==0;
    for (int i=0;i<tot&&prime[i]*prime[i]<= n;i++)
        if (n%prime[i]==0)
            return 0;
    return 1;
}


int main()
{
    init();
    int t;
    ll a,b;
    int k,s;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%d%d",&a,&b,&k,&s);
        vector<int>r;
        int ans=0;
        for(ll i=a;i<=b;i++)
        {
            if(check(i))
            {
                r.push_back(i);
            }
        }
         for (int i=0;i+k-1<r.size();i++)
            if (r[i+k-1]-r[i]==s)
                ans++;
        printf("%d\n",ans);
    }

}

这个做的我有点懵,看数据范围要用LL,可一用就超时,都换成int就没事了,额(⊙﹏⊙),应该是数据太水了吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值