博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Beta Round #96 (Div. 2) 133B. Unary(快速幂)
阅读量:5121 次
发布时间:2019-06-13

本文共 3252 字,大约阅读时间需要 10 分钟。

133B.
UnaryUnary is a minimalistic Brainfuck dialect in which programs are written using only one token.Brainfuck programs use 8 commands: "
+", "
-", "
[", "
]", "
<", "
>", "
." and "
," (their meaning is not important for the purposes of this problem). Unary programs are created from Brainfuck programs using the following algorithm. First, replace each command with a corresponding binary code, using the following conversion table:
  • "> →  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).

Input

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

Output the size of the equivalent Unary program modulo 1000003 (106 + 3).

Sample test(s)
Input
,.
Output
220
Input
++++[>,.<-]
Output
61425
Note

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; }

转载于:https://www.cnblogs.com/lidaojian/archive/2011/12/05/2277374.html

你可能感兴趣的文章
mysql日期和字符相互转换
查看>>
python 配置github项目配置
查看>>
copy()之绝版应用
查看>>
sparksql
查看>>
软件工程概论 - 个人总结
查看>>
数据库系统概论 中文高清PDF版下载
查看>>
bzoj3289: Mato的文件管理
查看>>
POJ 1995 Raising Modulo Numbers (快速幂取余)
查看>>
js this 引起的祸
查看>>
我是如何写作一本软件+哲学式的书籍的(下)
查看>>
用LSTM生成武侠人名
查看>>
深度学习在graph上的使用
查看>>
apt-get常用命令(转载)
查看>>
信安之星(iSecStar)U盘安全管理系统
查看>>
每天一个linux命令(32):gzip命令
查看>>
2018/12/08 L1-037 A除以B Java
查看>>
汤唯:在街头卖艺的那些日子
查看>>
进程,线程,主线程,异步
查看>>
SQL 中having 和where的区别分析
查看>>
Windows 平台安装 MongoDB
查看>>