CF44H Phone Number
题意翻译
给你一个电话号码,根据这个号码生成一个新的号码。生成的规则就是 新号码的第一个数任意选(0-9), 然后之后的每一个新号码都按照以下规则生成:
第i个新号码=(第i-1个新号码+第i个老号码的和)/2
这里如果乘除,则新号码i唯一,否则新号码i可以向上或向下取整。
求总共能生成多少种号码
感谢@夜刀神十香ღ 提供的翻译
题目描述
Alas, finding one's true love is not easy. Masha has been unsuccessful in that yet. Her friend Dasha told Masha about a way to determine the phone number of one's Prince Charming through arithmancy.
The phone number is divined like that. First one needs to write down one's own phone numbers. For example, let's suppose that Masha's phone number is 1234512345 . After that one should write her favorite digit from 00 to 99 under the first digit of her number. That will be the first digit of the needed number. For example, Masha's favorite digit is 99 . The second digit is determined as a half sum of the second digit of Masha's number and the already written down first digit from her beloved one's number. In this case the arithmetic average equals to (2+9)/2=5.5(2+9)/2=5.5 . Masha can round the number up or down, depending on her wishes. For example, she chooses the digit 55 . Having written down the resulting digit under the second digit of her number, Masha moves to finding the third digit in the same way, i.e. finding the half sum the the third digit of her number and the second digit of the new number. The result is (5+3)/2=4(5+3)/2=4 . In this case the answer is unique. Thus, every ii -th digit is determined as an arithmetic average of the ii -th digit of Masha's number and the i-1i−1 -th digit of her true love's number. If needed, the digit can be rounded up or down. For example, Masha can get:
1234512345 9544495444 Unfortunately, when Masha tried dialing the number, she got disappointed: as it turned out, the number was unavailable or outside the coverage area. But Masha won't give up. Perhaps, she rounded to a wrong digit or chose the first digit badly. That's why she keeps finding more and more new numbers and calling them. Count the number of numbers Masha calls. Masha calls all the possible numbers that can be found by the described means of arithmancy, except for, perhaps, her own one.
输入输出格式
输入格式:
The first line contains nonempty sequence consisting of digits from 00 to 99 — Masha's phone number. The sequence length does not exceed 5050 .
输出格式:
Output the single number — the number of phone numbers Masha will dial.
输入输出样例
12345
48
09
15
Solution:
本题先给定了一段数字,那么新生成的在第i位的数推及第i+1位及以后的方案都是固定的,于是我们考虑记忆化搜索。
定义状态$f[i][j]$表示当前在第i位且上一位的答案为j的方案数,则不难得到状态转移方程:$f[i][j]=f[i+1][\lfloor \frac{s[i]+j}{2}\rfloor]+f[i+1][\lceil \frac{s[i]+j}{2} \rceil]$,注意转移时若$s[i]+j$为偶数就不需要加第二个情况。
那么答案为$\sum\limits_{i=0}^{i\leq 9}{f[2][i]}$,坑点是当$i\leq n$都满足$s[i]-s[i-1]\leq 1$时,会出现和原号码一模一样的情况,此时答案还需减1。
代码:
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
ll n,ans,f[N][];
char s[N]; ll dfs(int tot,int lst){
if(tot>n) return ;
if(f[tot][lst]!=-)return f[tot][lst];
int p=(s[tot]^)+lst;
return f[tot][lst]=dfs(tot+,p>>)+(p&?dfs(tot+,p+>>):);
} int main(){
cin>>s+,n=strlen(s+);
if(n==)puts(""),exit();
memset(f,-,sizeof(f));
For(i,,) ans+=dfs(,i);
For(i,,n) if(abs(s[i]-s[i-])>)break; else if(i==n)ans--;
cout<<ans;
return ;
}
CF44H Phone Number的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
随机推荐
- day 12 列表字典 补充
1.列表list的遍历 ##### while遍历 需要len(list) list = [11,22,33,44,55] len_list = len(list) i = 0 while i< ...
- Luogu1445 [Violet]樱花
题面 题解 $$ \frac 1x + \frac 1y = \frac 1{n!} \\ \frac{x+y}{xy}=\frac 1{n!} \\ xy=n!(x+y) \\ xy-n!(x+y) ...
- CF833D Red-Black Cobweb
题面 题解 点分治大火题... 设白边数量为$a$,黑边为$b$,则$2min(a,b)\geq max(a,b)$ 即$2a\geq b\;\&\&2b\geq a$ 考虑点分治时如 ...
- .NET Core单元测试之搞死开发的覆盖率统计(coverlet + ReportGenerator )
.NET Core单元测试之搞死开发的覆盖率统计 这两天在给项目补单元测试,dalao们要求要看一下测试覆盖率 翻了一波官方test命令覆盖率倒是有支持了,然而某个更新日志里面写着 ["Su ...
- springmvc 使用 response 的注意事项以及解决500 空指针异常找不到 response 的方法
使用注解方式在类中(Controller)来装载request时,是可以正常使用request的(必须在启动时才注入,所以不支持热部署),但是同样使用这种方式在已经装载了 request的情况下装载 ...
- Spring学习(五)-----注入bean属性的三种方式( 1: 正常的方式 2: 快捷方式 3: “p” 模式)
在Spring中,有三种方式注入值到 bean 属性. 正常的方式 快捷方式 “p” 模式 看到一个简单的Java类,它包含两个属性 - name 和 type.稍后将使用Spring注入值到这个 b ...
- axios封装(一)基础配置
axios 是目前流行的Promise网络请求库,在浏览器端他通过 xhr方式创建ajax请求.在node环境下,通过 http 库创建网络请求. axios 提供了丰富的配置,这里讲一讲我在工作中通 ...
- hadoop常见错误解决方法
一.启动集群时 1.节点启动失败 1.1端口占用 1.1报错信息:address already in use - bind Address:50070 解决步骤: 查询端口占用:lsof -i:50 ...
- golang笔记1
golang笔记1 go代码是用包来组织的,每个包有一个或多个go文件组成,这些go文件文件放在一个文件夹中 每个源文件开始都用一个package声明,指明本源文件属于哪个包 pakage声明后紧跟这 ...
- sql注入waf绕过简单入门
0x1 白盒 0x2 黑盒 一.架构层 1.寻找源站==> 2.利用同网段==> 3.利用边界漏洞==> ssrf只是一个例子 二.资源限制 Waf为了保证业务运行,会忽略对大的数 ...