天天看點

高精度 - SGU 112 a^b-b^aa^b-b^a  Problem's Link

Mean: 

analyse:

簡單題,隻用編個高精度乘法和減法即可.

Time complexity: O(N)

view code

 java

import java.math.BigInteger;

import java.util.Scanner;

public class Solution

{

   public static void main(String[] args)

   {

       Scanner cin = new Scanner(System.in);

       int a, b;

       while(cin.hasNext())

       {

           a = cin.nextInt();

           b = cin.nextInt();

           System.out.println(BigInteger.valueOf(a).pow(b).add(BigInteger.valueOf(b).pow(a).negate()));

       }

   }

}

C++

/**

* -----------------------------------------------------------------

* Copyright (c) 2016 crazyacking.All rights reserved.

*       Author: crazyacking

*       Date  : 2016-01-06-20.55

*/

#include <queue>

#include <cstdio>

#include <set>

#include <string>

#include <stack>

#include <cmath>

#include <climits>

#include <map>

#include <cstdlib>

#include <iostream>

#include <vector>

#include <algorithm>

#include <cstring>

using namespace std;

typedef long long(LL);

typedef unsigned long long(ULL);

const double eps(1e-8);

#define inf 0xfffffff

#define CLR(a,b) memset((a),(b),sizeof((a)))

int const nMax = 1000;

int const base = 10;

typedef int LL;

typedef pair<LL,LL> pij;

struct Int

   int a[nMax];

   int len;

   void clear()

       CLR(a,0);

       len=0;

       return ;

   Int (int n=0)

       clear();

       while(n)

           a[len++]=n%base;

           n/=base;

   bool operator < (const Int& b)const

       if(len<b.len)return true;

       if(len>b.len)return false;

       for(int i=len-1; i>=0; i--)

           if(a[i]<b.a[i])return true;

           if(a[i]>b.a[i])return false;

       return false;

   Int operator +(Int&b)

       Int c;

       c.len=max(len,b.len);

       int d=0;

       for(int i=0; i<c.len; i++)

           c.a[i]=(a[i]+b.a[i]+d)%base;

           d=(a[i]+b.a[i]+d)/base;

       while(d)

           c.a[c.len++]=d%base;

           d/=base;

       return c;

   Int operator -(Int&b)

       c.len=len;

       int f;

           c.a[i]=a[i]-b.a[i]-d;

           d=0;

           if(c.a[i]<0)

           {

               c.a[i]+=base;

               d++;

           }

           //   printf("c[%d]=%d\n",i,c.a[i]);

       while(c.a[c.len-1]==0&&c.len>0)

           c.len--;

       if(c.len==0)c.len=1;

   Int operator *(int &b)

       int d;

           c.a[i]=(a[i]*b+d)%base;

           d=(a[i]*b+d)/base;

   Int operator *(Int & b)

       for(int i=0; i<len; i++)

           for(int j=0; j<b.len; j++)

               c.a[i+j]+=a[i]*b.a[j];

       c.len=len+b.len-1;

       int d=0;       //就是這裡忘了置0

           f=d+c.a[i];

           c.a[i]=f%base;

           d=f/base;

   void output()

       for(int i=len-1; i>=0; i--)printf("%d",a[i]);

       printf("\n");

   string Int2Str()

       string ans;

       ans.clear();

           ans+=('0'+a[i]);

       return ans;

};

Int exp(Int b,int n)

   if(n==0)return Int(1);

   Int c=exp(b,n/2);

   c=c*c;

   if(n%2==1)return c*b;

   return c;

int main()

   int a,b;

   cin>>a>>b;

   Int A(a);

   Int B(b);

   A=exp(A,b);

   B=exp(B,a);

   //  A.output();

   //  B.output();

   if(A<B)

       Int C=B-A;

       cout<<"-"<<C.Int2Str()<<endl;

   else

       Int C=A-B;

       // C.output();

       //  printf("%d\n",C.len);

       cout<<C.Int2Str()<<endl;

   return 0;

上一篇: quotation