快速幂入门

来源:互联网 发布:渣到触画画的过程知乎 编辑:程序博客网 时间:2024/05/21 19:22

快速幂入门

描述

题目描述:

快速幂为我们的幂运算提供了一个很快的方法。这道题要求你练习刚刚学会的快速幂。即计算a^n。我们都知道整数能够表示的范围有限,所以这里要求让结果对100000007取模。

输入:

多组输入数据,请处理至文件结尾 每组数据包含两个整数,a与n,输入保证a和n都在32位整数能够表示的范围内,a,n>=0

输出:

a^n对100000007取模的值,每组数据的输出占一行。

样例输入

2 2
9 2
3 3

样例输出

4
81
27


#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<string>#include<cstring>#define MOD 100000007using namespace std;typedef long long ll;ll pow(ll a,ll i){  if (i==0) return 1;  ll temp=pow(a,i>>1);  temp=temp*temp%MOD;  if (i&1) temp=(ll)temp*a%MOD;  return temp%MOD;}int main(){ll a,n;while(~scanf("%lld%lld",&a,&n)){cout<<pow(a,n)<<endl;}return 0;}


原创粉丝点击