Codeforces Round #304 (Div. 2) D. Soldier and Number Game 素数打表+质因数分解
3 seconds
256 megabytes
standard input
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?
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.
For each game output a maximum score that the second soldier can get.
2
3 1
6 3
2
5
题意:求[b+1,a]区间内的所有数质因数分解的得到的的质数个数;
思路:素数打表+质因数分解求前缀和,这题nsqrt(n)不知道能不能过;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 1e-10
const int N=1e6+,M=1e6+,mod=1e9+,inf=1e9+;
const int MAXN=;
int prime[MAXN];
bool vis[MAXN];
int sum[MAXN];
int Prime(int n)
{
int cnt=;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
if(!vis[i])
prime[cnt++]=i;
for(int j=;j<cnt&&i*prime[j]<n;j++)
{
vis[i*prime[j]]=;
if(i%prime[j]==)
break;
}
}
return cnt;
}
int main()
{
int x,y,z,i,t;
int cnt=Prime(MAXN);
for(i=;i<MAXN;i++)
{
int flag=i;
for(t=;t<cnt;t++)
{
if(flag==)
break;
if(!vis[flag])
{
sum[i]++;
break;
}
while(flag%prime[t]==)
{
flag/=prime[t];
sum[i]++;
}
}
sum[i]+=sum[i-];
}
int T;
scanf("%d",&T);
while(T--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",sum[a]-sum[b]);
}
return ;
}
Codeforces Round #304 (Div. 2) D. Soldier and Number Game 素数打表+质因数分解的更多相关文章
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数学 质因数个数
D. Soldier and Number Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
- 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 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 Round #304 (Div. 2)(CF546D) Soldier and Number Game(线性筛)
题意 给你a,b(1<=b<=a<=5000000)表示a!/b!表示的数,你每次可以对这个数除以x(x>1且x为这个数的因子)使他变成a!/b!/x, 问你最多可以操作多少次 ...
- queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards
题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...
随机推荐
- 【BZOJ4444】[Scoi2015]国旗计划 双指针+倍增
[BZOJ4444][Scoi2015]国旗计划 Description A国正在开展一项伟大的计划——国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形 ...
- Happy Necklace(矩阵快速幂)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submissi ...
- java图片二进制相互转换
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOu ...
- JavaScript基础深入之
JS的数值类型是分为两类:基本数据类型和引用数据类型. 基本类型占据的内存栈空间,引用类型被保存在堆空间.引用类型赋值的变量也是被保存在栈空间的,它的作用类似于电视遥控器,负责操作堆空间内指向的对象. ...
- C#快速整理代码格式
删除最后一个大括号,再添加.vs自动整理代码结构.
- 网络免费API接口整理
转载自: https://www.cnblogs.com/doit8791/p/9351629.html 从网上看到一些免费API接口,在个人开发小程序等应用练手时可试用. 各类无次数限制的免费API ...
- PAT 1067. 试密码(20)
当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格式: 输入在第一行给出一个密码(长度不超过20的.不包含空格.Ta ...
- 安装mysql以及修改初始密码
我们可以采用类似安全模式的方法修改初始密码 先执行命令 mysqld_safe --skip-grant-tables & (设置成安全模式) &,表示在后台运行,不再后台运行的 ...
- 升级到tomcat8遇到The method getDispatcherType() is undefined for the type HttpServletRequest
今天升级到tomcat8,发现原来的项目不能运行了,遇到下面的错误:The method getDispatcherType() is undefined for the type HttpServl ...
- Python基础-shelve模块
shelve模块会生成test.txt.bak.test.txtdat.test.txt.dir三个文件 import shelve f = shelve.open(r'text.txt') f['} ...