题目链接:http://codeforces.com/contest/805/problem/D

题意:只有一个操作就是将ab变成bba直到不能变为止,问最少边几次。

题解:这题可以多列几组来找规律,事实上是可以递推的,递推可得到一个式子

a[n]=2*a[n-1]+1,然后化成通项公式.

a[n]+1=2*(a[n-1]+1),a[n]+1=2^n,a[n]=2^n-1;

然后就可以解决b前面有几个a的问题了,ab=2^1-1,aab=2^2-1,

#include <iostream>
#include <cstring>
#include <string>
#define mod 1000000007
using namespace std;
typedef long long ll;
const int M = 1e6 + 10;
string s , sl;
ll fpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) {
res = (res * a) % mod;
}
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
cin >> s;
int len = s.size();
int l = 0 , r = len - 1;
for(int i = 0 ; i < len ; i++) {
if(s[i] == 'a') {
l = i;
break;
}
}
for(int i = len - 1 ; i >= 0 ; i--) {
if(s[i] == 'b') {
r = i;
break;
}
}
for(int i = l ; i <= r ; i++) {
sl += s[i];
}
len = sl.size();
ll count = 0 , sum = 0;
for(int i = 0 ; i < len ; i++) {
if(sl[i] == 'a') {
count++;
}
if(sl[i] == 'b') {
sum += fpow(2 , count) - 1;
sum = (sum + mod) % mod;
}
}
cout << (sum + mod) % mod << endl;
return 0;
}

codeforces 805 D. Minimum number of steps(数学)的更多相关文章

  1. Codeforces 805 D Minimum number of steps

    题意: 给定一串字符串,将所有“ab”的子串替换为“bba”,询问多少次操作后没有子串“ab”. 分析: 观察可得,将“ab”替换为“bba”有两种结果. ①a移到了b的后面 ②增加了一个b 而且最终 ...

  2. 【codeforces 805D】Minimum number of steps

    [题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...

  3. Codeforces 805D/804B - Minimum number of steps

    传送门:http://codeforces.com/contest/805/problem/D 对于一个由‘a’.‘b’组成的字符串,有如下操作:将字符串中的一个子串“ab”替换成“bba”.当字符串 ...

  4. Minimum number of steps CodeForces - 805D(签到题)

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  5. Codeforces Round #411 div 2 D. Minimum number of steps

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  6. Codeforces 805D - Minimum number of steps

    805D - Minimum number of steps 思路:简单模拟,a每穿过后面一个b,b的个数+1,当这个a穿到最后,相当于把它后面的b的个数翻倍.每个a到达最后的步数相当于这个a与它后面 ...

  7. Minimum number of steps 805D

    http://codeforces.com/contest/805/problem/D D. Minimum number of steps time limit per test 1 second ...

  8. codeforce 804B Minimum number of steps

    cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each ...

  9. Codeforces805D. Minimum number of steps 2017-05-05 08:46 240人阅读 评论(0) 收藏

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

随机推荐

  1. POJ18060

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16244   Accepted: 565 ...

  2. 经典SQL(sqlServer)

    一.基础 .说明:创建新表create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) .分组: ...

  3. JavaSE(二)标识符,关键字,数据类型

    一.标识符和关键字         1.具有特殊作用的分隔符:分号;.花括号{}.圆括号().空格.圆点 .          2.标识符规则:用于给程序中变量.类.方法命名的符号.       Ja ...

  4. 以太坊RLPx传输协议

    本文主要内容翻译自:The RLPx Transport Protocol,其中添加了一些个人的理解,由于密码学水平有限,不正确之处望指正.另外原文可能已经更新,最新内容请直接阅读原文. 本文档定义了 ...

  5. Android:JNI与NDK(三)NDK构建的脚本文件配置

    友情提示:欢迎关注本人公众号,那里有更好的阅读体验以及第一时间获取最新文章 本文目录 一.前言 本篇我们介绍Android.mk与CMakeLists.txt构建NDK的配置文件,我们知道目前NDK的 ...

  6. Apache ActiveMQ任意文件写入漏洞(CVE-2016-3088)复现

    Apache ActiveMQ任意文件写入漏洞(CVE-2016-3088)复现 一.漏洞描述 该漏洞出现在fileserver应用中,漏洞原理:ActiveMQ中的fileserver服务允许用户通 ...

  7. 教老婆学Linux运维(一)初识Linux

    零.前言 之一 为什么写这个系列?为什么是Linux? 老婆自从怀孕以后,辞职在家待了好几年了,现在时常感觉与社会脱节.所以想找个工作. 做了多年程序员,有点人脉也都基本是在IT圈子里,只能帮忙找找I ...

  8. loadrunner中的ie浏览器无法使用

    我的loadrunner是12.55版本的,windows10系统 在我们学习loadrunner的过程中,会出现下面一个问题: 在录制脚本时,loadrunner中的ie浏览器无法使用处于飘红状态. ...

  9. React Native-安卓环境的搭建

    最近公司做一个项目,项目中使用React Native(简称RN)来写安卓APP,现在我分享下安卓环境搭建的过程. 我参照https://reactnative.cn/docs/getting-sta ...

  10. 源码解读 Spring Boot Profiles

    前言 上文<一文掌握 Spring Boot Profiles> 是对 Spring Boot Profiles 的介绍和使用,因此本文将从源码角度探究 Spring Boot Profi ...