快速幂

来源:互联网 发布:易语言源码图标 编辑:程序博客网 时间:2024/06/05 08:32
/*快速幂*/#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;const int maxn = 10010;int p_cofficient[maxn],q_cofficient[maxn],x_self[maxn];int fast_power(int a,int b){    int res = 1;    int tmp = a;    while(b)    {        if(b&0x1)   res *= tmp;        tmp *= tmp;        b >>= 1;        //printf("%d\n",res);    }    //printf("%d\n",res);    return res;}int main(){    int p,q,x,n,m,ans = 0,i,j,x_index;    while(scanf("%d%d%d%d%d",&p,&q,&x,&n,&m)!=EOF)    {        ans = 0;        for( i = 0; i <= n-1; i++)            {p_cofficient[i] = fast_power(p,n-1-i);            //printf("%d\n",p_cofficient[i]);            }        for( j = 0;j <= m-1; j++)            {q_cofficient[j] = fast_power(q,m-1-j);            //printf("%d\n",q_cofficient[j]);            }        for( i=0; i<=m+n-2; i++)            x_self[i] = fast_power(x,m+n-2-i);        for( x_index=0;x_index<=m+n-2;x_index++){             i = 0;             j = x_index - i;            while(j > m-1)            {                j--;                i++;            }            while(j>=0){                ans += p_cofficient[i]*q_cofficient[j]*x_self[x_index];                j--;                i++;            }        }        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击