- ">" → 1000,
- "<" → 1001,
- "+" → 1010,
- "-" → 1011,
- "." → 1100,
- "," → 1101,
- "[" → 1110,
- "]" → 1111.
Next, concatenate the resulting binary codes into one binary number in the same order as in the program. Finally, write this number using unary numeral system — this is the Unary program equivalent to the original Brainfuck one.
You are given a Brainfuck program. Your task is to calculate the size of the equivalent Unary program, and print it modulo 1000003 (106 + 3).
The input will consist of a single line p which gives a Brainfuck program. String p will contain between 1 and 100 characters, inclusive. Each character of p will be "+", "-", "[", "]", "<", ">", "." or ",".
Output the size of the equivalent Unary program modulo 1000003 (106 + 3).
,.
220
++++[>,.<-]
61425
To write a number n in unary numeral system, one simply has to write 1 n times. For example, 5 written in unary system will be 11111.
In the first example replacing Brainfuck commands with binary code will give us 1101 1100. After we concatenate the codes, we'll get 11011100 in binary system, or 220 in decimal. That's exactly the number of tokens in the equivalent Unary program.
解题报告:找规律加上快速幂,以后大数取模要利用快速幂!
代码如下;
#include#include #include #include #include using namespace std; const int N = 110; __int64 ans; __int64 Expmod(__int64 m,__int64 n) { __int64 b=1; int k=1000003; while(n>0) { if(n%2==1) { b=(b*m)%k; } n=n/2; m=(m*m)%k; } return b; } int main() { __int64 i,len,j; char str[N]; scanf("%s",str); len=strlen(str); ans=0; for(j=len-1,i=0;j>=0;j--,i++) { if(str[j]=='>') { ans+=8*Expmod(2,4*i); ans=ans%1000003; } else if(str[j]=='<') { ans+=9*Expmod(2,4*i); ans=ans%1000003; } else if(str[j]=='+') { ans+=10*Expmod(2,4*i); ans=ans%1000003; } else if(str[j]=='-') { ans+=11*Expmod(2,4*i); ans=ans%1000003; } else if(str[j]=='.') { ans+=12*Expmod(2,4*i); ans=ans%1000003; } else if(str[j]==',') { ans+=13*Expmod(2,4*i); ans=ans%1000003; } else if(str[j]=='[') { ans+=14*Expmod(2,4*i); ans=ans%1000003; } else if(str[j]==']') { ans+=15*Expmod(2,4*i); ans=ans%1000003; } } ans=ans%1000003; printf("%I64d\n",ans); return 0; }