CF261E Maxim and Calculator

洛谷评测传送门

题目描述

Maxim has got a calculator. The calculator has two integer cells. Initially, the first cell contains number 11 , and the second cell contains number 00 . In one move you can perform one of the following operations:

  1. Let's assume that at the current time the first cell contains number aa , and the second cell contains number bb . Write to the second cell number b+1b+1 ;
  2. Let's assume that at the current time the first cell contains number aa , and the second cell contains number bb . Write to the first cell number a·bab .

Maxim is wondering, how many integers xx (l<=x<=r)(l<=x<=r) are there, such that we can write the number xx to the first cell of the calculator, having performed at most pp moves.

输入格式

The first line contains three integers: ll , rr , pp (2<=l<=r<=10^{9},1<=p<=100)(2<=l<=r<=109,1<=p<=100) .

The numbers in the line are separated by single spaces.

输出格式

In a single line print a single integer — the answer to the problem.

题意翻译

二元组(a,b)(a,b),可以变成(a,b+1)(a,b+1)或(ab,b)(a**b,b)

你有初始二元组(1,0)(1,0),给你区间[l,r][l,r],和一个整数pp,在区间内选一个数xx,使(1,0)(1,0)在不超过pp步变化后,第一维的值变成xx,求xx的个数

Translated by @Fheiwn

输入输出样例

输入 #1复制

输出 #1复制

输入 #2复制

输出 #2复制

输入 #3复制

输出 #3复制

题解:

这是2019.10.16模拟赛的一道题。

一开始我发现,如果每次操作都加的话,那么操作数对应的答案都是可行的(这很显然)。但是这样肯定不会是答案,所以我们应该继续考虑一种情况:操作二,乘法运算。

然后我想到了整数的唯一分解定理:一个正整数可以被唯一分解成若干个质数的乘积。

这个思路紧紧地抓住了我的心//那么,对于一个数,我们肯定可以用算术基本定理把它分成若干个数的乘积,那么,如果这些数最大的那个和这些数的个数的和(最大的数+数的个数)比\(p\)小,那么这个数就是合法的。

证明是显然的:我们操作的过程是从1累加,每逢到了要作为\(a\)的质因子的数,就多用一次操作乘过去,这样,一直到了最后的质因子(也是最大的),就能乘出\(a\)。

这样就确定了我们的判定思路。但是如果暴力枚举的话,TLE是没跑的。

所以,我们进一步开始细细地琢磨怎么降低复杂度。

通过刚刚的讲解,我们发现,如果有一个数被唯一分解成若干个质数的乘积,并且其最大的质因子要比\(p\)还要大,那么这个数就根本不可能合法。

所以,我们发现,\(b\le p\)是必须的。所以,我们就可以通过线性筛选处理出\(\le p\)的所有质数,并且通过深搜来处理出所有能用这些质数表示出来(乘出来)的数。

这里简单说一下为什么要用深搜:根据算术基本定理,我们会有很多质数来乘成一个合数。而且,这些质数是可以重复的:也就是说,普通的循环遍历根本不会满足这样的要求,所以需要通过递归求解(即深搜)。

以上都是预处理部分,现在我们要检验这些数是否能在$\le p $的次数内出解。

(到这个地方本蒟蒻就不会了。模拟赛40分*¥%)

这个地方要用\(DP\)求解......

设\(f[i] [j]\)表示把第一元变成\(i\),第二元变成\(j\)所用的操作二的最少次数。为什么这么设置呢?因为操作一作为加法,是可以直接计算出来的。而且,因为这样设置转移方程一定会导致爆空间,第二维(表示第二元)一定是要被滚动掉的。

所以,我们对深搜处理出的数列进行排序。外层枚举\(2-p\),内层枚举\(1-tot\)(\(tot\)表示数列的长度),如果\(i|a[j]\),则用单调队列指针寻找\(i\times a[k]=a[j]\),这时就用\(dp[k]\)更新\(dp[j]\),判断其加上数的个数(\(i\))是否超过了\(p\)。

代码如下:(注意空间范围,不然会死得超级惨)

