周三山师对抗赛

题目一(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值