链接: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的更多相关文章

  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. 访问GitLab的PostgreSQL数据库,查询、修改、替换等操作

    1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...

  2. 源码安装Nginx以及用systemctl管理

    一.源码安装Nginx: 先安装gcc编译器(安装过的可以忽略) [root@localhost ~]# yum -y install gcc gcc-c++ wget 进入src目录 [root@l ...

  3. 【Linux】-NO.87.Assembly.1.滴水逆向.1.001-【介绍】-

    1.0.0 Summary Tittle:[Linux]-NO.87.Assembly.1.滴水逆向.1.001-[基础]- Style:Java Series:Log4j Since:2017-04 ...

  4. Domain Driven Development相关概念

    Entity 与 Value Object1,Entity有唯一的身份标识,是可变的对象.Value Object是immutable,创建了就不能改变.2,Value Object可以在多个领域之间 ...

  5. Bukkit之yaml动态读取

    在使用bukkit框架写插件的时候会经常使用到yml格式的文件来存储配置或者玩家数据,这里来说一下实现yml中数据的动态读写: 先来看一下yml文件中的内容结构 public boolean addB ...

  6. OpenStack-RabbitMQ-获取vm、磁盘、网络设备的状态变化

    需求 及时知道vm,硬盘,镜像,网络设备.负载均衡器状态变化 分析 Dashboard中也是通过定时使用ajax调用API来获取虚拟机的状态信息的定时轮训的方式过于被动 解决方案 共用rabbitmq ...

  7. CSS——对height和line-height的理解

    最近在做CSS界面时经常遇到line-height和height这两个属性,一直没搞很明白,今天静下心来好好网上查阅了一下,算是有所领悟.https://blog.csdn.net/a20131263 ...

  8. 怎样从外网访问内网Rails

    外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...

  9. SolrJ的入门

    什么是SolrJ? solrj是访问solr服务的java客户端,提供索引和搜索的请求方法, SolrJ和图形界面操作的区别就类似于数据库中使用jdbc和mysql客户端的区别一样. 我在测试Solr ...

  10. BinaryTree

    public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode ...