#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
LL q_pow(LL a,LL n){
LL ans=1;
while(n){
if(n&1)ans=(ans*a)%mod;n--;
a=a*a%mod;
n/=2;
}
return ans;
}
LL quickmod(LL a,char *b,int len){
LL ans=1;
while(len>0){
if(b[len-1]!='0'){
int s=b[len-1]-'0';
ans=ans*q_pow(a,s)%mod;
}
len--;
a=q_pow(a,10);
}
return ans;
}
int main(){
char s[100050];
int a;
scanf("%d",&a);
scanf("%s",s);
int len=strlen(s);
printf("%I64d",quickmod(a,s,len));
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int mod=1E9+7;
int qmul(int a,int b)
{
int ans=0;
while(b)
{
if(b&1)ans=(ans+a)%mod;
a+=a;
b>>=1;
}
return ans;
}//龟速乘防止快速幂在乘时爆long long
快速幂就是,通过让数半分减少乘的次数,每次遇到奇数乘一次(有些地方会加上b--但除以2之后其实一样),然后每次都除以2,把乘的数再平方以下,这样,每次半分速度相对比较快。
如果碰到数超过long long大数快速幂,需要把从后面往前乘,每次内部快速幂,每次乘完之后a需要变成a的10次快速幂(这里是进一位的表达)记得%mod。
需要注意的第一次编译结果是错误的因为%lld是无法通过编译的,之后改为%I64d,查了下关于%I64d与%lld的区别如下
如果服务器是linux系统,那么定义用long long,IO用%lld
如果服务器是win系统,那么声明要针对编译器而定:
+ 如果用MS系列编译器,声明用__int64 [现在新版的Visual Studio也支持long long了]
+ 如果用MinGW环境,声明用long long
+ 无论什么编译器,IO一律%I64d
下面把各大OJ情况列表如下:
1. TOJ : Linux系统
2. ZOJ : Linux系统
3. POJ : Win系统,语言如选择C/C++,则用MS编译器[支持两种声明],如选择GCC/G++,则为MinGW
4. UVa : Linux系统
5. Ural: Win系统,MS编译器[支持两种声明]
6. SPOJ: Linux系统
7. SGU : Win系统,MS编译器[支持两种声明]
如果有不太清楚的情况可以先看看各OJ上的FAQ,通常会有说明。
另外,为了避免混乱,当数据量不大时,用cin, cout进行输入输出也是一种选择