现在的位置: 首页 > 综合 > 正文

【SGU】112. a^b – b^a 高精度

2017年10月16日 ⁄ 综合 ⁄ 共 2704字 ⁄ 字号 评论关闭

传送门:【SGU】112. a^b - b^a

题目分析:裸高精度。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std ;

#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )

typedef long long LL ;

const LL L = 1000000000 ;//1e9
const int N = 120 ;
const int MAXN = 1005 ;

struct BigInt {//only support positive number
	int digit[N] ;
	int length ;

	BigInt () : length ( 0 ) { clr ( digit , 0 ) ; }
	BigInt ( LL number ) : length ( 0 ) {
		clr ( digit , 0 ) ;
		while ( number ) {
			digit[length ++] = number % L ;
			number /= L ;
		}
	}
	BigInt operator = ( LL number ) {
		length = 0 ;
		clr ( digit , 0 ) ;
		while ( number ) {
			digit[length ++] = number % L ;
			number /= L ;
		}
		return *this ;
	}
	BigInt operator = ( const char buf[] ) {
		int len = strlen ( buf ) ;
		length = ( len - 1 ) / 9 + 1 ;
		clr ( digit , 0 ) ;
		rep ( i , 0 , len ) {
			int index = ( len - i - 1 ) / 9 ;
			digit[index] = digit[index] * 10 + buf[i] - '0' ;
		}
		return *this ;
	}
	BigInt maintain () {
		while ( length && digit[length - 1] == 0 ) -- length ;
		return *this ;
	}
	int operator [] ( const int index ) const {
		return digit[index] ;
	}
	int& operator [] ( const int index ) {
		return digit[index] ;
	}
	BigInt operator + ( const BigInt& b ) const {
		BigInt c ;
		c.length = max ( length , b.length ) + 1 ;
		LL addv = 0 ;
		rep ( i , 0 , c.length ) {
			addv += digit[i] + b[i] ;
			c[i] = addv % L ;
			addv /= L ;
		}
		return c.maintain () ;
	}
	BigInt operator - ( const BigInt& b ) const {
		BigInt c ;
		c.length = length ;
		LL delv = 0 ;
		rep ( i , 0 , length ) {
			delv += digit[i] - b[i] ;
			c[i] = delv ;
			delv = 0 ;
			if ( c[i] < 0 ) {
				LL tmp = ( -c[i] - 1 ) / L + 1 ;
				c[i] += tmp * L ;
				delv = -tmp ;
			}
		}
		return c.maintain () ;
	}
	BigInt operator * ( const BigInt& b ) const {
		BigInt c ;
		c.length = length + b.length ;
		rep ( i , 0 , length ) {
			LL mulv = 0 ;
			For ( j , 0 , b.length ) {
				mulv += ( LL ) digit[i] * b[j] + c[i + j] ;
				c[i + j] = mulv % L ;
				mulv /= L ;
			}
		}
		return c.maintain () ;
	}
	BigInt operator / ( const LL b ) const {
		BigInt c ;
		c.length = length ;
		LL divv = 0 ;
		rev ( i , length - 1 , 0 ) {
			divv = divv * L + digit[i] ;
			c[i] = divv / b ;
			divv %= b ;
		}
		return c.maintain () ;
	}
	bool operator < ( const BigInt& b ) const {
		if ( length != b.length ) return length < b.length ;
		rev ( i , length - 1 , 0 ) if ( digit[i] != b[i] ) return digit[i] < b[i] ;
		return 0 ;
	}
	bool operator > ( const BigInt& b ) const {
		return b < *this ;
	}
	bool operator <= ( const BigInt& b ) const {
		return !( b < *this ) ;
	}
	bool operator >= ( const BigInt& b ) const {
		return !( *this < b ) ;
	}
	bool operator == ( const BigInt& b ) const {
		return !( b < *this ) && !( *this < b ) ;
	}
	bool operator != ( const BigInt& b ) const {
		return b < *this || *this < b ;
	}
	BigInt operator += ( const BigInt& b ) {
		return *this = ( *this ) + b ;
	}
	BigInt operator -= ( const BigInt& b ) {
		return *this = ( *this ) - b ;
	}
	BigInt operator *= ( const BigInt& b ) {
		return *this = ( *this ) * b ;
	}
	BigInt operator /= ( const LL b ) {
		return *this = ( *this ) / b ;
	}
	void show () {
		if ( length == 0 ) printf ( "0" ) ;
		else {
			printf ( "%d" , digit[length - 1] ) ;
			rev ( i , length - 2 , 0 ) printf ( "%09d" , digit[i] ) ;
		}
		printf ( "\n" ) ;
	}
} ;

int a , b ;

void solve () {
	BigInt ans1 = 1 , ans2 = 1 ;
	For ( i , 1 , b ) ans1 *= a ;
	For ( i , 1 , a ) ans2 *= b ;
	//ans1.show () ;
	//ans2.show () ;
	BigInt ans ;
	if ( ans1 >= ans2 ) {
		ans = ans1 - ans2 ;
		ans.show () ;
	} else {
		ans = ans2 - ans1 ;
		printf ( "-" ) ;
		ans.show () ;
	}
}

int main () {
	while ( ~scanf ( "%d%d" , &a , &b ) ) solve () ;
	return 0 ;
}

代码如下:

抱歉!评论已关闭.