![](http://acm.fzu.edu.cn/image/problem.gif)
Accept: 6 Submit: 29
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Given one non-negative integer A and one positive integer B, it’s very easy for us to calculate A Mod B. Here A Mod B means the remainder of the answer after A is divided by B. For example, 7 Mod 5 = 2, 12 Mod 3 = 0, 0 Mod 3 = 0.
In this problem, we use the following rules to express A.
(1) One non-empty string that only contains {0,1,2,3,4,5,6,7,8,9} is valid.
For example, 123, 000213, 99213. (Leading zeros is OK in this problem)
(2) If w is valid, then [w]x if valid. Here x is one integer that 0<x<10.
For example, [012]2=012012, [35]3[7]1=3535357.
(3) If w and v are valid, then wv is valid.
For example, w=[231]2 and v=1, then wv=[231]21 is valid (which is 2312311).
Now you are given A and B. Here A is express as the rules above and B is simply one integer, you are expected to output the A Mod B.
Input
The first line of the input contains an integer T(T≤10), indicating the number of test cases.
Then T cases, for any case, only two lines.
The first line is one non-empty and valid string that expresses A, the length of the string is no more than 1,000.
The second line is one integer B(0<B<2,000,000,000).
You may assume that the length of number A in decimal notation will less than 2^63.
Output
Sample Input
Sample Output
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#define ll __int64
using namespace std;
const int N = 2222;
char s[N];
ll MOD=10007;
int exist[N];
ll POW(ll base,ll n){
ll ans = 1;
while(n){
if(n&1) (ans*=base)%=MOD;
(base*=base)%=MOD;
n >>=1;
}
return ans;
}
ll val(int i,int j){
ll ret = 0;
while(s[i]=='[') i++;
while(s[j]==']') j--;
for(;i <= j;i++){
ret = ret * 10 + s[i]-'0';
ret %= MOD;
}
return ret;
}
int weishu(ll n){
if(!n)return 1;
int ret=0;
while(n){
n /=10;
ret++;
}
return ret;
}
ll Z(ll left,ll right,int w){
if(right == 0)return 0;
ll ret = 0;
ll tmp = POW(10,w);
for(ll i = 0;i < right;i++){
ret = ret * tmp + left;
ret %= MOD;
}
return ret;
}
ll digui(int l,int r,int rr,int &wei){// [[33]3]这种结构
if(exist[r]==exist[l]){ // 此时[33]3结构
wei = (r-l+1)*val(r+2,rr);
return Z(val(l,r),val(r+2,rr),r-l+1);
}else{ // 要处理[[33]3]4这种结构
ll ans = 0;
int tWei = 0;
int li,lj,rj;
for(li = l;li <= r;li++){
if(s[li] == '['){
int c = 1;
int j = li+1;
int tmp_wei;
while(c){
if(s[j]=='[')
c++;
else if(s[j]==']')
c--;
j++;
}
ll tt = digui(li+1,j-2,j,tmp_wei);
ans = ans * POW(10,tmp_wei) % MOD;
tWei += tmp_wei;
ans = (ans+tt)%MOD;
li = j;
}else{
ans = (ans * 10+ s[li]-'0')%MOD;
tWei += 1;
}
}
wei = tWei * val(r+2,rr);
return Z(ans,val(r+2,rr),tWei);
}
}
ll cal()
{
int len = strlen(s);
int count=1;
ll ans = 0;
for(int i = 0;i<len;i++){
if(s[i] == '['){
int c = 1;
int j = i+1;
int tmp_wei;
while(c){
if(s[j]=='[')
c++;
else if(s[j]==']')
c--;
j++;
}
ll tt = digui(i+1,j-2,j,tmp_wei);
ans = ans * POW(10,tmp_wei) % MOD;
ans = (ans+tt)%MOD;
i = j;
}else{
ans = (ans * 10+ s[i]-'0')%MOD;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%s",s);
cin >> MOD;
exist[0]=0;
int len= strlen(s);
for(int i = 1;i < len;i++){
exist[i]= s[i]==']'?exist[i-1]+1:exist[i-1];
}
printf("%d\n",cal());
}
return 0;
}