题目描述

A ternary string is a sequence of digits, where each digit is either 0, 1, or 2.
Chiaki has a ternary string s which can self-reproduce. Every second, a digit 0 is inserted after every 1 in the string, and then a digit 1 is inserted after every 2 in the string, and finally the first character will disappear.
For example, ``212'' will become ``11021'' after one second, and become ``01002110'' after another second.
Chiaki would like to know the number of seconds needed until the string become an empty string. As the answer could be very large, she only needs the answer modulo (109 + 7).

输入描述:

There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
The first line contains a ternary string s (1 ≤ |s| ≤ 10^5)
It is guaranteed that the sum of all |s| does not exceed 2 x 10^6

输出描述:

For each test case, output an integer denoting the answer. If the string never becomes empty, output -1 instead.

题意:给出一串字符,只包含012,每过一秒有以下的操作(在所有的2后面插一个1,在所有的1后面插一个0,删掉第一个字符),
问经过多少秒之后才会消完整个字符串 思路:我们想前面每经过一秒,后面又会多加了一堆数,所以前面插入数的次数会影响到后面,所以我们可以打表去找规律
我们会发现如下规律
如果前面经过x次 那当前是0的话,到这就是x+1次
如果前面经过x次 那当前是1的话,到这就是2*(x+1)次
如果前面经过x次 那当前是2的话,到这就是3*(2^(x+1)-1)次 

所以我们就需要知道每个字符前到底发生多少次,我们又想算出前面的值之后再递推回来一个一个的算后面的值
这明显就是一个递归的过程,所以我们从最后一个字符开始,递归算出前面的值之后再来算当前值,但是这个字符串长度这么长
我们这个次方明显是成几何倍数递增的,所以快速幂已经不能完全解决问题,我们就可以考虑到数论里面的一个欧拉降幂
然后套一个欧拉降幂的模板即可,因为广义欧拉的推导我也不会>_<
公式 : a^x=a^(phi(p)+x mod phi(p))mod p
#include<bits/stdc++.h>
#define lson l,m
#define rson m+1,r
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = int(1e5) + ;
const int maxm=;
const int BN = ;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = (int)1e9 + ;
const double EPS = 1e-;
char str[maxn];
map<ll,ll>mo;
ll phi(ll x) { //欧拉降幂 把原有的求欧拉值进行了一点修改
ll res=x,a=x;
for(ll i=; i*i<=x; i++) {
if(a%i==) {
res=res/i*(i-);
while(a%i==) a/=i;
}
}
if(a>) res=res/a*(a-);
return res;
}
ll quick_pow(ll base,ll n,ll modd) { //快速幂
ll ans=;
while(n) {
if(n&) ans=ans*base%modd;
base=base*base%modd;
n>>=;
}
return ans;
}
void init(int mod){ //预处理出所有的欧拉降幂
while(mod!=){
mo[mod]=phi(mod);
mod=mo[mod];
}
mo[]=;
}
ll dfs(int pos,int mod) {//递归分别对应三个数字不同的情况 我们会递归到最底的时候把前面有多少数递归回来
if(pos==) return ;
else if(str[pos]=='') return (dfs(pos-,mod)+)%mod;
else if(str[pos]=='') return (*dfs(pos-,mod)+)%mod;
else return (*quick_pow(,dfs(pos-,mo[mod])+,mod)-+mod)%mod;
}
int main() {
init(mod);
int T;
scanf("%d",&T);
while(T--) {
memset(str,,sizeof(str));
scanf("%s",str+);
int len=strlen(str+);
ll ans=dfs(len,mod);
printf("%lld\n",ans);
}
return ;
}


 
 

牛客多校第四场 A Ternary String的更多相关文章

  1. 牛客多校第四场sequence C (线段树+单调栈)

    牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...

  2. 牛客多校第四场 F Beautiful Garden

    链接:https://www.nowcoder.com/acm/contest/142/F来源:牛客网 题目描述 There's a beautiful garden whose size is n ...

  3. 牛客多校第四场 G Maximum Mode

    链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...

  4. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  5. 2019牛客多校第四场 A meeting

    链接:https://ac.nowcoder.com/acm/contest/884/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10485 ...

  6. 2019年牛客多校第四场 B题xor(线段树+线性基交)

    题目链接 传送门 题意 给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\). 思路 线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~ 线性基交学习博客:传 ...

  7. 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)

    题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...

  8. 2018牛客多校第四场 J.Hash Function

    题意: 给出一个已知的哈希表.求字典序最小的插入序列,哈希表不合法则输出-1. 题解: 对于哈希表的每一个不为-1的数,假如他的位置是t,令s = a[t]%n.则这个数可以被插入当且仅当第s ~ t ...

  9. 2019牛客多校第四场B xor——线段树&&线性基的交

    题意 给你 $n$ 个集合,每个集合中包含一些整数.我们说一个集合表示一个整数当且仅当存在一个子集其异或和等于这个整数.现在你需要回答 $m$ 次询问 ($l, r, x$),是否 $l$ 到 $r$ ...

随机推荐

  1. uva10564

    路径条数很好找.记录最小路径,就记录到各点的最小字符串,存储起来. #include <iostream> #include <cstdio> #include <cma ...

  2. 说说secondarynamenode作用和配置

    说说secondarynamenode作用 http://my.oschina.net/u/1464779/blog/289895 说说secondarynamenode的配置 hadoop2.X如何 ...

  3. android -------- 安装APK报错:Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE解决方法

    记录一个 DELETE_FAILED_INTERNAL_ERROR Error while Installing APK问题 之前遇到这个问题 方案1 将data/data/目录下该应用的包名的目录删 ...

  4. Android--------WebView+H5开发仿美团 预加载,加载失败和重新加载

    Android嵌入式开发已经占大多数了,很多界面都是以网页的形式展示,WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. 本博客主要是模仿美团的旅游出行模块的预加载,网页加载失 ...

  5. 整合多个网络的拓扑结构并降维(Mashup)

    整合多个网络的拓扑结构并降维(Mashup) 介绍一个整合多个网络拓扑结构的方法,方法来源:Compact Integration of Multi-Network Topology for Func ...

  6. 2017-6-5/MySQL分库分表

    分库分表,顾名思义,就是把原本存储于一个库一张表的数据分块存储到多个库多张表上.对于大型互联网应用来说,当一张表的数据量达到百万.千万时,数据库每执行一次查询所花的时间会变多,并且数据库面临着极高的并 ...

  7. ECharts 报表事件联动系列二:柱状图,饼状图添加事件

    代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

  8. Mybatis中tinyint(1)数据自动转化为boolean处理

    问题描述:使用Mybatis查询tinyint(1)字段数据,返回值为Map类型,那么tinyint(1)的数据默认会转化为boolean类型数据.解决方案:  1.使用ifnull(column, ...

  9. [LightOJ 1287] Where to Run

    Where to Run Last night you robbed a bank but couldn't escape and when you just got outside today, t ...

  10. Charles破解网站收藏(持续更新)

    1. 在这个网站(http://charles.iiilab.com/)下载破解文件 charles.jar 2. 替换掉原文件夹里的charles.jar Mac: /Applications/Ch ...