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 ...
随机推荐
- kubernetes-控制器Deployment和DaemonSet(八)
Pod与controllers的关系 •controllers:在集群上管理和运行容器的对象•通过label-selector相关联•Pod通过控制器实现应用的运维,如伸缩,升级等 控制器又称工作负载 ...
- 01_13_JSP编译指令
01_13_JSP编译指令 1. Directive Directive(编译指令)相当于在编译期间的命令 格式: <%@Directive 属性=”属性值”%> 常见的Directive ...
- 洛谷P2468 [SDOI2010]粟粟的书架(二分答案 前缀和 主席树)
题意 题目链接 给出一个矩形,每个点都有一些值,每次询问一个子矩阵最少需要拿几个数才能构成给出的值 Sol 这题是真坑啊.. 首先出题人强行把两个题拼到了一起, 对于前$50 \%$的数据,考虑二分答 ...
- mysql 查询 7天内的数据
SELECT ID,SERVICE FROM new_schedules_spider_full WHERE SERVICE = 'WSA2' and date_sub(curdate(), inte ...
- ARM协处理器
协处理器是一种芯片,用于减轻系统微处理器的特定处理任务.例如,数学协处理器可以控制数字处理:图形协处理器可以处理视频绘制.例如,intel pentium微处理器就包括内置的数学协处理器. 协处理器 ...
- vue.js devtools安装
在调试的时候chrome会提示安装vue devtools,我以为是要在chrome的程序商店直接安装,但是国内对谷歌的和谐,无法访问谷歌商店内容 官方有源代码,可以下载下来自行编译安装 官方地址:h ...
- 使用VUE开发
<一>VUE的开发分两种,一种是直接在HTML文件中使用,一种是VUE文件的形式开发 1,首先我们先让 HTML 文件支持 VUE 的语法指令提示 2,File -> Setting ...
- JZOJ 3509. 【NOIP2013模拟11.5B组】倒霉的小C
3509. [NOIP2013模拟11.5B组]倒霉的小C(beats) (File IO): input:beats.in output:beats.out Time Limits: 1000 ms ...
- JAVA使用JDBC连接,修改MySQL数据库(比较乱)
原文地址1(连接MySQL图文) : http://www.cnblogs.com/GarfieldEr007/p/5746137.html 原文地址2 (修改MySQL图文): http://www ...
- (原创)task和function语法的使用讨论(Verilog,CPLD/FPGA)
1. Abstract function和task语句的功能有很多的相似之处,在需要有多个相同的电路生成时,可以考虑使用它们来实现.因为个人使用它们比较少,所以对它们没有进行更深的了解,现在时间比较充 ...