1. 链接:https://www.nowcoder.com/acm/contest/142/A
  2. 来源:牛客网
  3.  
  4. 题目描述
  5. A ternary string is a sequence of digits, where each digit is either , , or .
  6. 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.
  7. For example, ``'' will become ``'' after one second, and become ``'' after another second.
  8. 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 ( + ).
  9. 输入描述:
  10. There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
  11. The first line contains a ternary string s ( |s| ).
  12. It is guaranteed that the sum of all |s| does not exceed x .
  13. 输出描述:
  14. For each test case, output an integer denoting the answer. If the string never becomes empty, output - instead.
  15. 示例1
  16. 输入
  17.  
  18. 复制
  19.  
  20. 输出
  21.  
  22. 复制

欧拉降幂且记录;

(1)如果在消除一个 0 前经过了 n 秒,那么消掉这个 0 需要 n + 1 秒。

(2)如果在消除一个 1 前经过了 n 秒,那么消掉这个 1 与其产生的所有数需要 (n + 1) * 2 秒。

(3)如果在消除一个 2 前经过了 n 秒,那么消掉这个 2 与其产生的所有数需要 (2 ^ (n + 1) - 1) * 3 秒。

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cmath>
  4. #include<cstdlib>
  5. #include<ctime>
  6. #include<vector>
  7. #include<map>
  8. #include<string.h>
  9. using namespace std;
  10. #define ll long long
  11. map<ll,ll>mp;
  12. char s[];
  13. ll phi(ll x)
  14. {
  15. if(mp[x]) return mp[x];
  16. ll temp=x;
  17. ll ans=x;
  18. if(x==) return mp[]=;
  19. for(ll i=;i*i<=x;i++){
  20. if(x%i==){
  21. ans=ans/i*(i-);
  22. while(x%i==) x/=i;
  23. }
  24. }
  25. if(x>) ans=ans/x*(x-);
  26. return mp[temp]=ans;
  27. }
  28.  
  29. ll qsm(ll a,ll b,ll c)
  30. {
  31. ll ret = ;
  32. for (;b;b >>= ,(a *= a)%=c)
  33. if (b & ) (ret *= a)%=c;
  34. return ret;
  35. }
  36. ll solve(ll x,ll mod)
  37. {
  38. if(x==||mod==)
  39. return ;
  40. if(s[x]==''){
  41. return (1LL+solve(x-,mod)+mod)%mod;
  42. }else if(s[x]==''){
  43. return (*(solve(x-,mod)+)+mod)%mod;
  44. }else{
  45. ll ph=phi(mod);
  46. ll t=(solve(x-,ph))%ph;
  47. return (qsm(,t,mod)*%mod-+mod*)%mod;
  48. }
  49.  
  50. }
  51. int main()
  52. {
  53. int n;
  54. scanf("%d",&n);
  55. while(n--)
  56. {
  57. scanf("%s",s+);
  58. printf("%lld\n",solve(strlen(s+),1e9+));
  59. }
  60. return ;
  61. }

牛客网第4场A的更多相关文章

  1. 牛客网第9场多校E(思维求期望)

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...

  2. 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 ...

  3. 牛客网PAT-练兵场-挖掘机技术哪家强

    题目地址:https://www.nowcoder.com/pat/6/problem/4058 题解:用数组下标当学校编号.输入一次数据的时候,直接在相应数组下标位置累加内容,同时更新最大的总分的学 ...

  4. 牛客网NOIP赛前集训营-提高组(第四场)游记

    牛客网NOIP赛前集训营-提高组(第四场)游记 动态点分治 题目大意: \(T(t\le10000)\)组询问,求\([l,r]\)中\(k(l,r,k<2^{63})\)的非负整数次幂的数的个 ...

  5. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  6. 牛客网NOIP赛前集训营-提高组(第四场)B区间

    牛客网NOIP赛前集训营-提高组(第四场)B区间 题目描述 给出一个序列$ a_1  \dots   a_n$. 定义一个区间 \([l,r]\) 是好的,当且仅当这个区间中存在一个 \(i\),使得 ...

  7. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  8. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  9. 牛客网 暑期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 ,问你 ...

随机推荐

  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 由于 ...

  2. 你不知道的JavaScript-2.词法作用域

    考虑以下代码: function foo(a) { var b = a * 2; function bar(c) { console.log( a, b, c ); } bar( b * 3 ); } ...

  3. Ch01 基础 - 练习

    1. 在Scala REPL 中键入3.,然后按Tab键.有哪些方法可以被应用? scala> 3. %   *   -   >    >>    ^              ...

  4. Python类中的__init__() 和 self 的解析

    原文地址https://www.cnblogs.com/ant-colonies/p/6718388.html 1.Python中self的含义 self,英文单词意思很明显,表示自己,本身. 此处有 ...

  5. 第一章 JQuery概述

    1.JQuery的作用:访问和操作DOM元素控制页面样式对页面事件进行处理扩展新的JQuery插件与Ajax技术完美结合注:JQuery能完成的效果js都能完成,但是JQuery的开发效率更高,代码更 ...

  6. [Android] websocket客户端开发

    为了能够在H5和APP都保持同一套长连接接口,因为采用websocket协议作为开发 使用的第三方库是:https://github.com/TakahikoKawasaki/nv-websocket ...

  7. [完美]原生JS获取浏览器版本判断--支持Edge,IE,Chrome,Firefox,Opera,Safari,以及各种使用Chrome和IE混合内核的浏览器

    截至自2017-08-11,支持现世已出的几乎所有PC端浏览器版本判断. 受支持的PC端浏览器列表: Edge IE Chrome Firefox Opera Safari QQ浏览器 360系列浏览 ...

  8. inux下输入ifconfig命令,没有eth0,怎么解决

    用ifconfig命令,只有lo,没有eth0的解决方案 问题描述:视频中输入ifconfig命令,显示eth0和lo,但是自己在虚拟机中并非得到这样的结果,而是只有lo,即网卡未启动,也没有ip,无 ...

  9. python的优点

    在当下编程语言繁多,选择python是因为他存在这许多的优点: Python是一种效率极高的语言:相比于众多其他的语言,使用Python编写时,程序包含的代码行更少.Python的语法也有助于创建整洁 ...

  10. java的四大特性

    java的四大特性是:封装.继承.多态,抽象.