Poj2739 Sum of Consecutive Prime Numbers(再见尺取)

题目传送门

题意简述:给定一个n,如果n可以表示为连续的素数和,求出有多少种这样不同的表示。

先筛一遍素数,然后尺取记录就可以啦~

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cin.tie(0);                  \
    // cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 11092019;
int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
int a[maxn];
bool isprime[maxm]; //若1为素数 若0不是素数
int su[maxm];
int cnt = 0;
void prime()
{
    memset(isprime, true, sizeof isprime);
    memset(su, 0, sizeof su);
    isprime[0] = isprime[1] = false; // 0 和 1 不是素数
    for (int i = 2; i <= maxm; i++)
    {
        if (isprime[i])
            su[++cnt] = i;
        for (int j = 1; j <= cnt && i * su[j] <= maxm; j++)
        {
            isprime[i * su[j]] = false;
            if (i % su[j] == 0)
                break;
        }
    }
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    IO;
    int T;
    int n, s;
    prime();
    // for (int i = 1; i <= 25; i++)
    //     cout << su[i] << endl;
    // cout << cnt << endl;
    while (scanf("%d", &n) == 1 && n)
    {
        int L = 1;
        int R = 1;
        int ans = 0;
        int t = su[1];
        while (L <= cnt && R <= cnt)
        {
            if (t >= n)
            {
                if (t == n)
                    ans++;
                t -= su[L];
                ++L;
            }
            else
            {
                ++R;
                t += su[R];
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值