题目链接: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. Maven项目的打包发布到Nexus私服和服务器

    1.编写pom文件如下: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...

  2. 中间件增强框架之-CaptureFramework框架

    一.背景 应用服务监控是智能运维系统的重要组成部分.在UAV系统中,中间件增强框架(MOF)探针提供了应用画像及性能数据收集等功能,其中数据收集功能主要采集四类数据:实时数据.画像数据.调用链接数据生 ...

  3. java封装 redis 操作 对象,list集合 ,json串

    /** * 功能说明: * 功能作者: * 创建日期: * 版权归属:每特教育|蚂蚁课堂所有 www.itmayiedu.com */package com.redis.service; import ...

  4. pythonday03数据类型(一)

    今日内容 1.整型 2.布尔型 3.字符串 4.补充 5.作业讲解 6,pycharm自动生成头文件 1.整型(int) py2 int/long 32位电脑:-2147483648-21474836 ...

  5. 【Aizu - 2249】Road Construction(最短路 Dijkstra算法)

    Road Construction Descriptions Mercer国王是ACM王国的王者.他的王国里有一个首都和一些城市.令人惊讶的是,现在王国没有道路.最近,他计划在首都和城市之间修建道路, ...

  6. CodeForces 427D Match & Catch

    洛谷题目页面传送门 & CodeForces题目页面传送门 给定\(2\)个字符串\(a,b,|a|=n,|b|=m\),求最长的既在\(a\)中出现恰好\(1\)次又在\(b\)中出现恰好\ ...

  7. 【数学+思维】ZZULIOJ 1531: 小L的区间求和

    题目链接 题目描述 在给定的一个整数序列中,小L希望找到一个连续的区间,这个区间的和能够被k整除,请你帮小L算一下满足条件的最长的区间长度是多少. 输入 第一行输入两个整数n.k.(1 <= n ...

  8. LCA最近公共祖先---倍增法笔记

    先暂时把模板写出来,A几道题再来补充 此模板也是洛谷上的一道模板题 P3379 [模板]最近公共祖先(LCA) #pragma GCC optimize(2) //o2优化 #include < ...

  9. vue 异步加载远程组件(支持编译less语法)

    本代码已组件化,可以直接使用. 说明:本组件可以直接解析.vue文件,为了支持less语法解析,在组件中引入less.js,可在less官网下载. 组件代码 <template> < ...

  10. Tomcat源码分析 (十)----- 彻底理解 Session机制

    Tomcat Session 概述 首先 HTTP 是一个无状态的协议, 这意味着每次发起的HTTP请求, 都是一个全新的请求(与上个请求没有任何联系, 服务端不会保留上个请求的任何信息), 而 Se ...