题目描述:

Soldier and Number Game

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x > 1, such that n is divisible by x and replacing n with n / x. When n becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.

To make the game more interesting, first soldier chooses n of form a! / b! for some positive integer a and b (a ≥ b). Here by k! we denote the factorial of k that is defined as a product of all positive integers not large than k.

What is the maximum possible score of the second soldier?

Input

First line of input consists of single integer t (1 ≤ t ≤ 1 000 000) denoting number of games soldiers play.

Then follow t lines, each contains pair of integers a and b (1 ≤ b ≤ a ≤ 5 000 000) defining the value of n for a game.

Output

For each game output a maximum score that the second soldier can get.

Examples

Input

Copy

23 16 3

Output

Copy

25

思路:

这题目要求是给一个数n,求他的质因数的个数。又因为n是\(\frac{a!}{b!}\)的形式,即\(\prod_{i=b+1}^ai\)。也就是从b+1到a的每个数的质因数个数之和。刚开始最最原始的方法求,也就是从b+1开始遍历,求每个数的质因数的个数,然后加起来,求质因数用的是那种朴素的\(O(\sqrt{n})\)的做法,如果n是很大,而且t也很多(\(\leq10^{6}\)),就gg了。

然后执迷不悟,想的是问题出在了质因数分解上(好吧,确实出在了质因数分解上),打出miler_rabin和pollard-rho模板,分解质因数,可还是超时。

然后想到既然是区间性问题为什么我不用素数筛呢?但一看数据到\(5*10^{6}\),限时是3秒,这样打出来也是超时啊。因为我的印象中,一秒的循环次数大概在10^6,这样,那岂不是要5秒才能处理完素数表?

但后来的事情刷新了我的认知,真的是可以这么快的。那思路就是在筛素数时遇到合数是当前素数的倍数就不断地除以这个素数看他有几个这样的素因子。他也可能不同的素数的倍数,到时都会不断的加他素因子的个数。那么素数表打完了,我们的每个数的素因子个数也统计出来了。但这样是不够的,为什么?想一想询问的是一个区间,最大长度是\(10^6\)量级,加上询问的t,又gg了。这时就想到了前缀和处理区间问题。我们把前缀和算出来,在询问某个区间时对应的两个前后项一减就是区间内的统计结果。后来因为读入数据量比较大再加了读入优化,吸了氧(其实不吸也可以)。

我后来统计了一下初始化的时间,大概在0.4s左右,开了O2能少个100ms的样子。

代码:

