题目描述:

Inna and Nine

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Inna loves digit 9 very much. That's why she asked Dima to write a small number consisting of nines. But Dima must have misunderstood her and he wrote a very large number a, consisting of digits from 1 to 9.

Inna wants to slightly alter the number Dima wrote so that in the end the number contained as many digits nine as possible. In one move, Inna can choose two adjacent digits in a number which sum equals 9 and replace them by a single digit 9.

For instance, Inna can alter number 14545181 like this: 14545181 → 1945181 → 194519 → 19919. Also, she can use this method to transform number 14545181 into number 19991. Inna will not transform it into 149591 as she can get numbers 19919 and 19991 which contain more digits nine.

Dima is a programmer so he wants to find out how many distinct numbers containing as many digits nine as possible Inna can get from the written number. Help him with this challenging task.

Input

The first line of the input contains integer a (1 ≤ a ≤ \(10^{100000}\)). Number a doesn't have any zeroes.

Output

In a single line print a single integer — the answer to the problem. It is guaranteed that the answer to the problem doesn't exceed \(2^{63 - 1}\).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

Copy

369727

Output

Copy

2

Input

Copy

123456789987654321

Output

Copy

1

Input

Copy

1

Output

Copy

1

Note

Notes to the samples

In the first sample Inna can get the following numbers: 369727 → 99727 → 9997, 369727 → 99727 → 9979.

In the second sample, Inna can act like this: 123456789987654321 → 12396789987654321 → 1239678998769321.

思路:

题目是要给一个数字串,两个相邻的数字加起来是9的话可以合并,问在9最多的情况下最多可以得到多少种不同的数字串。

刚开始看这道题看起来非常麻烦,这个,,,怎么做,我要怎么找有几个加起来等于9还不同的情况,确定一个什么策略可以区分并尝试加成不同的数,一个一个试?递归?动态规划?看一眼数据范围,直接10^5,限时一秒枚举不够啊。这就说明有更简洁的方法。

只能一个一个找规律了。

我们现看看样例14545181,这个九要最多,只能19991或19919,也就是说中间的4545刚好组成两个9就不能拆开,拆开会少。那么什么情况下会有多种可能呢?我们把注意力转向181,就是这个产生了两种可能。

我们便发现,要想有多种可能,必须满足一个性质:一个数和左边加起来是9,和右边加起来也是9

举个栗子:1454541

我们发现两边的1对结果毫无影响,因为它们并不能凑一个9出来,于是去掉,得到45454,这有几种情况呢?994,949,499三种,这个串里面我们发现刚好有5,4,5这三个数满足上面的性质

再来:454,有94和49两种,有5一个数满足上面的性质

数在长一点:4545454有4999,9499,9949,9994四种,满足上述性质的串中的数有5,4,5,4,5这五个

要是长度为偶数呢?4545,我们发现只有99一种可能,而其中满足性质的有5,4两个数

发现规律没有?只有当满足性质的数有奇数个,才有多种可能,偶数个只可能为一

而且满足性质的数和产生的可能性有如下关系:\(a_n=(n+3)/2\),\(n\)为满足性质的数的个数,且为奇数,\(a_n\)为可能的种数。

现在问题只剩下如何识别具有这种模式的串中数字的个数。就按性质来识别就好了,遍历除开头结尾的每个数,看是不是满足性质,是就把个数加一。注意的是满足性质的字串不一定是连续的,串断裂后及时把计数变量清零。还有注意个数为偶数的情况并不贡献可能性。

代码:

#include <iostream>
#include <string>
using namespace std;
string s;
long long judge(string s)
{
long long ans = 1;
long long num = 0;
for(int i = 1;i<s.size()-1;i++)
{
if(s[i-1]+s[i]-2*'0'==9&&s[i]+s[i+1]-2*'0'==9)
{
num++;
}
else if(num%2==1)
{
ans = ans*((num+3)/2);
//cout << "num " << num << endl;
num = 0;
}
else
{
num = 0;
}
}
if(num%2==1)
{
ans = (ans*(num+3)/2);
}
return ans;
}
int main()
{
cin >> s;
long long ans = judge(s);
cout << ans << endl;
return 0;
}

