CodeForces 546 D. Soldier and Number Game(素数有关)
Description
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.
Sample Input
Input Output
题目链接:http://codeforces.com/problemset/problem/546/D
********************************************
分析:用到一个公式
primefactors[a] = primefactors[a / primediviser[a]] + 1,
primefactor[a]表示a因数中素数的个数。
只要把a,b的primefactor[]算出来,减一下就行了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std; #define N 5000000
#define INF 0x3f3f3f3f int c[N]; void p()
{
memset(c,,sizeof(c));
int i,j;
for(i=;i<=N;i++)
{
if(c[i]==)
{
for(j=;j*i<=N;j++)
c[j*i]=c[j]+;
}
}
for(i=;i<=N;i++)
c[i]+=c[i-];
} int main()
{
int T,a,b;
p();
scanf("%d", &T); while(T--)
{
scanf("%d%d", &a,&b);
printf("%d\n", c[a]+a-c[b]-b);
}
return ;
}
CodeForces 546 D. 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 546D】Soldier and Number Game
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces D546:Soldier and Number Game
题目链接 输入t对数 a, b 求(b,a]内的每个数拆成素因子的个数和 这里每个数都可以写成素数的乘积,可以写成几个素数的和就有几个素因子,这里求的是(b,a]内的素因子和 思路: 素数的素因子个数 ...
- codeforces Soldier and Number Game(dp+素数筛选)
D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...
- Codeforces J. Soldier and Number Game(素数筛)
题目描述: Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- 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 ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- 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 ...
- 数学+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;再来一个前缀和 */ /***************** ...
随机推荐
- 理解 Objective-C 的 ARC
英文原文:Understanding Automatic Reference Counting in Objective-C 自动引用计数(Automatic Reference Counting, ...
- Openjudge-计算概论(A)-取石子游戏
描述: 有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍.最后谁能够把一堆石子取空谁就算赢. 比如初始的时候两堆石子的数目是25和7 2 ...
- CSS中background:url(图片) 不能显示的问题
刚刚碰到一个奇怪的问题,这样一段CSS代码: .pho6 { background: url(img/pho6.jpg); } 这段代码居然不能显示出背景图片,路经绝对是没错的代码肯定没有问题, ...
- 1010 Robot Motion
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- AltiumDesigner14绘制四层板设置
1,快捷键(O+K)进入板层设置界面: 2,选择AddLayer,里边有两个选项(add layer(添加信号层)||add internal plane(增加平面)) 四层板的话一般层次的划分是t ...
- log4j配置详解[http://www.iteye.com/topic/378077]
log4j是一个非常强大的log记录软件,下面我们就来看看在项目中如何使log4j. 首先当然是得到log4j的jar档,推荐使用1.2.X版,下载地址: http://logging.apache. ...
- POJ 3548 Restoring the digits
暴力搜索.注意题目说每个字母对应的数字不同,这句话表明最多只有10个字母,所以暴力DFS绝对不会TLE. #include<cstdio> #include<cstring> ...
- Windows进程间通信(中)
二.文件映射 文件映射(Memory-Mapped Files)能使进程把文件内容当作进程地址区间一块内存那样来对待.因此,进程不必使用文件I/O操作,只需简单的指针操作就可读取和修改文件的内容. W ...
- nmon的安装与使用
nmon的安装与使用 1.下载 nmon:http://nmon.sourceforge.net/pmwiki.php?n=Site.Download nmonanalyser http://www. ...
- 规划(纪念我在ACM道路上的一年)
现在已经是晚上一点了,我早早的躺在床上,不能入睡,因为睡觉前看了一下我们学校今年区域赛的成绩总结,派出八次队伍,七个铜-- 再加上这两天ACM迎新杯的筹备过程的问题,让我产生了深深的思考-- 去年司老 ...