Exponentiation幂指数

本文介绍了一种解决大数幂运算问题的方法,该方法通过将浮点数拆分为整数部分进行逐位计算来避免数据溢出。适用于计算非常大的数值幂次方,例如R^n形式的表达式。

引用:


Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 67973 Accepted: 15908

Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

Sample Input

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

Hint

If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer
C++

while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work */
{
...
}


分析:

题目要求输入实数R,范围0.0~99.999,有效数字5位。幂指数n,范围0~25,整数.

难点在于乘法计算过程中,float型实数最大只有3.4E +/- 38 (7 digits)。不可能作为结果,数据会溢出。

对策:把乘数、被乘数拆为只有1位的整数,存入分配的整形数组。

 

我的代码(作参考,可实现目标。未测试"计算优化")

 

#include <conio.h>
#include <iostream>

using namespace std;

 

struct R_Input
{
 float R;
 int n;
 struct R_Input* next;
};

//申明函数
int Add(int* R, int num); //*R,num 0-9
void Cal(R_Input* rTail);


int main()
{


 cout<<"R:0.0000<R<99.999 and n:0<n<=25"
  <<"/nInput any character excluding number to quit."<<endl;

 //input data
 float R_In=0;
 int n=0;
 R_Input R_cin;
 R_Input* rTail;
 
 cin>>R_In>>n;
 R_cin.R=R_In;
 R_cin.n=n;
 R_cin.next=NULL;
 rTail=&R_cin;
 
 while(cin>>R_In>>n)
 {
  R_Input* s=new R_Input;
  s->R=R_In;
  s->n=n;
  s->next=NULL;
  rTail->next=s;
  rTail=s;
 }

 //计算
 
 rTail=&R_cin;

 cout<<"Sample Output"<<endl;
 while(rTail)
 {
  Cal(rTail);

  rTail=rTail->next;
 }

 
 _getch();
 return 0;
}


int Add(int* R, int num)
{
 int m,res;
 m=*R;
 res=m+num;
 if(res<10) *R=res;
 else
 {
  *R=res%10;
  Add(R+1,res/10);
  return 1;
 }
 return 0;
}

const int SPACE=256;

void Cal(R_Input* rTail)
{
 int ans[SPACE]={0};
 int lenInt,lenDec;

 //输入实数R

 if(1)
 {
  int k=(int)rTail->R;
  int l=(int)((rTail->R-k)*10000);
  for(int i=1;i<5;ans[i-1]=l%10,l=l/10,i++);
  l=(int)((rTail->R-k)*100000);
  if((l-l/10*10)!=0) Add(ans,1);
  for(int i=4;i<=5;ans[i]=k%10,k=k/10,i++);
 }

 //幂运算

 int multiplier[6];
 multiplier[0]=ans[0];
 multiplier[1]=ans[1];
 multiplier[2]=ans[2];
 multiplier[3]=ans[3];
 multiplier[4]=ans[4];
 multiplier[5]=ans[5];

 int Exp=rTail->n;

 while(Exp>1)
 {
  int An[256];
  for(int i=0;i<256;i++)
  {
   An[i]=ans[i];
   ans[i]=0;   
  }
  int len;
  for(len=SPACE-1;!An[len];len--);
  for(int i=0;i<=len;i++)
   for(int j=0;j<6;j++)
   {
    int A,B,Mi,Mj,Ex10;
    for(Mi=1;Mi<=i;Mi++);
    for(Mj=1;Mj<=j;Mj++);
    //计算A*B
    Ex10=Mi+Mj-2;
    A=An[i];
    B=multiplier[j];
    if(A*B!=0)
     Add(&ans[Ex10],A*B);
    
   }

  Exp--;
 }

 

 char ch[SPACE]={0};

 int where_0;
 for(where_0=SPACE-1;ans[where_0]==0 && where_0>0;where_0--);
 //获取小数、整数部分数位
 lenDec=4*rTail->n;
 lenInt=where_0-lenDec+1;
 
 int prt_n=where_0;
 int chEnd=0;
 for(;lenInt>0;prt_n--,lenInt--)
 {
//  printf("%d",ans[prt_n]);
  sprintf(&ch[chEnd++],"%d",ans[prt_n]);
 }
// printf(".");
 if(lenInt<0)
 {
  sprintf(&ch[chEnd++],"0");
  prt_n=prt_n-lenInt;
 }
 sprintf(&ch[chEnd++],".");

 for(;prt_n>=0;prt_n--)
 {
//  printf("%d",ans[prt_n]);
  sprintf(&ch[chEnd++],"%d",ans[prt_n]);
 }
 
// printf("/n");
 for(chEnd--;chEnd>=0 && ch[chEnd]=='0';chEnd--);
 ch[chEnd+1]='/0';
 cout<<ch<<endl;

}

 

在C++中,有多种表示幂指数的方法: - **使用`pow`函数**:`pow`函数是C++标准库中的指数函数,用于x的y次幂的值。该函数总是以第一个参数为底数,第二个参数为指数,结果从该函数返回,并在调用了该函数的语句中使用。使用前要包含`<cmath>`头文件。示例代码如下: ```cpp #include <iostream> #include <cmath> int main() { double a = 4.0; double b = 2; std::cout << pow(a, b) << std::endl; return 0; } ``` 使用`pow`函数能够方便快捷地计算幂指数,但是由于它是一个通用的浮点数运算函数,对于整数幂运算可能存在精度问题,并且性能相对较低。 - **使用循环实现**:可以使用`for`循环来实现一个数的幂次运算。示例代码如下: ```cpp #include <iostream> int main() { int base = 2; int exponent = 3; int result = 1; for (int i = 0; i < exponent; i++) { result *= base; } std::cout << result << std::endl; return 0; } ``` 使用`for`循环的方式实现幂运算较为直观,易于理解,但当指数较大时,时间复杂度为$O(n)$,效率较低。 - **矩阵快速幂算法**:对于矩阵幂,即对一个矩阵进行连续自乘的操作,可使用矩阵快速幂算法(Matrix Exponentiation)。此算法在图论、动态规划等很多数学和计算问题中都有应用。 - **快速幂算法**:对于大指数幂的计算,可使用快速幂算法,通过移位运算等方式优化计算过程。示例代码如下: ```cpp #include <iostream> long long ksm(long long a, long long k) { long long res = 1; while (k) { if (k & 1) res *= a; k >>= 1; a *= a; } return res; } int main() { long long base = 3; long long exponent = 19; std::cout << ksm(base, exponent) << std::endl; return 0; } ``` 快速幂算法的核心思想是通过将指数二进制分解,利用指数的二进制表示来减少乘法运算的次数,时间复杂度为$O(log n)$,能显著提高计算效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值