Codeforce 546D
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
2
3 1
6 3
2
5
#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <cstdlib> #include <string> #include <queue> #include <map> #include <stack> using namespace std; typedef long long LL; const LL INF = 1 << 31; const int MAXN = 5000005; int pri[5000], vis[MAXN], res[MAXN], c = 1; void getpri() //刷选2500以内的素数, 同时vis[]判断 500w以内的数是否是素数 { memset (vis, 0, sizeof vis); vis[0] = vis[1] = 1; for ( int i = 2; i < 2500; ++i) if (!vis[i]) { pri[c++] = i; for ( int j = i * i; j < MAXN; j += i) vis[j] = 1; } } int main() { int ans, g, flag; getpri(); res[1] = res[0] = 0; for ( int i = 2; i < MAXN; ++i) { g = i; ans = 0; if (!vis[i]) ans = 1; //素数的结果不用除, 只有自己本身一个因子 else { flag = 0; for ( int j = 1; g != 1 && j < c; ++j) { //要想把 500w以内的合数都分解为质数, <br> //只需要一直尝试除于 sqrt(500w)内的质数就好了 //因为对于 一个合数n, 它总有一个 质因子小于或等于 sqrt(n) while (g % pri[j] == 0) { g /= pri[j]; ans++; if (!vis[g]) { ans++;flag = 1; break ; } //此步必须要有, 在试除的过程中, 若已为质数, 及时推出 //另一方面, 如n = 13 * 11111, 由于只刷选到 2500内的质数, 11111不能检测到 } if (flag) break ; } } res[i] = res[i - 1] + ans; } int t, a, b; scanf ( "%d" , &t); while (t--) { scanf ( "%d%d" , &a, &b); printf ( "%d\n" , res[a] - res[b]); } }<br><br>另一种做法是:充分利用已经得到的结果, 如 res[100] = res[2] + res[100 / 2] 其中由于 2 和 50 都小于 100, 故已经算出来了 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