牛客网第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 ,问你 ...
随机推荐
- [js]js设计模式-工厂模式
// 定义一个人 var p1 = { name: 'wxb', age: 22, writejs: function () { console.log(this.name + ' can sing. ...
- [js] 渲染树构建、布局及绘制
渲染树构建.布局及绘制
- CentOS 7静默安装Oracle 11g R2数据库软件
之前安装Oracle 11g R2数据库软件都是建立在图形界面上的,不过现在大部分服务器上都没有安装图形界面.图形界面安装较为方便,安装选项清晰,步骤明确,但Oracle还支持另一种安装方式,就是通过 ...
- safari无法调试iphone提示“无可检查的应用程序”的解决方法
iphone上打开safari,随便访问一个网站,mac上通过Safari的开发,我的iphone是可以看到的.如果打开APP,就提示“无可检查的应用程序”. 解决方法 使用XCode运行我们的App ...
- 新装Windows Server 2008 r2无法连接有线网络
新装的Windows Server 2008 r2没有网卡驱动,所以没有网络适配器. 首先,我在相同的型号电脑上查到这个主板的网卡驱动安装的是Intel(R) Ethernet Coinnection ...
- linux tail命令及其它日志查看命令的用法
当日志文件存储日志很大时,我们就不能用vi直接进去查看日志,需要Linux的命令去完成我们的查看任务. Log位置: /var/log/message 系统启动后的信息和错误日志,是Red Hat L ...
- mongodb认识
MongoDB安装 一.软件的获取地址 1.使用本安装文档提供的安装软件 本安装文档提供的软件为window版本的64位MongoDB的安装包 2.在官网上下载所需的安装软件 下载地址:https:/ ...
- Linux下启动Oracle服务和监听程序
$ su – oracle $ sqlplus / nolog sql> conn / as sysdba sql> startup #启动Oracle,需 ...
- spool例子
set head offset echo offset feed offset heads offset pages 50000SET NEWPAGE NONEcolumn yesterday new ...
- Bugku-CTF之管理员系统+程序员本地网站
Day12 管理员系统 http://123.206.31.85:1003/ flag格式flag{}