2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]
题目链接:https://www.nowcoder.com/acm/contest/142/A
题目描述
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'$ 秒结时,它以及由它所产生(直接或间接)的所有数字全部消失。
那么,对于三个数字就有三种对应情况:
- 数字0:$t' = t + 1$,不管经过多少秒,0都不会再产生任何数字,所以只需要1秒钟就能消除掉;
- 数字1:$t' = 2t + 2$,经过 $t$ 秒后,总共产生 $t$ 个0,在 $t+1$ 秒时,又产生一个0,同时1消失,则还剩下 $t+1$ 个0,所以总共花费 $1+t+1$ 秒消除掉全部;
- 数字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 - [欧拉降幂公式][扩展欧拉定理]的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)
Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)
题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...
- 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]
题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第二场) A - run - [DP]
题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...
- 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]
题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs and where are i ...
- 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]
题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述 Given a sequence of integers a1, a2, ..., an a ...
- 2018牛客网暑期ACM多校训练营(第九场)A -Circulant Matrix(FWT)
分析 大佬说看样例就像和卷积有关. 把题目化简成a*x=b,这是个xor的FWT. FWT的讲解请看:https://www.cnblogs.com/cjyyb/p/9065615.html 那么要求 ...
随机推荐
- iOS UITextField控件总结
先声明下面总结不是自己写的. 参考网址:http://blog.csdn.net/tskyfree/article/details/8121915 //初始化textfield并设置位置及大小 U ...
- Aspose------导出Excel
代码: /// <summary> /// 导出Excel /// </summary> /// <typeparam name="T">泛型类 ...
- 第四章 TCP粘包/拆包问题的解决之道---4.2--- 未考虑TCP粘包导致功能异常案例
4.2 未考虑TCP粘包导致功能异常案例 如果代码没有考虑粘包/拆包问题,往往会出现解码错位或者错误,导致程序不能正常工作. 4.2.1 TimeServer 的改造 Class : TimeServ ...
- form enctype:"multipart/form-data",method:"post" 提交表单,后台获取不到数据
在解决博问node.js接受参数的时候,发现当form中添加enctype:"multipart/form-data",后台确实获取不到数据,于是跑到百度上查了一下,终于明白为什么 ...
- Spring4 Quartz2 动态任务,Spring4整合quartz2.2.3简单动态任务
Spring4 Quartz2 动态任务 Spring4整合quartz2.2.3简单动态任务, Quartz2.2.3简单动态定时任务二, SimpleScheduleBuilder简单定时任务 ...
- Python 网络爬虫
爬虫介绍 爬取图片 爬取文本 爬虫相关模块:re 爬虫相关模块:urllib 爬虫相关模块:urllib2 爬虫相关模块:cookielib 爬虫相关模块:requests 爬取需要登录的页面
- Lua脚本和C++交互(一)
现在,越来越多的C++服务器和客户端融入了脚本的支持,尤其在网游领域,脚本语言已经渗透到了方方面面,比如你可以在你的客户端增加一个脚本,这个脚本将会帮你在界面上显示新的数据,亦或帮你完成某些任务,亦或 ...
- Python入门 学习笔记
十六进制:0x123 布尔运算:and, or, not 空值:None 注释:# raw字符串不需要转义:r'XXX' 多行字符:'''XXX''' 多行字符+raw字符串:r'''XXX''' U ...
- Java读取maven目录下的*.properties配置文件
public class ReadProperties{ private static String proFileName = "/config/MQSubjectId.propertie ...
- (原创)Android Binder设计与实现 - 实现篇(1)
本文属于原创作品,转载请注明出处并放于明显位置,原文地址:http://www.cnblogs.com/albert1017/p/3849585.html 前言 在学习Android的Binder机制 ...