CSUFT 1003 All Your Base

1003: All Your Base

Time Limit: 1 Sec      Memory Limit: 128 MB
Submit: 4      Solved: 2

Description

Premise: Given a specification for a “base” (well, actually a mixed radix number system), take in pairs of numbers written in our “base”, perform a specified operation on them and output the result in our base.

The Base: A number system where the right-most digit (digit 1) can be a counting number between 0 and 1, the second right-most digit (digit 2) can be a counting number between 0 and 2 and, more generally, each digit n (as labeled from the right) can have values between 0 and n. After 9, upper case letters are used, starting with A and going through Z. After the highest digit (which can be 0-Z), no further digits are possible; any numbers which go past that digit are invalid. Negative numbers are prefixed with a single “-” sign. Numbers never have leading zeros, with the exception of zero itself, which is represented by a single “0” character.

Operations: Addition (+) and subtraction (-): The numbers are added or subtracted as normal (including carrying, borrowing, etc).

Input

  • The first line of input is the number (base 10) of operations that need to be performed.
  • Each following line will be at most 1000 bytes and will consist of a variable-radix number, a space, a single character indicating the operation (+, or -), a space, another variable-radix number, and a newline (LF).
  • Either number for any operation (and also the result) may be negative.

Output

  • For each operation in the input, a single line of output should be produced containing either the result (a variable-radix number) or the string “Invalid”) (without quotes) followed by a newline (LF).
  • If either of the input numbers or the resulting number is not valid in the number system, or an error is encountered while performing the operation, the result is invalid.

Sample Input

3
3 + 5
9987654321 + 1
-A000000000 - 1

Sample Output

Invalid
A000000000
-A000000001

HINT

Source

2011 ACM ICPC South Central USA Regional Programming Contest

C语言差点写炸了,/(ㄒoㄒ)/~~,还是基本功不牢。

熟悉一下java,要注意的是:把每一位的权值计算出来,是 d[i] = d[i-1]*i;

把String 转成大整数,相加减得到结果,再返回去,去掉前导零。怎么得到数字,只要除以权值d[i];

//package Main;

import java.util.*;

import java.math.*;;

public class Main {

    BigInteger[] digit = new BigInteger[40];
    BigInteger A, B, C;
    boolean flag = true;

    void Getdigit() {
        digit[1] = BigInteger.ONE;
        for(int i=2;i<40;i++) {
            digit[i] = digit[i-1].multiply(BigInteger.valueOf(i));
        }
    }

    int GetVal(char c) {
        if(c<=‘9‘&&c>=‘0‘)
            return c-‘0‘;
        else return c-‘A‘+10;
    }

    char RetVal(BigInteger i) {
        if(i.intValue()<10)
            return (char)(i.intValue()+‘0‘);
        else return (char)(i.intValue()-10+(int)‘A‘);
    }

    BigInteger Check(String num) {
        BigInteger ret = BigInteger.ZERO;
        for(int i=num.length()-1,base = 2;i>=0;i--,base++) {
            if(num.charAt(i)==‘-‘) break;
            int tm = GetVal(num.charAt(i));
            if(tm>=base)
            {
                flag = false;
                return ret;
            }
            ret = ret.add(digit[base-1].multiply(BigInteger.valueOf(tm)));
        }
        if(ret.compareTo(digit[36])>=0) {
            flag = false;
            return ret;
        }
        if (num.charAt(0)==‘-‘) {
            ret = BigInteger.ZERO.subtract(ret);
        }
        return ret;
    } 

    void Print(BigInteger num) {
        if(num.compareTo(BigInteger.ZERO)<0)
        {
            System.out.printf("-");
            num = BigInteger.ZERO.subtract(num);
        }
        int i = 35;
        for(;i>1;i--) {
            if(num.divide(digit[i]).compareTo(BigInteger.ZERO)>0)
                break;
        }
        for(;i>=1;i--) {
            System.out.printf("%c", RetVal(num.divide(digit[i])));
            num = num.mod(digit[i]);
        }
        System.out.println();

    }

    void main() {
        Getdigit();
        String num1, num2, op;
        Scanner cin = new Scanner(System.in);
        int cas = cin.nextInt();
        for (int i = 0; i < cas; i++) {

            flag = true;
            num1 = cin.next();
            op = cin.next();
            num2 = cin.next(); 

            A = Check(num1);
            B = Check(num2);

            if (!flag) {
                System.out.println("Invalid");
                continue;
            }
            if (op.charAt(0) == ‘+‘) {
                C = A.add(B);
            } else {
                C = A.subtract(B);
            }
            if (C.abs().compareTo(digit[36]) >= 0) {
                System.out.println("Invalid");
                continue;
            } else {
                Print(C);
            }

        }
    }

