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 ...
随机推荐
- django使用流程
1.安装django包 (命令行)>pip install django # conda install django 2.安装成功后,可以新建django项目 1(命令行)>django ...
- STM32运行FreeRTOS出现prvTaskExitError错误死机
文件port.c prvTaskExitError();任务退出错误,一个可能在任务里面写了return,另一个可能任务切换退出问题,入栈和出栈的时候出了问题. static void prvTask ...
- Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子
在Spring,bean可以“访问”对方通过bean配置文件指定相同或不同的引用. 1. Bean在不同的XML文件 如果是在不同XML文件中的bean,可以用一个“ref”标签,“bean”属性引用 ...
- 人脸检测及识别python实现系列(1)——配置、获取实时视频流
人脸检测及识别python实现系列(1)——配置.获取实时视频流 1. 前言 今天用多半天的时间把QQ空间里的几篇年前的旧文搬到了这里,算是完成了博客搬家.QQ空间里还剩下一些记录自己数学学习路线的学 ...
- 剑指offer-二维数组中的查找01
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- <力荐>非常好的正则表达式的详解<力荐>
正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 列目录时, dir *.t ...
- Segments CodeForces 909B (找规律)
Description You are given an integer N. Consider all possible segments (线段,划分)on the coordinate axis ...
- c# 捕获一般获取不到的异常
1.主函数入口加异常事件,代码例如: /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void M ...
- Alpha事后诸葛会议
[设想和目标] Q1:我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? "小葵日记"是为了解决18-30岁年轻用户在记录生活时希望得到一美体验友好 ...
- 博弈---巴什博奕(Bash Game)(博弈入门)
巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规 定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走 ...