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 提示 以这道题目为 ...
随机推荐
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- 第09组 Beta冲刺(4/5)
队名:观光队 链接 组长博客 作业博客 组员实践情况 王耀鑫 过去两天完成了哪些任务 文字/口头描述 学习 展示GitHub当日代码/文档签入记录 无 接下来的计划 完成短租车,页面美化 还剩下哪些任 ...
- intellij idea安装教程
1. 双击ideaIU-12.1.1.exe,点击下一步,安装目录改为d:\Program Files\JetBrains\IntelliJ IDEA,其他项都默认即可(此处更改目录方便重做系统,不用 ...
- 用欧拉计划学Rust语言(第7~12题)
最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,所以先补一下Rust的基础知识.学习了一段时间,发现Rust的学习曲线非常陡峭,不过仍有快速入门的办法. 学习任何一项技能最怕没有 ...
- Qt 绘制图表 - Qt Charts版
一.前言 自从 Qt 发布以来,给广大跨平台界面研发人员带来了无数的福利.但是Qt自己却一直没有提供自带的图表库,这就使得 QWT.QCustomPlot 等第三方图表库有了巨大的生存空间,为了降低开 ...
- hdu6546 Function
Function \(\text{Alice}\) 有 \(n\) 个二次函数 \(F_i(x)=a_ix^2+b_ix+c_i(i \in [1,n])\). 现在他想在 \(\sum_{i=1}^ ...
- SWIG 3 中文手册——1. 前言
目录 1 前言 1.1 引言 1.2 SWIG 版本 1.3 SWIG 许可证 1.4 SWIG 资源 1.5 前提要求 1.6 本手册的组织构成 1.7 如何避免阅读手册 1.8 向后兼容 1.9 ...
- Linux(centOS6.5)安装RabbitMQ
第一.下载erlang和rabbitmq-server的rpm: wget http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.c ...
- Win10 CompatTelRunner.exe占用磁盘高的解决办法
(1)在运行里输入 taskschd.msc, 打开windows计划任务 (2)参考下图在Application Experience下,1,2,3,4步骤,把所有带有 “CompatTelRun ...
- C# WebBrowser控件 下载文件不弹下载提示框的办法
参考链接 https://stackoverflow.com/questions/6773866/download-file-and-automatically-save-it-to-folder p ...