2020暑假牛客多校 A (高精度) java第一题

tql
py3 : print(eval(input().replace('(', '**(')))



import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Scanner;
//import java.text.*;

public class Main {
	static String s;
	static int n, x;
	
	public static void main(String[] args) {
		
        Scanner cin = new Scanner (new BufferedInputStream(System.in));
		//Scanner sc=new Scanner(System.in);
        s = cin.nextLine();
        n = s.length();
        x = -1;
        BigInteger t = dfs();
        System.out.println(t);

	}

	static BigInteger qpow(BigInteger a, BigInteger b) {
		BigInteger res = BigInteger.valueOf(1);
		while(b.compareTo(BigInteger.valueOf(0)) !=0) {
			BigInteger tt = b.mod(BigInteger.valueOf(2));
			if(tt.compareTo(BigInteger.valueOf(1)) ==0) {
				res = res.multiply(a);
			}
			a = a.multiply(a);
			b = b.divide(BigInteger.valueOf(2));
		}
		return res;
	}
	

	static BigInteger dfs() {
		x ++;
		BigInteger res = BigInteger.valueOf(0);
		BigInteger temp = BigInteger.valueOf(0);
		int flag = 0;
		while(x < n && flag != 1) {
			if(s.charAt(x) == ')') flag = 1;
			if(s.charAt(x) == '2') {
				if(x + 1 < n && s.charAt(x + 1) == '(') {
					temp = dfs();
					BigInteger tt = qpow(BigInteger.valueOf(2), temp);
					//System.out.print("(" + temp + " " + tt+ ") ");
					res = res.add(qpow(BigInteger.valueOf(2), temp));
				}
				else {
					res = res.add(BigInteger.valueOf(2));
				}
			}
			if(flag == 0) x++;
		}
		//System.out.print(t + " ");
		return res;
	}	
	
}


posted @ 2020-08-14 16:00  A_sc  阅读(86)  评论(0编辑  收藏  举报