题目链接: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. 马尔科夫链蒙特卡洛(Markov chain Monte Carlo)

    (学习这部分内容大约需要1.3小时) 摘要 马尔科夫链蒙特卡洛(Markov chain Monte Carlo, MCMC) 是一类近似采样算法. 它通过一条拥有稳态分布 \(p\) 的马尔科夫链对 ...

  2. 关于error:Cannot assign to 'self' outside of a method in the init family

    有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Can ...

  3. 教你解锁被锁住的苹果mac电脑的文件跟文件夹,同时也可删除被锁的文件跟文件夹(转)

    在Mac OSX 下无法删除的文件可大概分为下列三种情形 1.档案(夹)被锁定 2.文件正在使用中 3.没有权限的档案(夹) 一.「 为什么档案会被锁定 」 1.个人自行替档案加上 2.在拷贝或是整理 ...

  4. ios开发之--[_NSInlineData objectForKeyedSubscript:]

    reason: '-[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fa2049 ...

  5. 新版本的body-parser中间件和morgan中间件引用问题:body-parser deprecated bodyParser和morgan deprecated morgan(options)

    引用新版本的body-parser中间件和morgan中间件时,报如下问题: Fri, 09 Jan 2015 06:32:04 GMT morgan deprecated morgan(option ...

  6. 中间件安全加固之Jboss

    JBoss 的安全设置 1) jmx-console A.jmx-console登录的用户名和密码设置 默认情况访问 http://localhost:8080/jmx-console 就可以浏览jb ...

  7. java 读写文件例子

    在linux下可以读写中文 import java.io.*; import java.text.SimpleDateFormat; import java.util.*; public class ...

  8. UISegmentedControl的基本用法

    本文转载至 http://www.tuicool.com/articles/yUfURj 原文  http://blog.csdn.net/hmt20130412/article/details/38 ...

  9. 【Java并发编程三】闭锁

    1.什么是闭锁? 闭锁(latch)是一种Synchronizer(Synchronizer:是一个对象,它根据本身的状态调节线程的控制流.常见类型的Synchronizer包括信号量.关卡和闭锁). ...

  10. WP8.1学习系列(第二十四章)——Json解析

    .net已经集成了json解析,类名叫DataContractJsonSerializer DataContractJsonSerializer 类型公开以下成员. 构造函数     名称 说明 Da ...