51nod 1441 士兵的数字游戏 质数筛选

题目来源: CodeForces
基准时间限制:6 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
 收藏
 关注

两个士兵正在玩一个游戏,游戏开始的时候,第一个士兵为第二个士兵选一个正整数n。然后第二个士兵要玩尽可能多的轮数。每一轮要选择一个正整数x>1,且n要是x的倍数,然后用n/x去代替n。当n变成1的时候,游戏就结束了,第二个士兵所得的分数就是他玩游戏的轮数。

为了使游戏更加有趣,第一个士兵用 a! / b! 来表示n。k!表示把所有1到k的数字乘起来。

那么第二个士兵所能得到的最大分数是多少呢?

Input
单组测试数据。
第一行包含一个整数t (1 ≤ t ≤ 1,000,000),表示士兵玩游戏的次数。
接下来t行,每行包含两个整数a,b (1 ≤ b ≤ a ≤ 5,000,000)。
Output
对于每一组数据,输出第二个士兵能拿到的最多分数。
Input示例
2
3 1
6 3
Output示例
2
5


做一个预处理的质数筛 但是用C++提交还是会T
但是换成Visual C++交就能A了....
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define INFLL   0x3f3f3f3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;
typedef pair<int, int> PII;
using namespace std;

const int maxn = 5e6 + 5;

int prime[maxn];
LL sum[maxn];

void Init() {
    prime[1] = 0;
    for(int i = 2; i < maxn; i++) {
        if(prime[i]) continue;
        for(int j = i; j < maxn; j += i) {
            int tmp = j;
            while(tmp % i == 0) {
                tmp /= i;
                sum[j]++;
            }
            prime[j]++;
        }
    }
    for(int i = 2; i <= maxn; i++) sum[i] += sum[i - 1];
}

int main() {
    //FIN
    int T;
    Init();
    scanf("%d", &T);
    while(T--) {
        int a, b;
        scanf("%d%d", &a, &b);
        printf("%lld
", sum[a] - sum[b]);
    }
}

  

原文地址:https://www.cnblogs.com/Hyouka/p/7448144.html