牛客网第4场A
链接:https://www.nowcoder.com/acm/contest/142/A
来源:牛客网 题目描述
A ternary string is a sequence of digits, where each digit is either , , or .
Chiaki has a ternary string s which can self-reproduce. Every second, a digit is inserted after every in the string, and then a digit is inserted after every in the string, and finally the first character will disappear.
For example, ``'' will become ``'' after one second, and become ``'' 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 ( + ).
输入描述:
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 ( ≤ |s| ≤ ).
It is guaranteed that the sum of all |s| does not exceed x .
输出描述:
For each test case, output an integer denoting the answer. If the string never becomes empty, output - instead.
示例1
输入 复制 输出 复制
欧拉降幂且记录;
(1)如果在消除一个 0 前经过了 n 秒,那么消掉这个 0 需要 n + 1 秒。
(2)如果在消除一个 1 前经过了 n 秒,那么消掉这个 1 与其产生的所有数需要 (n + 1) * 2 秒。
(3)如果在消除一个 2 前经过了 n 秒,那么消掉这个 2 与其产生的所有数需要 (2 ^ (n + 1) - 1) * 3 秒。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<map>
#include<string.h>
using namespace std;
#define ll long long
map<ll,ll>mp;
char s[];
ll phi(ll x)
{
if(mp[x]) return mp[x];
ll temp=x;
ll ans=x;
if(x==) return mp[]=;
for(ll i=;i*i<=x;i++){
if(x%i==){
ans=ans/i*(i-);
while(x%i==) x/=i;
}
}
if(x>) ans=ans/x*(x-);
return mp[temp]=ans;
} ll qsm(ll a,ll b,ll c)
{
ll ret = ;
for (;b;b >>= ,(a *= a)%=c)
if (b & ) (ret *= a)%=c;
return ret;
}
ll solve(ll x,ll mod)
{
if(x==||mod==)
return ;
if(s[x]==''){
return (1LL+solve(x-,mod)+mod)%mod;
}else if(s[x]==''){
return (*(solve(x-,mod)+)+mod)%mod;
}else{
ll ph=phi(mod);
ll t=(solve(x-,ph))%ph;
return (qsm(,t,mod)*%mod-+mod*)%mod;
} }
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",s+);
printf("%lld\n",solve(strlen(s+),1e9+));
}
return ;
}

牛客网第4场A的更多相关文章
- 牛客网第9场多校E(思维求期望)
链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...
- 2019 牛客网 第七场 H pair
题目链接:https://ac.nowcoder.com/acm/contest/887/H 题意: 给定A,B,C问在[1,A]和[1,B]中有多少对x,y满足x&y>C或者x^y ...
- 牛客网PAT-练兵场-挖掘机技术哪家强
题目地址:https://www.nowcoder.com/pat/6/problem/4058 题解:用数组下标当学校编号.输入一次数据的时候,直接在相应数组下标位置累加内容,同时更新最大的总分的学 ...
- 牛客网NOIP赛前集训营-提高组(第四场)游记
牛客网NOIP赛前集训营-提高组(第四场)游记 动态点分治 题目大意: \(T(t\le10000)\)组询问,求\([l,r]\)中\(k(l,r,k<2^{63})\)的非负整数次幂的数的个 ...
- 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...
- 牛客网NOIP赛前集训营-提高组(第四场)B区间
牛客网NOIP赛前集训营-提高组(第四场)B区间 题目描述 给出一个序列$ a_1 \dots a_n$. 定义一个区间 \([l,r]\) 是好的,当且仅当这个区间中存在一个 \(i\),使得 ...
- 牛客网暑期ACM多校训练营(第五场):F - take
链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学
牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...
随机推荐
- PyQt5的安装及基本配置
安装PyQt5 注:Pyqt5只支持python3.5以上版本 Mac安装 Mac上使用编译安装的话,后面使用pyinstaller打包可能会出问题,推荐使用命令行一键安装,Linux如果包管理没有的 ...
- python调用RPC接口
要调用RPC接口,python提供了一个框架grpc,这是google开源的 rpc相关文档: https://grpc.io/docs/tutorials/basic/python.html 需要安 ...
- JavaScript知识精简
JS单线程,同步,一次执行某一段代码,等到前一个程序执行完毕再执行.,阻塞,安全. 多线程,异步,不用等到前一个程序执行完毕就执行. 数据类型 JavaScript 是 弱类型 语言,但并不是没有 ...
- 为什么入门首选C语言
对于大部分程序员,C语言是学习编程的第一门语言,很少有不了解C的程序员. C语言除了能让你了解编程的相关概念,带你走进编程的大门,还能让你明白程序的运行原理,比如,计算机的各个部件是如何交互的,程序在 ...
- 以选项卡的故事扯扯js面向对象
在现在的网页中,选项卡(我自己这样子叫)是非常普遍的,也是比较基础,学了原型实现选项卡也挺久了,最近在学ES6,学了用类实现选项卡,今天就在此做个总结,别的废话也不多说. 以"貌" ...
- excel 八位二进制转换为十六进制公式
=BIN2HEX(C16&D16&E16&F16&G16&H16&I16&J16,2)
- Docker Kubernetes hostPort 代理转发
Docker Kubernetes hostPort 代理转发 hostPort: 1. 类似docker -p 映射宿主级端口到容器. 2. 容器所在的主机暴露端口转发到指定容器中. 3. hos ...
- oracle 11.2 asynch descriptor resize等待事件
asynch descriptor resize描述最近部分insert /*+ append */语句出现该等待时间,经查This event is set when the number of a ...
- 1. Dubbo原理解析-Dubbo内核实现之SPI简单介绍 (转)
转载自 斩秋的专栏 http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577159 Dubbo采用 微内核 + 插件体系,使得设计优 ...
- poj 3294 Life Forms - 后缀数组 - 二分答案
题目传送门 传送门I 传送门II 题目大意 给定$n$个串,询问所有出现在严格大于$\frac{n}{2}$个串的最长串.不存在输出'?' 用奇怪的字符把它们连接起来.然后求sa,hei,二分答案,按 ...