码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - "Red John is Back"

时间:2015-07-03 07:59:09      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

1AC. 1D DP + Sieving

#include <cmath>
#include <cstdio>
#include <cmath>
#include <climits>
#include <cctype>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
using namespace std;

int ways(int n)
{
    if (n <= 1) return n;

    vector<int> dp(n + 1, 0);
    dp[0] = 1;
    for (int i = 0; i < n; i++)
    {
        dp[i + 1] += dp[i];
        if ((i + 4) <= n) dp[i + 4] += dp[i];
    }
    return dp[n];
}

int cntPrimes(int n) // sieving
{
    vector<int> mark(n, 1);
    mark[0] = 0;

    int inx = 1; // 2
    int scnt = 1;
    while (inx < n && scnt > 0)
    {
        scnt = 0;
        for (int i = 2* inx + 1; i < n; i += (inx + 1))
        {
            mark[i] = 0;
            scnt++;
        }
        if (scnt > 0)
        {
            inx++;
            while (inx < n && mark[inx] == 0) inx++;
        }
    }

    return std::accumulate(mark.begin(), mark.end(), 0);
}

int main()
{
    int t; cin >> t;
    while (t--)
    {
        int n; cin >> n;
        int nCnt = ways(n);
        int ret = cntPrimes(nCnt);
        cout << ret << endl;
    }
    return 0;
}

HackerRank - "Red John is Back"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4617595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!