牛客网第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]面向对象1
数据赋值拷贝 1,值得赋值是独立的 num1=12; num1=num2 将存储值赋值一份存储. 2, var age=22; age2=age; age=24 console.log(age); a ...
- fiddler学习总结--autoresponder替换资源
意义:替换服务器返回的内容 1.找到需要替换的目标 2.选择目标后,点击“autoresponder”-->”add rules” 3.在下图中,选择“find a file”,再选择需要替换 ...
- 日志采集器windows客户端的配置释义
<Extension json> Module xm_json </Extension> <Extension charconv> Module xm_charco ...
- eclipse添加mybatis插件
1,在 eclipse中选择 Help->eclipse marketplace 2, 搜索安装
- nginx--service配置
nginx从今天开始进行相关学习了,包括:1.注册centos的service服务:2.相关的tomcat负载:3.https配置:4.session共享配置 1.注册centos的service服务 ...
- 剑指offer(43)左旋转字符串
题目描述 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”abc ...
- Python新手入门英文词汇(1-1)
英文词汇总结一.交互式环境与print输出 1.print:打印/输出2.coding:编码3.syntax:语法4.error:错误5.invalid:无效6.identifier:名称/标识符7. ...
- iOS开发 -------- storyBoard实现控制器添加childViewController
1 拖进去scrollView 添加约束(0,0,0,0); 2 更新scrollView约束,然后在scrollView上面加个view,设置其约束为(0,0,0,0) 和 水平滑动约束; ...
- 【NOI 2015】品酒大会
Problem Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和" ...
- Linux 查看进程之PS命令
要对进程进行检测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程运行状态.Linux 系统中 我们可以使用 ps 命令查看进程. ps 命令介绍 ps(process stat ...