Codeforces J. Soldier and Number Game(素数筛)
题目描述:
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(素数筛)的更多相关文章
- 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 ...
- codeforces 569C C. Primes or Palindromes?(素数筛+dp)
题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...
- codeforces 414A A. Mashmokh and Numbers(素数筛)
题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 546D Soldier and Number Game(数论)
类似筛素数的方法……求出前缀和.然后直接O(1)回答即可. #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) ...
- zoj3886--Nico Number(素数筛+线段树)
Nico Number Time Limit: 2 Seconds Memory Limit: 262144 KB Kousaka Honoka and Minami Kotori are ...
- CodeForces 546D Soldier and Number Game 打表(求质因子个数)
题目:戳我这个题与HDUOJ 5317有异曲同工之妙 题意:题意看懂了上面的一大串英文之后其实很简单,就是给你一个正整数n,问你n有多少个质因子,不过这里n是通过a!/b!给定的,也就是说n=(a!/ ...
- Soldier and Number Game-素数筛
Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and giv ...
- codeforces 546D Soldier and Number Game
题目链接 这个题, 告诉你a, b的值, 那么只需要求出b到a之间的数, 每个数有多少个因子就可以. 具体看代码, 代码里面有解释 #include<bits/stdc++.h> usin ...
- 素数筛总结篇___Eratosthenes筛法和欧拉筛法(*【模板】使用 )
求素数 题目描述 求小于n的所有素数的数量. 输入 多组输入,输入整数n(n<1000000),以0结束. 输出 输出n以内所有素数的个数. 示例输入 10 0 示例输出 4 提示 以这道题目为 ...
随机推荐
- 在程序中修改IP win7 winXP(参考1)
https://blog.csdn.net/bbdxf/article/details/7548443 Windows下程序修改IP的三种方法 以下讨论的平台依据是Window XP + SP1, 不 ...
- [LeetCode] 900. RLE Iterator RLE迭代器
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- [LeetCode] 438. Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- 文件夹如何添加备注(保证可以WIN7 WIN10测试通过)
网上很多方法都有人说试过了,不可以.其实不是不可以,他们都没有说完整 今天自己弄了下,弄出来了,废话不多说先上图 如果需要用备注排序,那么就需要把排序的选项加上备注的分类 1.获得desktop.in ...
- Zuul整合Swagger,使用ZuulFilter解决下游服务context-path
问题起因:使用Zuul网关服务,需要整合下游系统的swagger,但是下游服务存在context-path配置,无法正确跳转,最后使用ZuulFilter解决. 1.Zuul整合下游swagger 首 ...
- idea找不到tomcat,找不到Tomcat server
打开settings 添加即可 如果再没有,可能需要先安装timcat插件
- java中super()和this()、super和this的区别
1.super()和this()区别: super():调用父类无形参的构造方法: super(形参):调用父类中某个带形参的构造方法: this(形参):调用本类中另一种形式的构造方法: 注意:放在 ...
- 阿里巴巴 Java 开发手册 1.4.0
一.编程规约(一) 命名风格1. [强制]代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束.反例: _name / __name / $name / name_ / name$ ...
- vertica创建新用户并授权
1.创建用户,并设置密码: create user user1 identified by 'pwd1'; 2.把角色授权给用户(dbduser是普通角色): grant dbduser to use ...
- url拼接参数格式
在一些情况下,需要直接往url上拼接请求参数. http://www.yanggb.com?flag=1&type=normal&role=customer 通过上面的例子就可以看出, ...