    public static void main(String[] args) {
        new Main().main();
    }

}

/**************************************************************
    Problem: 1003
    User: YinJianZuiShuai
    Language: Java
    Result: Accepted
    Time:196 ms
    Memory:8424 kb
****************************************************************/
时间: 2024-10-11 04:46:10

CSUFT 1003 All Your Base的相关文章

Linux设备树(六 memory&amp;chosen节点)

六 memory&chosen节点 根节点那一节我们说过,最简单的设备树也必须包含cpus节点和memory节点.memory节点用来描述硬件内存布局的.如果有多块内存,既可以通过多个memory节点表示,也可以通过一个memory节点的reg属性的多个元素支持.举一个例子,假如某个64位的系统有两块内存,分别是 ? RAM: 起始地址 0x0, 长度 0x80000000 (2GB)? RAM: 起始地址 0x100000000, 长度 0x100000000 (4GB) 对于64位的系统,根

OpenVPN (base CentOS6.6)

VPN基本概念 虚拟专用网VPN 功能:在不安全的公共网络上建立安全的专用网络,进行数据加密传输 VPN与隧道技术 隧道协议包括 乘客协议:被封装的协议,如PPP,SLIP 封装协议:隧道的建立.维持及断开,如L2TP.IPSec 承载协议:承载经过封装后的数据包的协议,如IP 实例部署 一.环境部署 内网主机(slave1) vpnserver(master)       vpnclient(slave2) 192.168.1.0/24      192.168.1.1         202

1003: [ZJOI2006]物流运输 最短路+dp

https://www.lydsy.com/JudgeOnline/problem.php?id=1003 数据范围很小,怎么瞎搞都行,n方dp,然后跑出最短路暴力转移,需要注意的是不能使用的可能有多个区间 /************************************************************** Problem: 1003 User: walfy Language: C++ Result: Accepted Time:180 ms Memory:1400 k

1003 阶乘后面0的数量

1003 阶乘后面0的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 720,720后面有1个0. Input 一个数N(1 <= N <= 10^9) Output 输出0的数量 Input示例 5 Output示例 1其实只要循环除五就可以找到规律,其实也可以证明出来. 1 #include <iostream> 2 #include <cstdio> 3 #

nova - nova base image id的生成算法

nova spawn instance的时候,会先create_image,下面是获取的base image的函数 def get_cache_fname(images, key):     """Return a filename based on the SHA1 hash of a given image ID.     Image files stored in the _base directory that match this pattern     are c

使用国内镜像通过pip安装python的一些包 Cannot fetch index base URL http://pypi.python.org/simple/

原文地址:http://www.xuebuyuan.com/1157602.html 学习flask,安装virtualenv环境,这些带都ok,但是一安装包总是出错无法安装, 比如这样超时的问题: (env)[email protected]:~/flask_study/venv-test/test$ easy_install Flask-SQLAlchemy Searching for Flask-SQLAlchemy Reading http://pypi.python.org/simpl

thinkphp中SQLSTATE[42S02]: Base table or view not found: 1146 Table错误解决方法

随手记录下今天在thinkphp3.2.3中遇到的错误SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.file_info' doesn't exist,之前用pdo连接数据库一直没有问题,今天突然发现报了上述错误,查看了别人的解决方法,都没有解决,后来看了下之前的表名都是小写的,我现在的表名是"file_Info",改为"file_info"后就不报异常了

BZOJ 1003: [ZJOI2006]物流运输trans

二次联通门 : BZOJ 1003: [ZJOI2006]物流运输trans /* BZOJ 1003: [ZJOI2006]物流运输trans Spfa + Dp Spfa预处理出i到j天的最小花费 然后N^2 dp即可 */ #include <cstdio> #include <iostream> #include <cstring> #include <queue> #define INF 1e6 const int BUF = 12312313;

(转)C# :base的用法(冒号后面的base)

c# 中关于: base()用法,在此记录一下,方便查阅 1.this是标识当前资源对象的,而base是基于父级的. 2.base发挥了期灵魂级的作用--多态 3.base子类可以访问父类成员 4.base调用父类方法必须重写父类方法 5.base子类构造函数直接访问:base父类构造方法 6.base同样不能用于静态方法 public abstract class HttpBasedTransport : ClientTransportBase { protected HttpBasedTra