3001. 计算a的n次方的大整数

Li Dao

高精度乘法,我简单封装了一下

include

using namespace std;
typedef vector BIG;

int T,n;

BIG create(int aa)
{
BIG ret;
ret.push_back(aa);
while(ret[ret.size()-1]>=10)
{
int Top=ret.size()-1;
ret.push_back(ret[Top]/10);
ret[Top]%=10;
}
return ret;
}

BIG operator(const BIG& aa,int bb)
{
BIG ret;
ret.assign(aa.begin(),aa.end());
for(int i=0;i=10) { int Top=ret.size()-1; ret.push_back(ret[Top]/10); ret[Top]%=10; } while(ret[ret.size()-1]==0) ret.pop_back(); return ret; } void print(const BIG& aa) { for(int i=aa.size()-1;i>=0;i–) cout<>a>>b;
if(b==0) cout<<1<<endl;
else
{
BIG tmp=create(a);
for(int i=1;i<b;i++) tmp=tmp
a;
print(tmp);
cout<>T;
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

10165101251

python来凑
a=eval(input())
c=0
while a>0:
b=input().split()
print(‘{}{}{}’.format(‘case #’,c,’:’))
c=c+1
print(int(b[0])**int(b[1]))
a=a-1

10205101536

本菜鸟的另一种垃圾算法
先将每一位进行无进位乘法,再处理每一位的进位

include

int cnt = 0;
void mulByArr(int nums[], int val){
int temp;
for(int k = cnt;k>=0;k–){
nums[k] *=val;//无进位乘
}
if(cnt>=1){ //对后n-1位进行进位
for(int i = cnt;i>=1;i–){
if(nums[i]>=10){
temp = nums[i]/10;
nums[i]%=10;
nums[i-1]+=temp;
}
}
}
if(nums[0]>=10){//判断首位是否十进制溢出,如果是则把后n-1位统一向后移一位,然后处理首位溢出
for(int j = cnt;j>=1;j–){
nums[j+1] = nums[j];
}
nums[1] = nums[0]%10;
nums[0] = nums[0]/10;
cnt++;
}
}

int main(){
int T;
scanf(“%d”,&T);
for(int i =0;i<T;i++){
int n ,m ;
scanf(“%d %d”,&n,&m);
int nums[100000] = {1};
cnt = 0;
for(int i =0;i<m;i++){
mulByArr(nums,n);
}

printf("case #%d:\n",i);
for(int i=0;i<=cnt;i++){
    printf("%d",nums[i]);
}       
printf("\n");   
}

}

aiden
#include <iostream>
using namespace std;

const int len = 1000;

int main()
{
    int t;
    cin >> t;
    for (auto z = 0; z < t; z++)
    {
        int num[len] = { 0 };
        num[len - 1] = 1;
        int a, n; //a^n
        cin >> a >> n;
        for (auto i = 0; i < n; i++)
        {
            for (auto j = len - 1; j >= 0; j--)
                num[j] *= a;
            for (auto j = len - 1; j >= 0; j--)
                if (num[j] >= 10)
                {
                    num[j - 1] += num[j] / 10;
                    num[j] = num[j] % 10;
                }
        }
        int start;
        for (auto i = 0; i < len; i++)
            if (num[i] == 0 && num[i + 1] != 0)
            {
                start = i + 1;
                break;
            }
        cout << "case #" << z << ":" << endl;
        for (auto i = start; i < len; i++)
            cout << num[i];
        cout.put('\n');
    }
    return 0;
}
10165101122

高精度乘法,我简单封装了一下
楼主写了好长。。膜
C语言版:

include

void solve(int x)
{
int a,b;
scanf(“%d %d”,&a,&b);
int s[100]={0};
s[0]=1;
int i,k,flag=0;
for(i=0;i<b;++i)
{
for(k=0;k<100;++k)
{
s[k]=s[k]*a;
}
for(k=0;k<100;++k)
{ s[k]=s[k]+flag;
flag=0;
if(s[k]>=10)
{
flag=s[k]/10;
s[k]=s[k]%10;
}
else
{
flag=0;
}
}
}

for(i=99;s[i]==0;--i);
printf("case #%d:\n",x);
for(;i>=0;--i)printf("%d",s[i]);
printf("\n");

}

int main()
{
int T,i;
scanf(“%d”,&T);
getchar();
for(i=0;i<T;++i)
{
solve(i);
}
}
[em:01]

smileelop

import java.io.*;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger a;
int b;
// TODO Auto-generated method stub
Scanner cin = new Scanner (System.in);
int t;
t = cin.nextInt();
for (int i = 0;i<t;i++) {
a = cin.nextBigInteger();
b = cin.nextInt();
System.out.println(“case #”+i+”:”);
System.out.println(a.pow(b));
// System.out.println(a.remainder(b));
}
}
}

你当前正在回复 博客/题目
存在问题!