1441 士兵的数字游戏
- 6 秒
- 131,072 KB
- 40 分
- 4 级题
两个士兵正在玩一个游戏,游戏开始的时候,第一个士兵为第二个士兵选一个正整数n。然后第二个士兵要玩尽可能多的轮数。每一轮要选择一个正整数x>1,且n要是x的倍数,然后用n/x去代替n。当n变成1的时候,游戏就结束了,第二个士兵所得的分数就是他玩游戏的轮数。
为了使游戏更加有趣,第一个士兵用 a! / b! 来表示n。k!表示把所有1到k的数字乘起来。
那么第二个士兵所能得到的最大分数是多少呢?
收起
输入
单组测试数据。 第一行包含一个整数t (1 ≤ t ≤ 1,000,000),表示士兵玩游戏的次数。 接下来t行,每行包含两个整数a,b (1 ≤ b ≤ a ≤ 5,000,000)。
输出
对于每一组数据,输出第二个士兵能拿到的最多分数。
输入样例
2 3 1 6 3
输出样例
2 5
题解:应该很容易想到,b+1 ~ a 的所有因子个数,每个数的因子个数 = (素数的指数 + 1 连乘),可以想到埃氏筛。
快读优化后,时间为 4s 多
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=(tree[rt<<1],tree[rt<<1|1]);
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1) (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=5e6+5;
int a[N];
ll sum[N];
void fun(){
a[0]=a[1]=1;
sum[0]=0; sum[1]=0;
for(int i=2;i<N;i++){
if(!a[i]){
// printf("i == %d\n",i);
sum[i]=1;
for(int j=i+i;j<N;j+=i){
a[j]=1;
int n=j,num=0;
while(n%i==0){
num++;
n/=i;
}
sum[j]+=num;
}
}
}
// printf("6 == %lld\n",sum[6]);
for(int i=1;i<N;i++)
sum[i]+=sum[i-1];
}
int main(){
int t,a,b;
fun();
scanf("%d",&t);
while(t--){
a=read();
b=read();
printf("%lld\n",sum[a]-sum[b]);
}
return 0;
}