题目一(1004题)
Time Limit: 1000 MS Memory Limit: 32768 KB
Total Submission(s): 262 Accepted Submission(s): 42
Description
There are many problems about a + b, however, there is no problem about a - b.
Now our friend Albert_s gives you a problem about a - b, to increase the interest of the question, he needs you calculate .
Input
The input will consist of a series of pairs of integers and , separated by a space, one pair of integers per line.
Output
For each pair of input integers and you should output .
Sample Input
2 1
Sample Output
1
题意
求a^b - b^a,
注意
本题数十分巨大,要用大整数。我本来用的快速幂然后取余,但是没给和几取余,所以不对。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct bign{
int d[300];
int len;
bign(){
memset(d,0,sizeof(d));
len=0;
}
};
bign change(char str[]){
bign a;
a.len=strlen(str);
for(int i=0;i<a.len;i++){
a.d[i]=str[a.len-i-1]-'0';
}
return a;
}
int compare(bign a,bign b){
if(a.len>b.len) return 1;
else if(a.len<b.len) return -1;
else{
for(int i=a.len-1;i>=0;i--){
if(a.d[i]>b.d[i]) return 1;
else if(a.d[i]<b.d[i]) return -1;
}
return 0;
}
}
bign chengfang(bign a,int b){
bign c;
int carry=0;
for(int i=0;i<a.len;i++){
int t=a.d[i]*b+carry;
c.d[c.len++]=t%10;
carry=t/10;
}
while(carry!=0){
c.d[c.len++]=carry%10;
carry/=10;
}
return c;
}
bign sub(bign a,bign b){
bign c;
bign t;
int pan=1;
if(compare(a,b)==-1){
pan=0;
t=a;
a=b;
b=t;
}
for(int i=0;i<a.len||i<b.len;i++){
if(a.d[i]<b.d[i]){
a.d[i+1]--;
a.d[i]+=10;
}
c.d[c.len++]=a.d[i]-b.d[i];
}
while(c.len-1>=1&&c.d[c.len-1]==0){
c.len--;
}
if(pan==0)
{
c.d[c.len-1]=-c.d[c.len-1];
}
return c;
}
int main()
{
char x[3],y[3];
while(cin>>x>>y)
{
int a,b;
if(strlen(x)==3)
{
a=100;
}
else if(strlen(x)==2)
{
a=(x[0]-'0')*10+(x[1]-'0');
}
else
{
a=x[0]-'0';
}
if(strlen(y)==3)
{
b=100;
}
else if(strlen(y)==2)
{
b=(y[0]-'0')*10+(y[1]-'0');
}
else
{
b=y[0]-'0';
}
bign a1,b1;
a1=change(x);
b1=change(y);
for(int i=0;i<b-1;i++)
{
a1=chengfang(a1,a);
}
for(int i=0;i<a-1;i++)
{
b1=chengfang(b1,b);
}
a1=sub(a1,b1);
for(int i=a1.len-1;i>=0;i--)
{
cout<<a1.d[i];
}
cout<<endl;
}
return 0;
}