Codeforces I. Inna and Nine(组合)的更多相关文章

  1. codeforces 374A Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行  m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...

  2. Codeforces 374A - Inna and Pink Pony

    原题地址:http://codeforces.com/contest/374/problem/A 好久没写题目总结了,最近状态十分不好,无论是写程序还是写作业还是精神面貌……NOIP挂了之后总觉得缺乏 ...

  3. CodeForces 400A Inna and Choose Options

    Inna and Choose Options Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on Cod ...

  4. codeforces 499A.Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...

  5. Codeforces 374B - Inna and Nine

    原题地址:http://codeforces.com/problemset/problem/374/B 这道题没什么难度,但是考场上就是没写对.Round #220彰显了它的逗比性质——这道题的“标算 ...

  6. codeforces C. Inna and Huge Candy Matrix

    http://codeforces.com/problemset/problem/400/C 题意:给你一个n*m的矩阵,然后在矩阵中有p个糖果,给你每个糖果的初始位置,然后经过x次顺时针反转,y次旋 ...

  7. Codeforces.1096E.The Top Scorer(组合)

    题目链接 感觉这题很裸啊,除了看着恶心点也没什么了,怎么过的人那么少.. \(Description\) 给定\(n,r,s\),表示有\(n\)个人,设每个人的得分是非负整数\(a_i\),已知第一 ...

  8. Codeforces 374C - Inna and Dima

    374C - Inna and Dima 思路:dfs+记忆化搜索 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  9. Codeforces 374D - Inna and Sequence

    374D - Inna and Sequence 思路: 树状数组+二分 因为被删的点最多N=1e6个,所以复杂度N*logN*logN 前段时间做过一道一样的题,这类题基本套路二分找没删除前的位置 ...

随机推荐

  1. [PHP] 浅谈 Laravel Authentication 的 auth:api

    auth:api 在 Laravel 的 Routing , Middleware , API Authentication 主题中都有出现. 一. 在 Routing 部分可以知道 auth:api ...

  2. Gerrit - 初始配置

    1 - 插件管理 1.1 下载并安装插件 以reviewers插件为例. 在GerritForge(https://gerrit-ci.gerritforge.com/),找到对应gerrit 版本的 ...

  3. MySQL Community Server 8.0.16

    1 首先 我们需要先下载一个 Mysql 点击这个网址进入 Mysql 的官网的下载地址: https://dev.mysql.com/downloads/mysql/ 首先 根据你的电脑的操作系统选 ...

  4. snapde的批量文件数据过滤保存功能

    一.snapde基本介绍 Snapde,一个专门为编辑超大型数据量CSV文件而设计的单机版电子表格软件:它运行的速度非常快,反应非常灵敏. 二.snapde批量数据筛选功能 如果想要对很多文件筛选出来 ...

  5. python入门之格式化输出

    目录 扩展: 保留几位小数 一.占位符格式化输出 1.1 %s 1.2 %d 二..format()方式 三.f-string 扩展: 保留几位小数 保留两位小数 a = 12345.2487 pri ...

  6. Python 入门(3):运算符

    Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 Python算术运算符: + 加 两个对象相加 a + ...

  7. android studio下 library打包文件(.aar)和本地引用

    关键点: 利用Gradle发布本地maven库支持android library 打包文件(*.aar) 的本地引用 开发环境: windows7 64位操作系统 android studio0.5. ...

  8. python_并行与并发、多线程

    问题一: 计算机是如何执行程序指令的? 问题二: 计算机如何实现并发的? 轮询调度实现并发执行 程序1-8轮询完成,才再CPU上运行 问题三: 真正的并行需要依赖什么? 并行需要的核心条件 多进程实现 ...

  9. git学习笔记 --分支管理策略

    通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的comm ...

  10. AutoCAD ObjectARX 二次开发(2020版)--3,执行ARX文件--

    上一节中我们在initApp()函数中,将helloWorld()函数注册给了CAD主程序,注册指令的字符串为“Hello”. void initApp() { acedRegCmds->add ...