Hdu 6144 Arithmetic of Bomb 模拟

来源:互联网 发布:软件销售管理制度 编辑:程序博客网 时间:2024/05/29 07:58

Arithmetic of Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 164    Accepted Submission(s): 118


Problem Description
众所周知,度度熊非常喜欢数字。

它最近在学习小学算术,第一次发现这个世界上居然存在两位数,三位数……甚至N位数!

但是这回的算术题可并不简单,由于含有表示bomb的#号,度度熊称之为 Arithmetic of Bomb。



Bomb Number中的bomb,也就是#号,会展开一些数字,这会导致最终展开的数字超出了度度熊所能理解的范畴。比如”(1)#(3)”表示”1”出现了3次,将会被展开为”111”,

同理,”(12)#(2)4(2)#(3)”将会被展开为”12124222”。

为了方便理解,下面给出了Bomb Number的BNF表示。

```
<bomb number> := <bomb term> | <bomb number> <bomb term>
<bomb term> := <number> | '(' <number> ')' '#' '(' <non-zero-digit> ')'
<number> := <digit> | <digit> <number>
<digit> := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
<non-zero-digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
```

请将Bomb Number中所有的#号展开,由于数字可能很长,结果对 1 000 000 007 取模。
 

Input
第一行为T,表示输入数据组数。

每组数据包含一个Bomb Expression。


- 1≤T≤100

- 1≤length(Bomb Number)≤1000
 

Output
对每组数据输出表达式的结果,结果对 1 000 000 007 取模。
 

Sample Input
41(1)#(3)(12)#(2)4(2)#(3)(12)#(5)
 

Sample Output
111112124222212121205
 

Source
2017百度之星程序设计大赛 - 复赛


没什么好说的,直接模拟。


对于每一位数,直接求在第几位。

对于每个循环节,求完之后直接乘10的相应倍数即可。

#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=1005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f,mod=1e9+7;   const ld pi=acos(-1.0L);  char s[maxn];int main() {//freopen("F.in","r",stdin);//freopen("F.out","w",stdout);int cas;scanf("%d",&cas);while (cas--) {int i,len,j;ll ans,base,tem;scanf("%s",s);ans=0;base=1;i=strlen(s);while (i>0) {i--;if (s[i]==')') {len=s[i-1]-'0';i-=5;tem=0;ll ba=1;while (s[i]!='(') {tem=tem+(s[i]-'0')*ba;tem%=mod;ba*=10;ba%=mod;i--;}tem*=base;tem%=mod;for (j=1;j<=len;j++) {ans+=tem;ans%=mod;tem=(tem*ba)%mod;}for (j=1;j<=len;j++) {base*=ba;base%=mod;}}else {ans+=(s[i]-'0')*base;ans%=mod;base=(base*10)%mod;}}printf("%I64d\n",ans);}return 0;}



原创粉丝点击