对于题中的"normal expression"(仅含加减乘和无前导0的非负整数,无括号)的计算,实际上并不需要通常的表达式求值,而可以用下述方式计算——

维护三元组$(a,b,c)$,分别表示已经确定的部分、下一个$\pm$之前这些数的系数和当前最后一个数字(或许解释并不清晰,可以参考转移),三者初始为$(0,1,0)$,并不断加入下一个字符$d$,转移如下
$$
\begin{cases}a'=a+bc,b'=1,c'=0&(+)\\a'=a+bc,b'=-1,c'=0&(-)\\a'=a,b'=bc,c'=0&(*)\\a'=a,b'=b,c'=10c+d&(num)\end{cases}
$$
再额外维护一个$bc$后,转移即均为线性,那么可以用矩阵描述

(具体实现还要再维护一个1,因此矩阵大小为$5\times 5$)

另外,若最终得到的三元组为$(a,b,c)$,则答案即为$a+bc$

最终,总复杂度为$o(5^{3}tl)$,可以通过

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 300005
4 #define mod 1000000007
5 #define ll long long
6 int t,n,flag;
7 ll m;
8 char s[N];
9 struct matrix{
10 int a[5][5];
11 matrix(int p=0){
12 memset(a,0,sizeof(a));
13 for(int i=0;i<5;i++)a[i][i]=p;
14 }
15 }o,ans,A[13];
16 int change(char c){
17 if (c=='+')return 10;
18 if (c=='-')return 11;
19 if (c=='*')return 12;
20 return c-'0';
21 }
22 matrix mul(matrix a,matrix b){
23 matrix ans;
24 for(int i=0;i<5;i++)
25 for(int j=0;j<5;j++)
26 for(int k=0;k<5;k++)ans.a[i][j]=(ans.a[i][j]+(ll)a.a[i][k]*b.a[k][j])%mod;
27 return ans;
28 }
29 matrix qpow(matrix a,ll m){
30 matrix s=a,ans(1);
31 while (m){
32 if (m&1)ans=mul(ans,s);
33 s=mul(s,s);
34 m>>=1;
35 }
36 return ans;
37 }
38 int main(){
39 for(int i=0;i<10;i++){
40 A[i].a[0][0]=A[i].a[1][1]=A[i].a[2][2]=1;
41 A[i].a[0][3]=A[i].a[2][4]=i;
42 A[i].a[3][3]=A[i].a[4][4]=10;
43 }
44 A[10].a[0][0]=A[10].a[1][1]=A[10].a[4][1]=A[10].a[0][2]=1;
45 A[11]=A[10],A[11].a[0][2]=mod-1;
46 A[12].a[0][0]=A[12].a[1][1]=A[12].a[4][2]=1;
47 scanf("%d",&t);
48 while (t--){
49 scanf("%s",s);
50 n=strlen(s),flag=0,ans=matrix(1);
51 for(int i=0;i<n;i++){
52 if (flag){
53 if (s[i]=='('){
54 m=0;
55 continue;
56 }
57 if (s[i]!=')')m=m*10+s[i]-'0';
58 else{
59 ans=mul(o,qpow(ans,m));
60 flag=0;
61 }
62 continue;
63 }
64 if (s[i]=='('){
65 o=ans,ans=matrix(1);
66 continue;
67 }
68 if (s[i]==')')continue;
69 if (s[i]=='#')flag=1;
70 else ans=mul(ans,A[change(s[i])]);
71 }
72 printf("%d\n",((ans.a[0][1]+ans.a[2][1])%mod+(ans.a[0][4]+ans.a[2][4])%mod)%mod);
73 }
74 return 0;
75 }

