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 ...
随机推荐
- matlab一次读取多张图片
实验平台:matlab R2010Rb 读取C:\Users\KCl\Documents\MATLAB\SRCNN\Set5文件夹下所有bmp文件,并存储到im字典中 clear all clc im ...
- 安装Ubuntu桌面环境后只能Guest登录的解决办法
1.安装Ubuntu桌面环境后,登录界面只显示了Guest 2.在登录界面按住crtl+shift+F1,进入tty模式 3.输入sudo -s进入root模式 4.输入vi /etc/lightdm ...
- pyqt4 python2.7 中文乱码的解决方法
import sysimport localefrom PyQt4.QtGui import *from PyQt4.QtCore import *from untitled import Ui_Di ...
- Oracle表连接学习笔记
目录 一.表连接类型 1.1 内连接 1.2 外连接 二.表连接方法 2.1 表连接方法分类 2.2 表连接方法特性区别 @ 一.表连接类型 表连接类型可以分为:内连接.外连接,在看<收获,不止 ...
- C#自增运算符(++)
一.C#自增运算符(++) 自增运算符(++)是将操作数加1. 1. 前缀自增运算符 前缀自增运算符是“先加1,后使用”.它的运算结果是操作数加1之后的值. 例如: ++x; // 前缀自增运算符 ...
- C# 接口慨述
接口(interface)用来定义一种程序的协定.实现接口的类或者结构要与接口的定义严格一致.有了这个协定,就可以抛开编程语言的限制(理论上).接口可以从多个基接口继承,而类或结构可以实现多个接口.接 ...
- 处理侧滑返回与 ScrollView 手势冲突
与处理双击.单击手势互斥原则一样: // 手势互斥(侧滑返回手势失效后才响应UITableView的滑动手势) [tableView.panGestureRecognizer requireGestu ...
- nginx Keepalived高可用集群
一.Keepalived高可用 1.简介 Keepalived软件起初是专为LvS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此, ...
- Window_Bat_Scripts—检测特定网段未使用的IP地址
1.1 脚本名称 Check_IP_Not_Use.bat 1.2 脚本代码 @Echo off set /p input_number=请输入网络位(192.168.1.): IF EX ...
- 水平垂直居中图片及文字(兼容IE6+)实例
直接看代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...