题目描述

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. 20 Interesting WPF Projects on CodePlex

    20 Interesting WPF Projects on CodePlex (Some for Silverlight too) Pete Brown - 22 November 2010   I ...

  2. Django 的 orm 查询

    一.模型关系表 1. 一对一 Author-AuthorDetail 关联字段可以在任意表下,但必须唯一约束.(unique约束) ad_id(unique约束) ad = models.oneToO ...

  3. ubuntu18.04安装jdk1.8.0_11并配置环境变量.md

    参考:https://www.jianshu.com/p/95f075761dc0 由于安装文件免安装程序,故只需要将对应文件复制到相应目录,然后配置环境变量即可: 1.移动文件到指定目录 (1)在/ ...

  4. css 选择器三

    2.4.10 浮动 浮动是css里面布局最多的一个属性,也是很重要的一个属性. float:表示浮动的意思.它有四个值. none: 表示不浮动,默认 left: 表示左浮动 right:表示右浮动 ...

  5. spring boot(十四)shiro登录认证与权限管理

    这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...

  6. spring boot 多数据源配置与使用

    在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties文 ...

  7. Hadoop介绍-1.基本原理

    了解大数据 首先,搞清楚hadoop在处理大数据的定位在哪里 什么是大数据?为什么要处理大数据? 数据量大(Volume) 数据类别复杂(Variety) 数据处理速度快(Velocity) 数据真实 ...

  8. 使用maven构建一个web项目

                          使用maven构建一个web项目     首先建立一个maven项目(关于环境配置上节有详细过程): 1)-->右键new,-->project ...

  9. SQLServer 2008以上误操作数据库恢复方法—日志尾部备份

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/8491327 问题: 经常看到有人误删数据,或者误操作,特别是update和delete的 ...

  10. 【转】[总结]vue开发常见知识点及问题资料整理(持续更新)

    1.(webpack)vue-cli构建的项目如何设置每个页面的title 2.vue项目中使用axios上传图片等文件 3.qs.stringify() 和JSON.stringify()的区别以及 ...