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;再来一个前缀和 */ /***************** ...
随机推荐
- Openjudge-计算概论(A)-1的个数
描述: 给定一个十进制整数N,求其对应2进制数中1的个数输入第一个整数表示有N组测试数据,其后N行是对应的测试数据,每行为一个整数.输出N行,每行输出对应一个输入.样例输入 4 2 100 1000 ...
- ural 1013. K-based Numbers. Version 3(动态规划)
1013. K-based Numbers. Version 3 Let’s consider K-based numbers, containing exactly N digits. We def ...
- 元素NULL判断
元素取值val() val()方法主要用来获取form元素的值像input select textarea.在对select取值的时候当没有option被选定时val()会返回null,至少一个opt ...
- Spring 接收表单List集合数据
4种方法,详情见:http://blog.csdn.net/lutinghuan/article/details/46820023 第一种: 1.新增类,其中有属性为该对象的集合 public cla ...
- opencv使用convexityDefects计算轮廓凸缺陷
引自:http://www.xuebuyuan.com/1684976.html http://blog.csdn.net/lichengyu/article/details/38392473 htt ...
- OC工程调用Swift方法
1.建一个OC工程命名为SwiftOC.如图所示: 2.新建一个swfit文件命名为Test.swift,会弹出提示,选择Create Bridging Header建立桥接文件,系统会建立“工程名- ...
- iosUISegmentedControl的基本设置
//创建segmentControl 分段控件 UISegmentedControl *segC = [[UISegmentedControl alloc]initWithFrame:CGRectMa ...
- Chapter 2 Open Book——20
Jessica pulled on my arm. Jessica拉了一下我的手臂. "Hello? Bella? What do you want?"I looked down; ...
- 5.如果第4题中在DOS命令下输入:java Hello 出现以下结果:Bad command or the file name 可能是什么原因?请说明理由。
1.没有输入javac指定路径,而是直接输入java Hello. 2.前面已经用过一次指令,没有重新输入路径.
- 利用proxychains在终端使用socks5代理
最近用各种脚本下载东西的时候发现有的站点需要当地IP才能下,比如.....nico, youtube等: 所以就找了下能在终端用socks5代理的工具,最后找到了proxychains,从此再无压力= ...