题目描述

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. p1460 Healthy Holsteins

    列举所有的子集找最优就行. #include <iostream> #include <cstdio> #include <cmath> #include < ...

  2. int __get_order(unsigned long size)

    2017-12-6 10:26:504.9.51int __get_order(unsigned long size){    int order; size--;    size >>= ...

  3. es-aggregations聚合分析

    聚合分析的格式: "aggregations" : { "<aggregation_name>" : { "<aggregation ...

  4. Beta阶段——第4篇 Scrum 冲刺博客

    Beta阶段--第4篇 Scrum 冲刺博客 标签:软件工程 一.站立式会议照片 二.每个人的工作 (有work item 的ID) 昨日已完成的工作 人员 工作 林羽晴 昨日完成获取提醒语句的接口函 ...

  5. Beta阶段——第6篇 Scrum 冲刺博客

    Beta阶段--第6篇 Scrum 冲刺博客 标签:软件工程 一.站立式会议照片 二.每个人的工作 (有work item 的ID) 昨日已完成的工作 人员 工作 林羽晴 完成了函数的编写,提供报表数 ...

  6. mysql 数据库的CUDR

    mysql删表和建表语句: DROP TABLE IF EXISTS `t_blog_user`;CREATE TABLE `t_blog_user` ( `id` int(11) NOT NULL ...

  7. WebSphere ssl证书公钥少于2048问题处理

    WebSphere https默认使用的是安装时生成的IBM签名的证书,该证书密钥长度1024位在某些检查中会认为这不够安全.处理这个问题我们可以创建一个自签名的证书作为默认证书. 登录控制台,安全性 ...

  8. Find a way out of the ClassLoader maze

    June 6, 2003 Q: When should I use Thread.getContextClassLoader() ? A: Although not frequently asked, ...

  9. 微信UnionId 部分开放

    以前要获得UnionID, 需要把公众号绑定到微信开放平台, 这个微信开放平台垃圾,还要300认证费. 今天突然发现在这个接口 https://api.weixin.qq.com/cgi-bin/us ...

  10. Struts 2 初步入门(二)

    Struts 2 动态方法调用 1.在HelloWorldAction中添加两个新的方法如下: import com.opensymphony.xwork2.ActionSupport; public ...