Prime counting,sum of primes,leetcode,dp.

主要参考 http://www.zhihu.com/question/29580448/answer/45218281

https://projecteuler.net/thread=10;page=5   Lucy_Hedgehog 的代码

如果不用map,自己写hash函数会更快

// 计算 Π(n-1)class Solution {
public:
    int countPrimes(int n) {
        n--;
        if(n<=1) return 0;
        int r = int(sqrt(n));
        map<int ,int> hmp;
        for(int i = 1;i<=r;i++) hmp[n/i] = n/i - 1;
        for(int i = n/r -1;i>0;i--) hmp[i] = i - 1;
        for(int p = 2;p<=r;p++){
            if(hmp[p]>hmp[p-1]){
                int sp = hmp[p-1];
                int p2 = p*p;
                for(int i = 1;i<=r;i++){
                    int t = n/i;
                    if(t<p2) break;
                    hmp[t] -= hmp[t/p] - sp;
                }
                for(int i = n/r -1;i>0;i--){
                    if(i<p2) break;
                    hmp[i] -= hmp[i/p] - sp;
                }
            }
        }
    return hmp[n];
    }
};
//计算 2....n之间的素数和long long  P10(long long n){
    long long r = long long(sqrt(n));
    hash_map<long long,long long> hmp;
    for(long long i = 1;i<=r;i++){
        long long t = n/i;
        hmp[t] = t*(t+1)/2 - 1;
    }
    for(long long i = n/r -1;i>0;i--){
        long long t = i;
        hmp[t] = t*(t+1)/2 - 1;
    }
    for(long long p = 2;p<=r;p++){
        if(hmp[p]>hmp[p-1]){
            long long sp = hmp[p-1];
            long long p2 = p*p;
            for(long long i = 1;i<=r;i++){
                long long t = n/i;
                if(t<p2) break;
                hmp[t] -= p*(hmp[t/p] - sp);
            }
            for(long long i = n/r -1;i>0;i--){
                long long t = i;
                if(t<p2) break;
                hmp[t] -= p*(hmp[t/p] - sp);
            }
        }
    }
    return hmp[n];
}
时间: 2024-11-05 14:58:35

Prime counting,sum of primes,leetcode,dp.的相关文章

hdu 1024 Max Sum Plus Plus(DP)

转移方程dp[i][j]=Max(dp[i][j-1]+a[j],max(dp[i-1][k] ) + a[j] ) 0<k<j 此链接中有详解点击打开链接 #include<stdio.h> #include<algorithm> #include<iostream> using namespace std; #define MAXN 1000000 #define INF 0x7fffffff int dp[MAXN+10]; int mmax[MAXN

UvaLive6661 Equal Sum Sets dfs或dp

UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cs

UVA - 10891 Game of Sum(记忆化搜索 dp)

#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int a[100+10]; int dp[120][120]; int sum[120]; int vis[120][120]; int dfs(int f,int t) { int i,j,k; if(vis[f][t]==

Codeforces 75D Big Maximum Sum 最大子段和 dp

题目链接:点击打开链接 题意: 第一行 n m n个vector 下面n行 第一个数字u表示vector 的大小,然后后面u个数字给出这个vector 最后一行m个数字 表示把上面的vector拼接起来 得到一个大序列,求这个大序列的最大子段和 先预处理出每个vector的最大子段和,左起连续最大,右起连续最大,所有数的和 然后dp 一下.. #include <cstdio> #include <iostream> #include <algorithm> #incl

Uva 10891 Game of Sum(区间博弈dp)

10891 - Game of Sum Time limit: 3.000 seconds This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end

[Leetcode] DP -- Target Sum

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers

Binary Tree Maximum Path Sum [leetcode] dp

a(i):以节点i作为终点的单边最大路径和 b(i):以节点i作为终点的双边边最大路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->right) }}; b(i) = max{ i->val, i->val + max{a(i->left), a(i->right) } , i->val + a(i->left) + a(i->right)}; 由于a(i), b(i)仅仅和a(i-

Hdoj 1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18861 Accepted Submission(s): 6205 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b

HDU 1024:Max Sum Plus Plus(DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24675    Accepted Submission(s): 8478 Problem Description Now I think you have g