题目描述

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. Confluence 6 设置你的个人空间主页

    不论你是否正在使用个人空间为沙盒来测试一些内容,组合灯显示是如何工作的,一个能够导航到其他空间和内容的页面,或者一些完全不同的东西.下面一些红能够帮助你在你的个人空间中更加有效的使用和发布信息. 使用 ...

  2. Oracl 12c安装

    Oracl安装部署 一.前置条件准备 修改hostname: hostname oracle 修改/etc/hosts:添加192.168.10.106 oracle 添加软件开发工具 搭建yum源 ...

  3. CentOS7 下源代码安装apache2.4

    Apache httpd 2.4 源代码安装   https://httpd.apache.org/docs/2.4/install.html   这里选用Apache2.4版本. wget http ...

  4. selenum threding多线程运行 实例

    1.配置相关驱动 chrome驱动 下载驱动,配置到环境变量中 如 xxxxchrome.jar 将其新建目录c:/driver---将目录 配置到环境变量 firefox驱动 下载驱动,将gecko ...

  5. ACM-ICPC World Finals 2019 G.First of Her Name

    题意:给一颗字典树,m次查询,每次给出一个字符串,问你该字符串是字典树上多少串的后缀 题解:字典树求广义sam,每次把查询串在sam上跑一遍,最后到达的点的sz就是答案,中途没法走了,就是没有出现过 ...

  6. Thymeleaf使用bootstrap及其bootstrap相关插件(一)

    Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷. 在完成信息录入界面 ...

  7. python_数据类型

    数据类型 1.数字类型 整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点.Python3 整型是没有限制大小的,可以当作 Long 类型使用,所以 Python3 没有 Pytho ...

  8. 第一讲(3)osgearth编译

    前题条件完成osg 3.0的编译Step 1 下载osgEarth 2.1.1https://github.com/gwaldron/osgearth/downloads------------> ...

  9. InnoDB行记录格式(compact)、InnoDB数据页结构

    1. compact 行记录格式: 变长字段长度列表,null标志位,记录头信息,列1数据,列2数据 …… 记录头信息中包含许多信息,只列举一部分: 名称 大小 描述 deleted_flag 1bi ...

  10. CNN autoencoder 先降维再使用kmeans进行图像聚类 是不是也可以降维以后进行iforest处理?

    import keras from keras.datasets import mnist from keras.models import Sequential from keras.layers ...