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 ...
随机推荐
- [agc011F]Train Service Planning-[线段树优化dp+神秘思考题]
Description 传送门 Solution 请围观lhx大佬的博客(大佬写的太好了我都没有写的动力了em) Code #include<iostream> #include<c ...
- day2 Ubuntu配置源
1.寻找国内镜像源 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 2.配置source list源 sources.list系统自带的,源是来U ...
- Yii2.0 高级模版编写使用自定义组件(component)
翻译自:http://www.yiiframework.com/wiki/760/yii-2-0-write-use-a-custom-component-in-yii2-0-advanced-tem ...
- Python接口测试实战5(下) - RESTful、Web Service及Mock Server
如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...
- 爬虫2.3-scrapy框架-post、shell、验证码
目录 scrapy框架-post请求和shell 1. post请求 2. scrapy shell 3. 验证码识别 scrapy框架-post请求和shell 1. post请求 scrapy框架 ...
- Fast R-CNN学习总结
Fast R-CNN是R-CNN的改良版,同时也吸取了SPP-net中的方法.在此做一下总结. 论文中讲到在训练阶段,训练一个深度目标检测网络(VGG16),训练速度要比R-CNN快9倍左右,比SPP ...
- hibernate.hbm.xml文件配置入门小结(1)
在Hibernate中,各表的映射文件xxx.hbm.xml可以通过工具生成,例如在使用MyEclipse开发时,它提供了自动生成映射文件的工具. hibernate.hbm.xml文件的基本结构如下 ...
- 数据库与数据仓库的比较Hbase——Hive
数据仓库(Data Warehouse)是一个面向主题的(Subject Oriented).集成的(Integrate).相对稳定的(Non-Volatile).反映历史变化(Time Varian ...
- nginx原声方法按照每天日志切割保存
首先配置日志变量,然后配置日志 在/etc/nginx/conf.d/default.conf 配置变量 server{ if ($time_iso8601 ~ "^(\d{4})-(\d{ ...
- Manacher算法——求最长回文子串
首先,得先了解什么是回文串.回文串就是正反读起来就是一样的,如“abcdcba”.我们要是直接采用暴力方法来查找最长回文子串,时间复杂度为O(n^3),好一点的方法是枚举每一个字符,比较较它左右距离相 ...