牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期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(欧拉降幂)的更多相关文章
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期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 ,问你 ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
- 牛客网暑期ACM多校训练营(第九场)D
链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...
- 牛客网暑期ACM多校训练营(第二场)B discount
链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 牛客网暑期ACM多校训练营(第二场) I Car 思维
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...
- 牛客网暑期ACM多校训练营(第二场) D money 思维
链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...
随机推荐
- ant design 修改tab样式
.ant-tabs-ink-bar{ background-color: transparent !important; } .ant-tabs-top .ant-tabs-ink-bar-anima ...
- MRC 和 ARC 混编
在targets的build phases选项下Compile Sources下选择要不使用arc编译的文件,双击它,输入 -fno-objc-arc 即可 MRC工程中也可以使用ARC的类.方法 ...
- Source folder is not on the Java build class path
源文件夹不在Java构建类路径上 只需右键单击文件夹src - > build path - >Using source folders就是这样
- React Native封装Toast与加载Loading组件
React Native开发封装Toast与加载Loading组件 在App开发中,我们避免不了使用的两个组件,一个Toast,一个网络加载Loading,在RN开发中,也是一样,React Nati ...
- 基于 HTML5 Canvas 的 3D 渲染引擎构建机架式服务器
前言 今天找到了 HT 的官网里的 Demo 网站( http://www.hightopo.com/demos/index.html ),看的我眼花缭乱,目不暇接. 而且 HT 的用户手册,将例子和 ...
- html表单相关标签及属性
1.<form>标签 定义整体的表单区域 action属性 定义表单数据提交地址 method属性 定义表单提交的方式,一般有“get”方式和“post”方式 2.<label> ...
- C语言顺序队列
顺序队列是一种只能在一头进和另一头出的数据结构,所以结构体里设2个指针分别指向头部和尾部,用数组来存储数据. #define MAXSIZE 1024 typedef int elemtype; ty ...
- golang基础--method方法
Go没有类似python语言中类class的概念,但依旧有method 类型方法定义格式如下 func (a mytype) method_name(x type)(y type){ return y ...
- scRNA-seq genomic analysis pipline
a scRNA-seq genomic anlysis pipline .caret,.dropup>.btn>.caret{border-top-color:#000!important ...
- 以太坊入门-solidity环境搭建
本地remix-ide安装教程 一 开始安装前 准备以下软件 vs2015(主要会用到vc++的工具以及Windows sdk ||vs2017没试过,不过应该可以) vs下载安装以及解压地址: h ...