#include <iostream>
#include <cstdio>
#include <memory.h>
#include <ctime>
#define max_n 5000005
using namespace std;
int t;
long long a,b;
int prime[max_n];
int num[max_n];
template<typename T>
inline void read(T& x)
{
x=0;int f=0;char ch=getchar();
while('0'>ch||ch>'9'){if(ch=='-')f=1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
x=f?-x:x;
}
void init()
{
memset(num,0,sizeof(num));
memset(prime,-1,sizeof(prime));
prime[0] = prime[1] = 0;
for(int i = 2;i<=max_n;i++)
{
if(prime[i]==-1)
{
num[i]++;
for(int j = 2*i;j<=max_n;j+=i)
{
prime[j] = 0;
int tmp = j;
while(tmp%i==0)
{
num[j]++;
tmp/=i;
} }
}
}
for(int i = 2;i<=max_n;i++)
{
num[i]+=num[i-1];
}
}
#pragma optimize(2)
int main()
{
//clock_t start = clock();
init();
//clock_t end = clock();
//printf("%f s\n",(double)(end-start)/CLOCKS_PER_SEC);
read(t);
while(t--)
{
read(a);
read(b);
printf("%d\n",num[a]-num[b]);
}
return 0;
}

Codeforces J. Soldier and Number Game(素数筛)的更多相关文章

  1. Codeforces Round #304 (Div. 2) D. Soldier and Number Game 素数打表+质因数分解

    D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  2. codeforces 569C C. Primes or Palindromes?(素数筛+dp)

    题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...

  3. codeforces 414A A. Mashmokh and Numbers(素数筛)

    题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...

  4. Codeforces 546D Soldier and Number Game(数论)

    类似筛素数的方法……求出前缀和.然后直接O(1)回答即可. #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) ...

  5. zoj3886--Nico Number(素数筛+线段树)

    Nico Number Time Limit: 2 Seconds      Memory Limit: 262144 KB Kousaka Honoka and Minami Kotori are ...

  6. CodeForces 546D Soldier and Number Game 打表(求质因子个数)

    题目:戳我这个题与HDUOJ 5317有异曲同工之妙 题意:题意看懂了上面的一大串英文之后其实很简单,就是给你一个正整数n,问你n有多少个质因子,不过这里n是通过a!/b!给定的,也就是说n=(a!/ ...

  7. Soldier and Number Game-素数筛

    Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and giv ...

  8. codeforces 546D Soldier and Number Game

    题目链接 这个题, 告诉你a, b的值, 那么只需要求出b到a之间的数, 每个数有多少个因子就可以. 具体看代码, 代码里面有解释 #include<bits/stdc++.h> usin ...

  9. 素数筛总结篇___Eratosthenes筛法和欧拉筛法(*【模板】使用 )

    求素数 题目描述 求小于n的所有素数的数量. 输入 多组输入,输入整数n(n<1000000),以0结束. 输出 输出n以内所有素数的个数. 示例输入 10 0 示例输出 4 提示 以这道题目为 ...

随机推荐

  1. $O(k^2)$ 求前缀 $k$ 次幂和(与长度无关)

    接下来求解前缀幂次和 求解 \(\sum_{i = 1}^{k} i^k\) \[ \begin{aligned} (p+1)^k - 1 = (p+1)^k - p^k + p^k - (p-1)^ ...

  2. [LeetCode] 410. Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  3. divide two numbers using + opertor

    package testpacknm; import java.util.Scanner; public class testcnm { public static void main(String[ ...

  4. SpringBoot第八篇:整合MyBatis-Generator

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10894278.html 版权声明:本文为博主原创文章,转载请附上博文链接! 注意:本章有大量代码 ...

  5. 【maven】pom.xml的exclusions排除依赖传递

    在引用两个有冲突的依赖时,就需要把其中一个的依赖中某个依赖排除掉 exclusions 例如: <dependency> <groupId>org.activiti</g ...

  6. SpringBoot 基于web应用开发(请求参数获取,静态资源,webjars)

    SpringBoot 基于web应用开发 一.Lombok使用 1.导入依赖库 <dependency>    <groupId>org.projectlombok</g ...

  7. Swagger 自定义UI界面

    Swagger 自定义UI界面 Swagger简单介绍 如何使用Swagger 添加自定义UI界面 使用swagger-ui-layer Swagger ui 的原生UI界面如下: 个人感觉原生UI显 ...

  8. MySQL中的 redo 日志文件

    MySQL中的 redo 日志文件 MySQL中有三种日志文件,redo log.bin log.undo log.redo log 是 存储引擎层(innodb)生成的日志,主要为了保证数据的可靠性 ...

  9. Kubernetes增强型调度器Volcano算法分析【华为云技术分享】

    [摘要] Volcano 是基于 Kubernetes 的批处理系统,源自于华为云开源出来的.Volcano 方便 AI.大数据.基因.渲染等诸多行业通用计算框架接入,提供高性能任务调度引擎,高性能异 ...

  10. 【04】Kubernets:资源清单(pod)

    写在前面的话 前面我们提到过,纯手敲 K8S 名称管理 K8S 服务只是作为我们了解 K8S 的一种方案,而我们最终管理 K8S 的方法还是通过接下来的资源清单的方式进行管理. 所以从本章节开始,将会 ...