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

传送门:【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 ;
}


代码如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值