add some template for ec-final

二维rmq 离线 init O( n*n*logn*logn )  query O(1)

http://www.cnblogs.com/kuangbin/p/3227420.html

求1-n有多少个素数 n = 1e9

 #include <bits/stdc++.h>
using namespace std;
namespace acm {
/// count primes between [1..n] (0< n <=1e9)
/// solution: dp
/// d(n, i) means that the number of value whose min prime number is bigger than i in [1..n]
/// d(n, i) = d(n, i-1) if number i is not a prime
/// d(n, i) = d(n, i-1) - d(n/i, i) + d(i, i-1) if number i is a prime
/// complex time: O(n^(3/4)), space: O(n^(1/2))
struct CountPrime {
int solve(int n) {
if (n < ) {
return ;
}
dp[] = ;
int sqrtn = , length = ;
for (; sqrtn <= n / sqrtn; ++sqrtn) {
dp[++length] = sqrtn - ;
}
for (int i = sqrtn - ; i > ; --i) {
int val = n / i;
if (val != i) {
dp[++length] = val - ;
}
}
for (int i = ; i < sqrtn; ++i) {
if (dp[i] == dp[i-]) {
continue;
}
for (int j=length; j>; --j) {
int val = j < sqrtn ? j : n / (length - j + );
if (i > val / i) {
break;
}
int pre_statue_pos = val / i;
if (pre_statue_pos >= sqrtn) {
pre_statue_pos = length - n / pre_statue_pos + ;
}
dp[j] += dp[i-] - dp[pre_statue_pos];
}
}
return dp[length];
}
static const int maxn = 1e5+;
int dp[maxn];
};
} // namespace acm
int main()
{
//freopen("dp.in", "r", stdin);
//freopen("dp.out", "w", stdout);
acm::CountPrime cp;
int n;
while (scanf ("%d", &n) != EOF) {
printf ("%d\n", cp.solve(n));
}
return ;
}

end

上一篇:【网络流24题】No.7 试题库问题 (最大流,二分图多重匹配)


下一篇:MySQL-悲观锁和乐观锁