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 ...
随机推荐
- Flutter系列博文链接
Flutter系列博文链接 ↓: Flutter基础篇: Flutter基础篇(1)-- 跨平台开发框架和工具集锦 Flutter基础篇(2)-- 老司机用一篇博客带你快速熟悉Dart语法 Flutt ...
- Android 测试 之MonkeyRunner
一.什么是MonkeyRunner monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.通过monkeyrunner,您可以 ...
- Lua学习笔记(4): 字符串
Lua的字符串有3种初始化方式 str = "2333" str = 'hahahaha' str = [[ruarua]] 其中单引号和双引号的初始化方式并无区别,[[]]双中括 ...
- cinder创建volume的流程-简单梳理
1. cinder-api接收到创建的请求,入口:cinder.api.v2.volumes.VolumeController#create,该方法主要负责一些参数的重新封装和校验,然后调用cinde ...
- 1014-C程序的语法树
- Scrum立会报告+燃尽图(十月二十日总第十一次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...
- 【探路者】团队中的每一次感动——Alpha版
我是[探路者]团队的leader翟宇豪.在软件工程课程开始时,当听说有团队作业这个任务时,我个人还是对leader这个角色很期待的.我很希望通过自己的努力,让我所在的团队变得更好,让组里的每一个成员在 ...
- c# 画一个报告
填充控件的grafics属性: private void printReportPage(object sender, System.Drawing.Printing.PrintPageEventAr ...
- Alpha冲刺——第七天
Alpha第七天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- Android工程方法数超过64k,The number of method references in a .dex file cannot exceed 64K.
最近将一个老的Eclipse项目转到Android Studio后,用gradle添加了几个依赖,项目可以make,但是一旦run就报错 Error:The number of method refe ...