寒假训练——第四周(筛法)

A - 简单素数筛法

A - 简单素数筛法

思路:

  • 筛素数
  • 法一:埃氏筛( O ( n ∗ l o g l o g n ) O(n*loglogn) O(nloglogn) )
  • 法二:欧氏筛 也叫 线性筛 ( O ( n ) O(n) O(n) )

埃氏筛代码如下:

void get_primes(int n)
{
    for (int i = 2; i <= n; i ++ )
    {
        if(!st[i]) primes[cnt ++ ] = i;
        for (int j = i * i; j <= n; j += i)
            st[j] = true;
    }
}

线性筛代码如下:

void get_primes(int n)
{
    for (int i = 2; i <= n; i ++ )
    {
        if(!st[i]) primes[cnt ++ ] = i;
        for (int j = 0; primes[j] <= n / i; j ++ )
        {
            st[i * primes[j]] = true;
            if(i % primes[j] == 0) break;
        }
    }
}

B - Sum of Consecutive Prime Numbers

B - Sum of Consecutive Prime Numbers

思路:

  • 连续素数和
  • 线性筛 + 双指针(或暴力)

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)

#define mkpr make_pair
#define endl '\n'
#define x first
#define y second
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;

const int N = 10010, M = N * 2;
const int YB = 8, YM = 1e8;

const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int T, cases;
int n, m, times;

int primes[N], cnt;
bool st[N];

void get_primes(int n)
{
    for (int i = 2; i <= N; i ++ )
    {
        if(!st[i]) primes[cnt ++ ] = i;
        for (int j = 0; primes[j] <= n / i; j ++ )
        {
            st[i * primes[j]] = true;
            if(i % primes[j] == 0) break;
        }
    }
}

void solve()
{
    int res = 0;
    int sum = 0;
    for (int l = 0, r = 0; primes[r] <= n; r ++ )
    {
        sum += primes[r];
        while(sum > n)
        {
            sum -= primes[l];
            l ++;
        }
        if(sum == n) res ++;
    }
    cout << res << endl;

    return;
}

signed main()
{
    get_primes(N);
    
    T = 1;
    //fast;cin >> T;
    //scanf("%d", &T);

    //for (cases = 1; cases <= T; cases ++ )
    while(cin >> n, n)
        solve();

    return 0;
}

C - Semi-prime H-numbers

C - Semi-prime H-numbers

注意:

  • 本题解的质数和素数同,,其实本来就相同,避免同学们弄混

思路:

  • 线性筛(欧氏筛) 或 埃氏筛 的扩展(自然数 缩小到 h数)

具体实现:

  • h数:所有模5余1的数
  • 我们要找的H-semi-primes(H半质数)a * b其中a bh素数
  • 何为h素数:在h范围内,除了1该数本身外无法被其他h数整除的数(也可定义为只有1该数本身两个正因数的h数),与自然素数定义很像
  • 自然素数:指在大于1的自然数的范围内,除了1该数自身外,无法被其他自然数整除的数(也可定义为只有1该数本身两个正因数的自然数
  • 如此我们发现,我们要求的h素数只不过为范围为h数的素数
  • 如此一来我们仅需在h数的范围内做线性筛,即可求出所有的h数;同时,筛h素数时,若两个数都为h素数,则a * bH-semi-primes(H半质数)

代码如下(和线性筛一模一样):

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)

#define mkpr make_pair
#define endl '\n'
#define x first
#define y second
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;

const int N = 1e6 + 10, M = N * 2;
const int YB = 8, YM = 1e8;

const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int T, cases;
int n, m, times;

int primes[N], cnt;
bool st[N];

bool Hprimes[N];
int ans[N];

void get_primes(int n)
{
    for (int i = 5; i <= n; i += 4 )
    {
        if(!st[i]) primes[cnt ++ ] = i;
        
        for (int j = 0; primes[j] <= n / i; j ++ )
        {
            st[i * primes[j]] = true;
            // st[primes[j]] = true 所以只判断st[i]是否为 h素数即可
            if(!st[i]) Hprimes[i * primes[j]] = true; 
            if(i % primes[j] == 0) break;
        }
    }
    
    for (int i = 1; i <= N; i ++ )
    {
        if(Hprimes[i]) ans[i] = ans[i - 1] + 1;
        else ans[i] = ans[i - 1];
    }
}

void solve()
{
    cout << n << " " << ans[n] << endl;
    
    return;
}

signed main()
{
    get_primes(N);
    
    T = 1;
    //fast;cin >> T;
    //scanf("%d", &T);

    //for (cases = 1; cases <= T; cases ++ )
    while(cin >> n, n)
        solve();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AC自动寄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值