zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #96 (Div. 2) 133B. Unary(快速幂)

    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 <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    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;
    }
  • 相关阅读:
    AcWing 204. 表达整数的奇怪方式 / Strange Way To Express Integers
    Codeforces Edu Round 67 A-C + E
    Codeforces Edu Round 66 A-E
    Codeforces Edu Round 65 A-E
    Codeforces Edu Round 64 A-D
    Codeforces Edu Round 63 A-E
    Codeforces Edu Round 62 A-E
    Codeforces Edu Round 61 A-C + F
    python 线程池和锁
    python 线程
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2277374.html
Copyright © 2011-2022 走看看