Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化
3 seconds
256 megabytes
standard input
standard output
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?
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.
For each game output a maximum score that the second soldier can get.
2
3 1
6 3
2
5 题意: t组数据 给你a b 两个数(a>=b)
两个人进行一个游戏 初始时第一个人挑选一个数n给第二个人 第二个人选择一个n的因子k使得n/k尽可能的大
使得n=n/k 并得1分 继续选择当前n的因子 继续游戏 当n==1时游戏结束 问第二个人的最大得分为多少?
这里n=a!/b!
题解: “数学上来先打表” 这句话堪称精辟
首先a b的范围很大 暴力处理是不可能的
分解开来n=a!/b!=(b+1)*(b+2).....*a; 为了使得n被多次分解 可以很容易的考虑到求每一项的质因子分解
(只有是质因子分解,才能够使得分解后的因子个数最多 不做解释)并且统计所有的分解后的因子的个数之和就是答案
对于询问1e6不可能每次都遍历去求
考虑打表求1~5000000的每个数的质因子分解的个数 并且暴力去求每个数的质因子必然超时 考虑记忆化
对于已经找过质因子个数的数进行存储 方便之后的数直接使用 免于重复计算
并且只需要找到当前数的一个因子就可以 其他的已经被计算过 这就是记忆化的优化所在点
并且记录前缀和
对于询问直接输出 sum[a]-sum[b]
//code by drizzle
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int t;
int a,b;
int sum[];
int have[];
void fun()
{
sum[]=;
sum[]=;
have[]=;
for(int i=;i<=;i++)
{
if(i%==)//只需要找到当前数的一个因子就可以 其他的已经被计算过
{
have[i]=+have[i/];
sum[i]=sum[i-]+have[i];
continue;
}
int flag=;
for(int j=;j*j<=i;j+=)
{
if(i%j==)
{
have[i]=+have[i/j];
sum[i]=sum[i-]+have[i];
flag=;
break;
}
}
if(flag==)
{
have[i]=;
sum[i]=sum[i-]+have[i];
}
}
}
int main()
{
fun();
scanf("%d",&t);
for(int i=;i<=t;i++)
{
scanf("%d %d",&a,&b);
printf("%d\n",sum[a]-sum[b]);
}
return ;
}
Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化的更多相关文章
- Codeforces Round #601 (Div. 2)E(寻找质因子,DP)
先分解质因数,对于当前a[i],假设当前的质因数为x,这个位置要满足能被k整除,有两个可能,要么是它向后一个转移x%k个,要么是后一个向它转移k-x%k个. 对于每一个a[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;再来一个前缀和 */ /***************** ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- 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 ...
- 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) B. Soldier and Badges【思维/给你一个序列,每次操作你可以对一个元素加1,问最少经过多少次操作,才能使所有元素互不相同】
B. Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #304 (Div. 2) -----CF546
A. Soldier and Bananas A soldier wants to buy w bananas in the shop. He has to pay k dollars for t ...
随机推荐
- 【Python全栈-CSS】background背景
background背景 一.背景图片 background-image: url("img/num.png"); background-position-x: -200px ; ...
- 阿里云服务器下安装LAMP环境(CentOS Linux 6.3) 安装与配置 FTP 服务器
我们经常会使用 FTP,把本地电脑上的文件上传到服务器上,或者把服务器上的文件下载到自己的电脑里面.FTP 有服务端和客户端,FTP 的服务端提供了这种传输文件的服务,FTP 的客户端提供了传输文件的 ...
- Redis学习记录(三)
1.Redis集群的搭建 1.1redis-cluster架构图 架构细节: (1)所有的redis节点批次互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. (2)节点的fail ...
- java基础面试题:java中实现多态的机制是什么?
靠的是父类或接口的引用指向子类或实现类的对象, 调用的方法是内存中正在运行的那个对象的方法.
- Centos7 PHP的安装和配置
前面Nginx和httpd的安装都是为了支持PHP而弄的,然后这个目标就给了我一沉重的打击,等我慢慢道来,先来说说PHP的安装和配置吧. 一.PHP的安装 1.由于linux的yum源不存在php7. ...
- Mycat高可用解决方案三(读写分离)
Mycat高可用解决方案三(读写分离) 一.系统部署规划 名称 IP 主机名称 配置 192.168.199.112 mycat01 2核/2G Mysql主节点 192.168.199.110 my ...
- ob缓存的基本使用
在页面 加载的时候 如果 图片 很多 很大 会造成页面的阻塞降低用户体验 我们在点击页面的时候可以使用OB缓存 整个页面, 当用户点击的时候直接请求的是我们预先准备好的html页面 .也降低了我们数据 ...
- 科学计算库Numpy——数组形状
改变数组维数 给数组的shape属性赋值,改变数组的维数.数组的大小是不能改变的. 增加维度 使用np.newaxis增加维度. 删除维度 使用squeeze()删除维度是1的维度,也就是删除shap ...
- 将Excel文件转为csv文件的python脚本
#!/usr/bin/env python __author__ = "lrtao2010" ''' Excel文件转csv文件脚本 需要将该脚本直接放到要转换的Excel文件同级 ...
- 动态规划、记忆化搜索:HDU1978-How many ways
Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标 ...