#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const int maxp=3*1e6+1;
int l,r,p,ans;
bool v[maxp],vis[maxp];
int a[maxp],prime[maxp],dp[maxp],tot,cnt;
void euler(int n)
{
cnt=0;
for(int i=2;i<=n;i++)
{
if(!v[i])
prime[++cnt]=i;
for(int j=1;j<=cnt;j++)
{
if(i*prime[j]>=p)
break;
v[i*prime[j]]=1;
if(i%prime[j]==0)
break;
}
}
}
void scan(int x,int step)
{
a[++tot]=x;
for(int i=step;i<=cnt;i++)
{
if(prime[i]*(ll)x<=r)
scan(prime[i]*x,i);
else
return;
}
}
int main()
{
scanf("%d%d%d",&l,&r,&p);
euler(p-1);
scan(1,1);
sort(a+1,a+tot+1);
memset(dp,0x3f,sizeof(dp));
dp[1]=0;
for(int i=2;i<=p;i++)
{
int k=1;
for(int j=1;j<=tot;j++)
if(a[j]%i==0)
{
while(a[k]*i<a[j])
k++;
dp[j]=min(dp[j],dp[k]+1);
if(dp[j]+i<=p)
vis[j]=1;
}
}
for(int i=1;i<=tot;i++)
if(vis[i] && a[i]>=l)
ans++;
printf("%d",ans);
return 0;
}

CF261E Maxim and Calculator的更多相关文章

  1. CF261E Maxim and Calculator (质数,完全背包)

    CF261E Maxim and Calculator 题目大意: 有两个初始参数 $ a=1 $ , $ b=0 $ ,你可以对它们进行两个操作: $ b~+=1 $ 或 $ a~\times =b ...

  2. [CF261E]Maxim and Calculator_搜索_欧拉筛素数_动态规划

    Maxim and Calculator 题目链接:https://www.luogu.org/problem/CF261E 数据范围:略. 题解: 考试的时候只会暴力,学弟太强了$\%\%\% Or ...

  3. Codeforce 水题报告

    最近做了好多CF的题的说,很多cf的题都很有启发性觉得很有必要总结一下,再加上上次写题解因为太简单被老师骂了,所以这次决定总结一下,也发表一下停课一星期的感想= = Codeforces 261E M ...

  4. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  5. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  6. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  7. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  8. Windows Universal 应用 – Tip Calculator

    声明 以下内容取材于 Bob Tabor 的课程<Windows Phone 8.1 Development for Absolute Beginners>,链接地址为:http://ww ...

  9. Calculator(1.5)

    Calculator(1.5) Github链接 ps.负数的处理未完成 解题过程中遇到的困难和解决 <stack>的使用: 认真研究了栈,基本上掌握了用法,与<queue>的 ...

随机推荐

  1. <DP> (高频)139 375 374 (DP hard)312

    139. Word Break 返回结果较为简单可用dp, 复杂用dfs class Solution { public boolean wordBreak(String s, List<Str ...

  2. 在windows上搭建git服务器教程

    1.首先,需要确保windows系统上安装并配置了Java运行环境,JDK>=1.7. 2.下载Gitblit,下载地址:http://www.gitblit.com/ 3.解压缩下载的压缩包即 ...

  3. Spring Boot(十二):LocalDateTime格式化处理

    Java 8之后,日期类的处理建议使用java.time包中对应的LocalDateTime, LocalDate, LocalTime类.(参考Java8新特性) 在Spring Boot中(验证版 ...

  4. 绿联Type-C千兆网卡AX88179芯片驱动(苹果Mac OSX系统)CM141丨CM179

    绿联Type-C千兆网卡AX88179芯片驱动(苹果Mac OSX系统)CM141丨CM179 下载地址:https://www.lulian.cn/download/6-cn.html AX8817 ...

  5. 九、Spring之BeanFactory源码分析(一)

    Spring之BeanFactory源码分析(一) ​ 注意:该随笔内容完全引自https://blog.csdn.net/u014634338/article/details/82865644,写的 ...

  6. tensorflow: arg_scope()

    with arg_scope(): 1.允许我们设定一些共享参数,并将其进行保存,必要时还可以嵌套覆盖 2.在指定的函数调用时,可以将一些默认参数塞进去. 接下来看一个tensorflow自带的例子. ...

  7. Python term 模块

    Python term 模块 term 模块适用于文字编辑,样式切换,光标移动 等一系列操作适用于终端命令行文字光标处理等操作. 安装 pip install py-term 使用方式 方式一 imp ...

  8. Quartz的配置与使用

    什么是Quartz Quartz是OpenSymphony开源组织在Job scheduling领域的开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为 ...

  9. LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold

    题目 我是按照边进行二分的 class Solution { public: int sum[100005]; int a[305][305]; int maxSideLength(vector< ...

  10. 页面直接导出为PDF文件,支持分页与页边距

    将WEB页面直接导出为pdf文件是经常会用到的一个功能,尤其是各种报表系统.总结了一下目前几种主流的做法: 在后端用代码生成pdf文件,比如iText一类: 在后端抓取页面并生成pdf文件,比如pha ...