[hdu6145]Arithmetic of Bomb II的更多相关文章

  1. 2017"百度之星"程序设计大赛 - 复赛1001&&HDU 6144 Arithmetic of Bomb【java大模拟】

    Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. hdu 6144 Arithmetic of Bomb

    Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. 2017百度之星程序设计大赛 - 复赛 Arithmetic of Bomb

    http://acm.hdu.edu.cn/showproblem.php?pid=6144 解法:一个简单的模拟 #include <bits/stdc++.h> using names ...

  4. 【2017"百度之星"程序设计大赛 - 复赛】Arithmetic of Bomb

    [链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=777&pid=1001 [题意] 在这里写 [题解] ...

  5. [SinGuLaRiTy] 2017 百度之星程序设计大赛 复赛

    [SinGuLaRiTy-1038] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. Arithmetic of Bomb Problem D ...

  6. 2017"百度之星"程序设计大赛 - 复赛

    Arithmetic of Bomb  Accepts: 1050  Submissions: 1762  Time Limit: 2000/1000 MS (Java/Others)  Memory ...

  7. oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)

    LAST UPDATE:     1 Dec 15, 2016 APPLIES TO:     1 2 3 4 Oracle Database - Enterprise Edition - Versi ...

  8. 2017"百度之星"程序设计大赛 - 复赛 01,03,05

    Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. Arithmetic Slices II - Subsequence LT446

    446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...

随机推荐

  1. 题解 CF1172E Nauuo and ODT

    题目传送门 题目大意 给出一个 \(n\) 个点的树,每个点有颜色,定义 \(\text{dis}(u,v)\) 为两个点之间不同颜色个数,有 \(m\) 次修改,每次将某个点的颜色进行更改,在每次操 ...

  2. NOI2018屠龙勇士(扩展CRT + splay(multiset))

    QWQ 一到假期就颓废 哎 今年新鲜出炉的NOI题,QwQ同步赛的时候写的,后来交了一发洛谷,竟然过了 首先 根据题目,我们很容易得到,假设对应每一条龙的剑的攻击力是\(atk\)的话 \[a_i-x ...

  3. web全栈后台权限管理系统(VUE+ElementUi+nodeJs+koa2)

    web全栈后台权限管理系统(VUE+ElementUi+nodeJs+koa2) 主要技术 前端 vue 全家桶 ElementUI 后端 Node.js Koa2 Mongoess 数据库 mong ...

  4. 【UE4 设计模式】设计模式一些概念

    定义 设计模式是一套被反复使用的.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用设计模式是为了重用代码.让代码更容易被他人理解.保证代码可靠性. 四人帮 GOF ( Gang of Four ...

  5. 谈谈BEM规范(含代码)

    css规范之BEM规范 前言 引用一句经典名言在编程的世界里其中一件最难的事情就是命名,不管是设计到编程语言还是标记语言都会有命名的需求.今天聊的就是关于css的命名规范的发展过程以及演变. 命名的发 ...

  6. Github Actions 实践

    Github Actions 实践 Github Actions 是 Github 的持续集成服务,通过在 repo 发生特定的行为时执行指定的命令实现自动测试.自动部署等功能. 基本术语 workf ...

  7. Spring Cloud Gateway 网关限流

    Spring Cloud Gateway 限流 一.背景 二.实现功能 三.网关层限流 1.使用默认的redis来限流 1.引入jar包 2.编写配置文件 3.网关正常响应 4.网关限流响应 2.自定 ...

  8. 『学了就忘』Linux基础 — 5、使用VMware创建虚拟机

    目录 1.在VMware中创建虚拟机 (1)点击[创建新的虚拟机]. (2)选择系统安装方式 (3)选择客户机操作系统 (4)自定义虚拟机的名称和安装位置. (5)指定系统硬盘容量 (6)完成创建 2 ...

  9. 多线程--vthread

    vthread中包含两个类: vthread.vthread.pool vthread.vthread.thread 其中class pool的原型如下: class pool(builtins.ob ...

  10. Swarm+Docker+Portainer(集群,图形化)

    参考文章 https://blog.csdn.net/u011781521/article/details/80469804 https://blog.csdn.net/u011781521/arti ...