DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
2024-08-30 14:04:18
/*
题意:b+1,b+2,...,a 所有数的素数个数和
DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1;否则它可以从一个数乘以素数递推过来
最后改为i之前所有素数个数和,那么ans = dp[a] - dp[b];
详细解释:http://blog.csdn.net/catglory/article/details/45932593
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <cmath>
using namespace std; typedef long long ll; const int MAXN = 5e6 + ;
const int INF = 0x3f3f3f3f;
int prime[MAXN];
bool is_prime[MAXN];
ll dp[MAXN]; int seive(void)
{
for (int i=; i<=5e6; ++i) is_prime[i] = true;
is_prime[] = is_prime[] = false;
int p = ;
for (int i=; i<=5e6; ++i)
{
if (is_prime[i])
{
prime[++p] = i;
for (int j=*i; j<=5e6; j+=i) is_prime[j] = false;
}
} return p;
} void solve(void)
{
int p = seive ();
for (int i=; i<=5e6; ++i)
{
if (is_prime[i]) {dp[i] = ; continue;}
for (int j=; j<=p; ++j)
{
if (i % prime[j] == )
{
dp[i] = dp[i/prime[j]] + ; break;
}
}
} for (int i=; i<=5e6; ++i) dp[i] += dp[i-];
} int main(void) //Codeforces Round #304 (Div. 2) D. Soldier and Number Game
{
solve ();
int t; scanf ("%d", &t);
while (t--)
{
int a, b;
scanf ("%d%d", &a, &b);
printf ("%I64d\n", dp[a] - dp[b]);
} return ;
} /*
3
3 1
6 3
5000000 4999995
*/
最新文章
- sp_sys_ERPTrigger_base
- C 中 关于printf 函数中度剖析
- 如何设置table的border-radius?
- JavaScript API
- 给ThinkPHP5增加验证码功能
- session和cookie的辨析[阅读]
- sqoop使用的问题
- DOM Exception error 类型
- Vasya the Hipster
- 设计模式 — 工厂方法模式(Factory Method)
- OpenCV__type()返回的数字
- functions 示例
- 论文阅读笔记二十二:End-to-End Instance Segmentation with Recurrent Attention(CVPR2017)
- JavaEE XML的读写(利用JDom对XML文件进行读写)
- Netty1
- 再谈fedora下的音乐和视频播放器的安装
- cmder简单使用
- 删除文件以后,如何通过git撤销删除的文件,不提交到远端代码库
- c# 设计模式 之:策略模式
- 使用C6748和C5509A对nRF24L01驱动进行数据传输