Cruel Math Teacher 1

题目链接:https://ac.nowcoder.com/acm/contest/1088/L

Bessie has returned to 8th grade in order to finish her diploma. Her cruel math teacher wants the students to calculate "powers of integers". An integer power is the resultant integer when some number N (1 <= N <= 2,000,000,000) is multiplied by itself over and over P times (1 <= P <= 100,000).

By way of example, 2 to the power 3 = 2 * 2 * 2 (three times) = 8.
Similarly, 123456 to the power 88 = 123456 * 123456 * ... * 123456 (88
times) = 
1129987770413559019467963153621658978635389622595924947762339599136126
3387265547320084192414348663697499847610072677686227073640285420809119
1376617325522768826696494392126983220396307144829544079751988205731569
1498433718478969549886325738202371569900214092289842856905719188890170
0772424218248094640290736200969188059104939824466416330655204270246371
3699112106518584413775333247720509274637795508338904731884172716714194
40898407102819460020873199616
when printed 70 digits per line.

Write a program to calculate the Pth power of an integer N. The answer is guaranteed to be no longer than 15,000 digits. Print your answer 70 digits per line (except the last line which might be shorter). Do not print leading zeroes, of course.

输入描述:

* Line 1: Two space-separated integers: N and P

输出描述:

* Lines 1..?: A single integer that is the result of the calculation. Print 70 digits per line except potentially for the last line, which might be shorter.

示例1

输入

复制

2 15

输出

复制

32768

思路:大数快速幂

#include<bits/stdc++.h>
using namespace std;
const int N=101010;
char z[N];
int b;
struct E{
	int f[N];
	int len;
}a,chu;
E mul(E aa,E bb)
{
	E res=chu;
	int x=0;
	res.len=aa.len+bb.len;
	for(int i=1;i<=aa.len;i++)
		for(int j=1;j<=bb.len;j++)
			res.f[i+j-1]+=aa.f[i]*bb.f[j];
	for(int i=1;i<=res.len;i++)
	{
		res.f[i]+=x;
		x=res.f[i]/10;
		res.f[i]%=10;
	}
	for(int i=res.len;i>=1;i--)
	{
		res.len=i;
		if(res.f[i]) break;
	}
	return res;
}
E ksm()
{
	E res=chu;
	res.len=1;
	res.f[1]=1;
	while(b)
	{
		if(b&1) res=mul(res,a);
		b>>=1;
		a=mul(a,a);	
	}
	return res;	
} 
int main()
{
	scanf("%s",&z);
	scanf("%d",&b);
	a.len=strlen(z);
	for(int i=0;i<a.len;i++) a.f[a.len-i]=z[i]-'0';
	E ans=ksm();
	int cnt=0;
	for(int i=ans.len;i>=1;i--)
	{
		cout<<ans.f[i];
		cnt++;
		if(!(cnt%70)) puts("");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值