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 ;
}
最新文章
- EasyPR--开发详解(6)SVM开发详解
- windows server 2012 r2 远程桌面连接指南
- [POJ2420]A Star not a Tree?(模拟退火)
- Windows Driver Frameworks
- 一个ORM的实现(附源代码)
- ThinkPHP框架表单验证
- Reactor模式详解
- C++位操作符总结
- vitrualbox虚拟机64位安装报错解决
- seajs常用API整理
- The requested page cannot be accessed because the related configuration data for the page is invalid
- Unix,windows和Mac中的换行
- emeditor 配置教程
- (1)Two Sum--求数组中相加为指定值的两个数
- zoj 1539 Lot 简单DP 记忆化
- CentOS 5.x 多个ISO文件 安装方法(VMware)
- [Codeforces 940E]Cashback
- iOS 图片裁剪与修改
- Git漏洞允许任意代码执行(CVE-2018-17456)复现
- UGUI合批原理笔记