题意:求a^b-b^a次,100以内。大数的-和*的模拟,用的模板,注意该模板中间和结果都不能出现负数。

#include<iostream>
#include<string>
using namespace std;
const int ten[4]={1,10,100,1000};
const int maxl=1000;
struct bignum
{
int d[maxl];
bignum(string s)
{
int len=s.size();
d[0]=(len-1)/4+1;
int i,j,k;
for(i=1;i<maxl;i++)d[i]=0;
for(i=len-1;i>=0;i--)
{
j=(len-i-1)/4+1;
k=(len-i-1)%4;
d[j]+=ten[k]*(s[i]-'0');
}
while(d[0]>1&&d[d[0]]==0)--d[0];
}
bignum()
{
*this=bignum(string("0"));
}
string tostring()
{
string s("");
int i,j,temp;
for(i=3;i>=1;i--)if(d[d[0]]>=ten[i])break;
temp=d[d[0]];
for(j=i;j>=0;j--)
{
s=s+(char)(temp/ten[j]+'0');
temp%=ten[j];
}
for(i=d[0]-1;i>0;i--)
{
temp=d[i];
for(j=3;j>=0;--j)
{
s=s+(char)(temp/ten[j]+'0');
temp%=ten[j];
}
}
return s;
}
}zero("0"),d,temp,mid1[20];
bool operator < (const bignum &a ,const bignum &b)
{
if(a.d[0]!=b.d[0])return a.d[0]<b.d[0];
int i;
for(i=a.d[0];i>0;i--)if(a.d[i]!=b.d[i])return a.d[i]<b.d[i];
return 0;
}
bignum operator - (const bignum &a ,const bignum &b)
{
bignum c;
c.d[0]=a.d[0];
int i,x=0;
for(i=1;i<=c.d[0];i++)
{
x=10000+a.d[i]-b.d[i]+x;
c.d[i]=x%10000;
x=x/10000-1;
}
while((c.d[0]>1)&&(c.d[c.d[0]]==0))--c.d[0];
return c;
}
bignum operator *(const bignum &a,const bignum &b)
{
bignum c;
c.d[0]=a.d[0]+b.d[0];
int i,j,x;
for(i=1;i<=a.d[0];i++)
{
x=0;
for(j=1;j<=b.d[0];j++)
{
x=a.d[i]*b.d[j]+x+c.d[i+j-1];
c.d[i+j-1]=x%10000;
x/=10000;
}
c.d[i+b.d[0]]=x;
}
while((c.d[0]>1)&&(c.d[c.d[0]]==0))--c.d[0];
return c;
}
string getstring(int a)
{
string ss;
while(a!=0)
{
ss= char('0'+(a%10))+ss;
a/=10;
}
return ss;
}
int main()
{ int aa,bb;
cin>>aa>>bb;
if(aa==bb)
{
cout<<0<<endl;
return 0;
}
string ta=getstring(aa),tb=getstring(bb);
bignum a(ta); bignum b(tb);
// cout<<a.tostring();
// cout<<b.tostring()<<endl;
bignum ans1("1"),ans2("1"); for(int i=0;i<bb;i++)
{
ans1=ans1*a;
}
// cout<<ans1.tostring()<<endl;
for(int i=0;i<aa;i++)
{
ans2=ans2*b;
}
// cout<<ans2.tostring()<<endl;
if(ans2<ans1)
{
bignum ans=ans1-ans2;
cout<<ans.tostring()<<endl;
}
else
{
bignum ans=ans2-ans1;
cout<<"-"<<ans.tostring()<<endl;
}
return 0;
}

SGU112的更多相关文章

  1. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

  2. 今日SGU 5.2

    SGU123 题意:求和 收获:无 #include<bits/stdc++.h> #define de(x) cout<<#x<<"="< ...

随机推荐

  1. HTML5 Geolocation(地理位置)

    HTML5 Geolocation(地理位置).是用来定位用户的位置的. HTML5 Geolocation API 用于获得用户的地理位置,鉴于该特性可能侵犯用户的隐私权,除非用户同意,否则不能获取 ...

  2. 【译】Swift 字符串速查表

    [译]Swift 字符串速查表 2015-12-18 10:32 编辑: suiling 分类:Swift 来源:CocoaChina翻译活动 10 5585 Swift字符串 招聘信息: iOS高级 ...

  3. Vuex基本概念

    Vuex基本概念 State Getter Mutation Action Module 简单的Store import Vue from 'vue'; import Vuex from 'vuex' ...

  4. touch-action css属性 滚动和缩放手势

    CSS 属性 touch-action 用于指定某个给定的区域是否允许用户操作,以及如何响应用户操作(比如浏览器自带的划动,缩放等) 默认情况下,平移(滚动) 和 缩放手势由浏览器专门处理.该属性用于 ...

  5. MYSQL中批量替换某个字段的部分数据

    1.修改字段里的所有含有指定字符串的文字 UPDATE 表A SET 字段B = replace(字段B, 'aaa', 'bbb') example: update  table set url= ...

  6. mysql函数总结

    MySQL函数 MySQL数据库提供了很多函数包括: 数学函数:字符串函数:日期和时间函数:条件判断函数:系统信息函数:加密函数:格式化函数: 一.数学函数 数学函数主要用于处理数字,包括整型.浮点数 ...

  7. python--subprocess,粘包现象与解决办法,缓冲区

    一. subprocess 的简单用法 import subprocess sub_obj = subprocess.Popen( 'dir', #系统指令 shell=True, #固定方法 std ...

  8. selenium.common.exceptions.WebDriverException: Message: u'unknown error: cannot get automation extension\nfrom unknown error: page could not be found: chrome-extension://aapnijgdinlhnhlmodcfapnahmbfeb

    Python2.7 selenium3.4.1在使用chrome driver时报错:selenium.common.exceptions.WebDriverException: Message: u ...

  9. LeetCode(1)Two Sum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  10. 00049_super关键字

    1.子父类中构造方法的调用 (1)在创建子类对象时,父类的构造方法会先执行,因为子类中所有构造方法的第一行有默认的隐式super();语句: (2)格式 调用本类中的构造方法 this(实参列表); ...