题目链接:https://www.nowcoder.com/acm/contest/142/A

题目描述

A ternary string is a sequence of digits, where each digit is either 0, 1, or 2.
Chiaki has a ternary string s which can self-reproduce. Every second, a digit 0 is inserted after every 1 in the string, and then a digit 1 is inserted after every 2 in the string, and finally the first character will disappear.
For example, "212'' will become "11021'' after one second, and become "01002110'' 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 (109 + 7).

输入描述:

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 (1 ≤ |s| ≤ 10^5).
It is guaranteed that the sum of all |s| does not exceed 2 x 10^6.

输出描述:

For each test case, output an integer denoting the answer. If the string never becomes empty, output -1 instead.

输入

3
000
012
22

输出

3
93
45

题意:

有一串数字串s,只包含三个数字0,1,2,

每过一分钟,先是每个2后面会产生一个1,每个1后面会产生一个0,然后串头第一个数字会消失,

问经过多少秒,整个串全部消失。

题解:

显然,1和2最后都会被消失掉,而0产生不了新的数字,整个串必然在若干秒后会消失,所以不可能有答案为 -1 的可能性;

那么,我们对于串上的每个数字 str[i] 考虑两个时间点 $t$ 和 $t'$,

分别代表:以最开始为 $0$ 秒记,第 $t$ 秒结束时,s[i]成为串头;s[i]成为串头后,在第 $t'$ 秒结时,它以及由它所产生(直接或间接)的所有数字全部消失。

那么,对于三个数字就有三种对应情况:

  1. 数字0:$t' = t + 1$,不管经过多少秒,0都不会再产生任何数字,所以只需要1秒钟就能消除掉;
  2. 数字1:$t' = 2t + 2$,经过 $t$ 秒后,总共产生 $t$ 个0,在 $t+1$ 秒时,又产生一个0,同时1消失,则还剩下 $t+1$ 个0,所以总共花费 $1+t+1$ 秒消除掉全部;
  3. 数字2:$t' = 6 \times 2^t - 3$,不难知道在第 $t+1$ 秒结束时,2产生了这样的数字串:$1101001000 \cdots 1\overbrace {00 \cdots 0}^t$,我们尝试 $t = 0,1,2,3$,就能得到 $t' = 3,9,21,45 \cdots = 1 \times 3,3 \times 3,7 \times 3,15 \times 3 \cdots = \left( {2^{t + 1} - 1} \right) \times 3 = 6 \times 2^t - 3$。

这样一来,假设就可以在 $O\left( {\left| s \right|} \right)$ 时间内计算出消除整个串的时间,

但是这里遇到一个问题,由于模运算的运算规则只有(参见模运算_百度百科):

  1、( a + b ) % n = ( a%n + b%n ) % n

  2、( a - b ) % n = ( a%n - b%n ) % n

  3、( a * b ) % n = ( a%n * b%n ) % n

  4、( a ^ b ) % n = ( (a%n) ^ b ) % n

也就是说,模1e9+7只能在计算 $t' = t + 1$ 和 $t' = 2t + 2$ 的过程直接取模,但是 $t' = 6 \times 2^t - 3$ 里 $t$ 太大了,需要进行降幂,

使用欧拉降幂公式:当且仅当 $B > \phi \left( C \right)$ 时,有 $A^B \bmod C = A^{B\bmod \phi \left( C \right) + \phi \left( C \right)} \bmod C$。(扩展欧拉定理,具体见传送门:https://blog.csdn.net/wu_tongtong/article/details/79631285

其中的 ${\phi \left( n \right)}$ 代表欧拉函数,指不超过 $n$ 且和 $n$ 互质的正整数个数,其中 $n$ 为正整数。

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=1e9+;
const int maxn=1e5+; char s[maxn];
map<ll,ll> mp; ll phi(ll n) //欧拉函数
{
ll res=n;
for(ll i=;i*i<=n;i++)
{
if(n%i==)
{
res=res-res/i;
while(n%i==) n/=i;
}
}
if(n>) res=res-res/n;
return res;
} ll fpow(ll a,ll b,ll p) //快速幂
{
ll r=,base=a%p;
while(b){
if(b&) r*=base, r%=p;
base*=base;
base%=p;
b>>=;
}
return r;
} void init()
{
ll x=MOD;
while(x>) x=(mp[x]=phi(x));
mp[]=;
} ll solve(int i,ll p)
{
if(i==-) return ;
if(p==) return ;
if(s[i]=='') return (*fpow(,solve(i-,mp[p]),p)-+p)%p;
if(s[i]=='') return (*solve(i-,p)++p)%p;
if(s[i]=='') return (solve(i-,p)++p)%p;
return ;
} int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
int len=strlen(s);
printf("%lld\n",solve(len-,MOD));
}
}

2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  3. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  4. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  5. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  6. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

  7. 2018牛客网暑期ACM多校训练营(第二场) A - run - [DP]

    题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...

  8. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

  9. 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述  Given a sequence of integers a1, a2, ..., an a ...

  10. 2018牛客网暑期ACM多校训练营(第九场)A -Circulant Matrix(FWT)

    分析 大佬说看样例就像和卷积有关. 把题目化简成a*x=b,这是个xor的FWT. FWT的讲解请看:https://www.cnblogs.com/cjyyb/p/9065615.html 那么要求 ...

随机推荐

  1. Hibernate_day03讲义_使用Hibernate完成一对多的关系映射并操作

  2. 在python中重新导入模块

    重新加载模块 倘若,更改了已经在 Python shell 中导入的模块,然后重新导入该模块,Python 会认为“我已经导入了该模块,不需要再次读取该文件”,所以更改将无效. 要解决这个问题,有以下 ...

  3. MessageDigest类提供MD5或SHA等加密算法

    MessageDigest可使用的加密方法有MD2\MD5\SHA-1\SHA-256\SHA-384\SHA-512,使用时候只替换相应参数值即可 MessageDigest md5 = Messa ...

  4. Weblogic集群部署

    有些事情不去尝试,注定是失败,如果预知90%的失败仍然去尝试了,那也会从中学到很多,何况仅靠那10%的可能性也会成功 weblogic安装后 1.打开Configuration Wizard 2.创建 ...

  5. Python easyGUI 登录框 非空验证

    import easygui as g msg='欢迎注册' title='注册' fieldNames=['*用户名','*密码','*重复密码','真实姓名','手机号','QQ','e-mail ...

  6. Python 文件学习笔记

    程序1 在上一题的基础上扩展,用户可以随意输入要显示的行数. 如输入2:5表示打印第2行到第5行的内容: 输入:2表示打印从开头到第2行的内容: 输入4:表示打印从第4行到结尾的内容: 输入:表示打印 ...

  7. 利用shell脚本自动获取awr报表

    观察Oracle数据库性能,oracle自带的awr功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告.通过报告可以了解一个系统的整个运行情况,生成的报告包括多个 ...

  8. python --help查询python相关命令

    C:\Users\lenovo>python --help usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... O ...

  9. informix中的时间计算

    今天看SUN服务器是的mail(vi   /var/mail/xxxuser),发现定时任务上的一些存储过程执行有错误,其中有一个错误是long transaction,长事务错误,到数据库一查,天哪 ...

  10. es5.0 安装ik中文分词器 mac

    es5.0集成ik中文分词器,网上资料很多,但是讲的有点乱,有的方法甚至不能正常运行此插件 特别注意的而是,es的版本一定要和ik插件的版本相对应: 1,下载ik 插件: https://github ...