题目链接: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. [OpenCV] Samples 06: logistic regression

    logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...

  2. Android刮刮卡效果

                         不多说,直接上代码: package com.example.test; import android.app.Activity; import androi ...

  3. ios开发之NSString用strong还是用copy?

    代码如下: 1,声明 @property(nonatomic,strong)NSString *firstName; @property(nonatomic,copy)NSString *second ...

  4. 【Android】amr播放

    http://download.csdn.net/download/r8hzgemq/4877495 http://www.cnblogs.com/fengzhblog/archive/2013/08 ...

  5. GSAP JS基础教程--TweenLite操作元素的相关属性

    今天来学习用TweenLite操作元素的各种属性,以Div为例,其他元素的操作也是一样的,只是可能一些元素有它们的特殊属性,就可能不同罢了.   代码里用详细注释,我就不再重复啦,大家看代码就可以啦! ...

  6. httpClient创建对象、设置超时

    从老版本和新版本进行比较说明: 1.创建HttpClient对象 3.X: HttpClient httpClient = new DefaultHttpClient(); 4.3: Closeabl ...

  7. Selenium 切换 Frame

    我们知道网页中有一种节点叫作 iframe ,也就是子 Frame ,相当于页面的子页面,它的结构和外部网页的结构完全一致. Selenium 打开页面后,它默认是在父级 Frame 里面操作,而此时 ...

  8. VS2017编译Poco1.9.0的64版本

    需要先准备好OpenSSL1.0.2 下载poco-poco-1.9.0-release.zip,解压,修改buildwin.cmd中的OPENSSL_DIR路径,特别注意OPENSSL_LIB的路径 ...

  9. 判断App整体处于前台还是后台

    1.通过RunningTaskInfo类判断(需要额外权限): 复制代码代码如下: /** *判断当前应用程序处于前台还是后台 */ public static boolean isApplicati ...

  10. Spring系列之IOC容器

    一.概述 IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象之间的依赖.应用程序无需直接在代码中new 相关的对象,应用程序由IOC容器进行组装.在S ...