Codeforces Round #304 (Div. 2)(CF546D) Soldier and Number Game(线性筛)
题意
给你a,b(1<=b<=a<=5000000)表示a!/b!表示的数,你每次可以对这个数除以x(x>1且x为这个数的因子)使他变成a!/b!/x, 问你最多可以操作多少次使这个数变成1
http://codeforces.com/problemset/problem/546/D
思路
显然要素因子分解,但直接计算a!/b!的素因子个数太慢了,可以发现实际上是计算a(a-1)(a-2)……(b+1),而这些数之积的所有素因子个数之和是等于每个数的素因子个数之和的(相当于对每一个数除以x的操作次数之和),所以我们线性筛的时候计算一下素因子个数即可,最后用前缀和搞一搞O(1)输出。
代码
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=5e6+5;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll sum[N];
int vis[N],prime[N],tot;
void shai()
{
tot=0;
memset(vis,0,sizeof(vis));
for(int i=2;i<N;i++)
{
if(!vis[i])
prime[tot++]=i,sum[i]=1;
for(int j=0;j<tot&&prime[j]*i<N;j++)
{
vis[prime[j]*i]=1;
sum[prime[j]*i]=sum[i]+1;
if(i%prime[j]==0)
break;
}
}
for(int i=2;i<N;i++)
sum[i]+=sum[i-1];
}
int main()
{
std::ios::sync_with_stdio(false);
int n;
shai();
scanf("%d",&n);
while(n--)
{
ll a,b;
scanf("%d%d",&a,&b);
printf("%lld\n",sum[a]-sum[b]);
}
return 0;
}
Codeforces Round #304 (Div. 2)(CF546D) Soldier and Number Game(线性筛)的更多相关文章
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- 数学+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;再来一个前缀和 */ /***************** ...
- 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 ...
- 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 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 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 ...
- queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards
题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...
随机推荐
- 编译 lineageos 14.1 on OnePlus3
breakfast oneplus3 命令的主要作用是 1.执行 vendorsetup.sh 2. variant 默认设置为 userdebug 3. 根据机型判断 调用 lunch cm 或 ...
- java.sql.Date赋值给了java.util.Date.转化成JSONArray时出错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject.defa ...
- 工具资源系列之给 windows 虚拟机装个 ubuntu
前面我们已经介绍了如何在 Windows 宿主机安装 VMware 虚拟机,这节我们将利用安装好的 VMware 软件安装 Ubuntu 系统. 前情回顾 虚拟机为我们在 Windows 宿主机体验别 ...
- Python网络编程基础 ❷ 基于upd的socket服务 TCP黏包现象
TCP的长连接 基于upd的socket服务 TCP黏包现象
- 今天好像找到C语言延迟输出的原因了
有时候运行c 第一行printf就像卡住一样.原来是这样<>>>>>>> int a; printf_s("input one number: ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary 水题
C. p-binary Vasya will fancy any number as long as it is an integer power of two. Petya, on the othe ...
- Introduction to Semidefinite Programming (SDP)
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-251j-introduction-to-mathe ...
- poi实现excel数据的导入和导出
内容来源于网络,侵删. 1.需要的jar包 <dependency> <groupId>org.apache.poi</groupId> <artifactI ...
- 「ZJOI2019」浙江省选
在八月来临前补完了zjoi2019 本来是想在八月前做完暑假作业的? 传送门 Description 给\(n\)条斜率为正的直线,询问每条直线是否在某处高度为前\(m\)名,如果是,询问最小排名 S ...
- flash判断,及安装注意
使用下面方法判断flash版本 function flashChecker() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //fla ...