牛客网第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 ,问你 ...
随机推荐
- mysql报错Ignoring the redo log due to missing MLOG_CHECKPOINT between
mysql报错Ignoring the redo log due to missing MLOG_CHECKPOINT between mysql版本:5.7.19 系统版本:centos7.3 由于 ...
- 你不知道的JavaScript-2.词法作用域
考虑以下代码: function foo(a) { var b = a * 2; function bar(c) { console.log( a, b, c ); } bar( b * 3 ); } ...
- Ch01 基础 - 练习
1. 在Scala REPL 中键入3.,然后按Tab键.有哪些方法可以被应用? scala> 3. % * - > >> ^ ...
- Python类中的__init__() 和 self 的解析
原文地址https://www.cnblogs.com/ant-colonies/p/6718388.html 1.Python中self的含义 self,英文单词意思很明显,表示自己,本身. 此处有 ...
- 第一章 JQuery概述
1.JQuery的作用:访问和操作DOM元素控制页面样式对页面事件进行处理扩展新的JQuery插件与Ajax技术完美结合注:JQuery能完成的效果js都能完成,但是JQuery的开发效率更高,代码更 ...
- [Android] websocket客户端开发
为了能够在H5和APP都保持同一套长连接接口,因为采用websocket协议作为开发 使用的第三方库是:https://github.com/TakahikoKawasaki/nv-websocket ...
- [完美]原生JS获取浏览器版本判断--支持Edge,IE,Chrome,Firefox,Opera,Safari,以及各种使用Chrome和IE混合内核的浏览器
截至自2017-08-11,支持现世已出的几乎所有PC端浏览器版本判断. 受支持的PC端浏览器列表: Edge IE Chrome Firefox Opera Safari QQ浏览器 360系列浏览 ...
- inux下输入ifconfig命令,没有eth0,怎么解决
用ifconfig命令,只有lo,没有eth0的解决方案 问题描述:视频中输入ifconfig命令,显示eth0和lo,但是自己在虚拟机中并非得到这样的结果,而是只有lo,即网卡未启动,也没有ip,无 ...
- python的优点
在当下编程语言繁多,选择python是因为他存在这许多的优点: Python是一种效率极高的语言:相比于众多其他的语言,使用Python编写时,程序包含的代码行更少.Python的语法也有助于创建整洁 ...
- java的四大特性
java的四大特性是:封装.继承.多态,抽象.