2017百度之星复赛1001 Arithmetic of Bomb
来源:互联网 发布:lol外服转国服 mac 编辑:程序博客网 时间:2024/06/05 00:51
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 取模。
这题我一开始看错了以为字符串重复的次数也可能会是一个100位以上的数字,于是就用了类似于快速幂的做法来展开,同时一边展开一边对1e9+7取余数,最后得到正确答案。代码:
#include <cmath>#include <queue>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define ll long longusing namespace std;inline void read(int &x){ char ch; bool flag=false; for (ch=getchar();!isdigit(ch);ch=getchar())if (ch=='-') flag=true; for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar()); x=flag?-x:x;}inline void write(int x){ static const int maxlen=100; static char s[maxlen]; if (x<0) { putchar('-'); x=-x;} if(!x){ putchar('0'); return; } int len=0; for(;x;x/=10) s[len++]=x % 10+'0'; for(int i=len-1;i>=0;--i) putchar(s[i]);}char s[3000];long long num[3000];const long long P=1000000007;long long get_pow(long long a,long long b){if (b==1) return a;long long tmp=get_pow(a,b/2)%P;if (b%2==0) return (tmp*tmp)%P;else return ( (tmp*tmp)%P*a)%P;}long long get_num(long long a,long long b ,int c){if (b==1) return a;if (b%2==0) return ( get_num(a,b/2,c)*get_pow( num[c] , b/2 )%P+get_num(a,b/2,c)%P )%P;else return ( get_num(a,b/2,c)*get_pow( num[c] , b/2+1 )%P+get_num(a,b/2,c)*num[c]%P+a%P )%P;}void doit(){scanf("%s",s);int n=strlen(s);long long tmp1=0;long long tmp2=0;int len1;long long ans=0;for (int i=0;i<n;i++) if (s[i]=='(') { int j=i+1; tmp1=0; len1=0; while ( s[j]!=')') { tmp1=( ( tmp1*10 )+s[j]-'0' )%P; j++; len1++; } j+=3; tmp2=0; while ( s[j]!=')') { tmp2=( ( tmp2*10 )+s[j]-'0' )%P; j++; } i=j; ans=( ans*get_pow(num[len1],tmp2)%P+get_num (tmp1,tmp2,len1)%P ) %P; } else ans=( ans*10ll+ s[i]-'0')%P;cout<<ans<<endl;}int main(){ int T; num[0]=1; for (int i=1;i<=1000;i++) num[i]=num[i-1]*10ll%P; read(T); for (int i=1;i<=T;i++) doit(); return 0;}CloseBestCoder Contest System 2.0Copyright
阅读全文
0 0
- 2017百度之星复赛1001 Arithmetic of Bomb
- 2017百度之星复赛1001 Arithmetic of Bomb
- HDU-2017"百度之星"程序设计大赛-复赛-1001-Arithmetic of Bomb
- 百度之星复赛--Arithmetic of Bomb----暴力
- Hdu6144 Arithmetic of Bomb(2017百度之星程序设计大赛
- HDU6144 Arithmetic of Bomb (2017百度之星程序设计大赛
- HDU6144-Arithmetic of Bomb
- HDU6144 Arithmetic of Bomb
- 2017百度之星 复赛
- HDU 6144:Arithmetic of Bomb
- [百度之星复赛T5]
- 百度之星复赛题解
- HDU 6144 Arithmetic of Bomb(模拟)
- Hdu 6144 Arithmetic of Bomb 模拟
- 【字符串处理】HDU6144 Arithmetic of Bomb
- 2014百度之星复赛解题报告复赛:Race
- 百度之星 2015 复赛 1001 (数长方形)
- HDU-2017"百度之星"程序设计大赛-复赛-补题
- 如何设置EditPlus保存时不生成bak文件
- 学生免费使用JetBrains系列产品
- dubbo与spring整合使用(服务与引用)
- Java连接Mysql(转至百度百科)
- poj 1150:The Last Non-zero Digit(求A(n, m)的最后一个非0位)
- 2017百度之星复赛1001 Arithmetic of Bomb
- webshell一句话木马
- Fastjson生成json时Null属性不显示
- NYOJ题目1057-寻找最大数(三)
- linux nfs
- Linux设备树解析
- Recurrences UVA
- 随想录
- [USACO12FEB]Nearby Cows solution 题解 [洛谷3047]