链接:牛客网暑期ACM多校训练营(第四场):A Ternary String

题意:给出一段数列 s,只包含 0、1、2 三种数。每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之后第一个数字消失。求最后为空串需要多少秒。

题解:

(1)如果在消除一个 0 前经过了 n 秒,那么消掉这个 0 需要 n + 1 秒。

(2)如果在消除一个 1 前经过了 n 秒,那么消掉这个 1 与其产生的所有数需要 (n + 1) * 2 秒。

(3)如果在消除一个 2 前经过了 n 秒,那么消掉这个 2 与其产生的所有数需要 (2 ^ (n + 1) - 1) * 3 秒。

需要用欧拉定理降幂。

#include <bits/stdc++.h>
using namespace std; const double EPS = 1e-;
const int INF = 0x3f3f3f3f;
const long long mod = 1e9 + ;
const int maxn = 1e5 + ;
char s[maxn];
map<long long, long long> phi; long long Eul(long long n)
{
long long ans = n;
for(int i = ; i * i <= n; i++){
if(n % i == ){
ans -= ans / i;
while(n % i == ) n /= i;
}
}
if(n > ) ans -= ans / n; return ans;
} long long Mod(long long x, long long y) //欧拉定理的条件
{
return x < y ? x : x % y + y;
} long long pow_mod(long long x, long long n, long long mod)
{
long long ans = ;
while(n){
if(n & ) ans = Mod(ans * x, mod);
x = Mod(x * x, mod);
n >>= ;
}
return ans;
} long long DFS(int i, long long mod)
{
if(i < ) return ; if(s[i] == '') return Mod((DFS(i-, mod) + ), mod);
if(s[i] == '') return Mod((DFS(i-, mod) + ) * , mod);
if(s[i] == '') return Mod((pow_mod(, DFS(i-, phi[mod]) + , mod) - ) * , mod); return ;
} int main()
{
for(long long x = mod, y = Eul(x); x != y; y = Eul(y)) phi[x] = y, x = y; phi[] = ; int T;
scanf("%d", &T);
while(T--){
scanf("%s", s); printf("%lld\n", DFS(strlen(s) - , mod) % mod);
} return ;
}

牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)的更多相关文章

  1. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  2. 牛客网 暑期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 ,问你 ...

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

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

  4. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  5. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

  6. 牛客网暑期ACM多校训练营(第九场)D

    链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...

  7. 牛客网暑期ACM多校训练营(第二场)B discount

    链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...

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

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

  9. 牛客网暑期ACM多校训练营(第二场) I Car 思维

    链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...

  10. 牛客网暑期ACM多校训练营(第二场) D money 思维

    链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...

随机推荐

  1. Bayesian Theorem

  2. 错误:maximum number of expressions in a list is 1000

    某一日发现这么如下这么一个错误  --> maximum number of expressions in a list is 1000 原因:因为SQL语句中用到了IN字句,而IN中的元素个数 ...

  3. 字符型设备驱动程序-first-printf以及点亮LED灯(一)

    学习使用 Linux 的  字符型设备驱动 来 进行 . 学习地址:http://edu.51cto.com/lesson/id-25710.html 第一步: 首先写 三个函数 ,2017年5月17 ...

  4. SSAS 度量值中的distinct count局聚合方式会数为null的值

    我们来看一个例子 Analysis Services: For Distinct Count measure NULL = 0 If you are to look at the table of v ...

  5. pl/sql下载

    详解Oracle客户端工具:PL/SQL工具下载: 下载地址:http://www.oraclejsq.com/article/010100114.html

  6. js随笔--关于数组

    1.split()将一个字符串分割成字符串数组 stringObject.split(separator,howmany) separator:必需,字符串或正则表达式,从该参数指定的地方分割stri ...

  7. code#5 P4 逻辑树

    逻辑树   时间限制: 3.0 秒 空间限制: 512 MB 相关文件: 题目目录 题目描述 有一棵树,叫逻辑树. 这个树有根,有 2N−1 个节点,其中 N 个叶子,每个非叶节点恰好有两个孩子. 每 ...

  8. php ajax confirm 删除

    <button name="del" type="button" class="btn btn-primary btn-xs" id= ...

  9. day 32 管道,信号量,进程池,线程的创建

    1.管道(了解) Pipe(): 在进程之间建立一条通道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象,强调一点:必须在产生Process对象之前产生管道. ...

  10. Spark Streaming 进阶与案例实战

    Spark Streaming 进阶与案例实战 1.带状态的算子: UpdateStateByKey 2.实战:计算到目前位置累积出现的单词个数写入到MySql中 1.create table CRE ...