求a的b次方,输出后三位
#include<stdio.h>int main()
{
int n,i,j,m,a,b;
scanf("%d",&n);
for(i=1;i<=n;i++)
{ m=1;
scanf("%d %d",&a,&b);
for(j=1;j<=b;j++)
{ m=m*a;}
if(m<=100)
{
printf("%d\n",m);
}
else
{
printf("%d\n",m%1000);
}
}
return 0;
}
为什么会这样
// 代码最简单 unsigned a_pow_b_mod_1000( unsigned a, unsigned b ) { unsigned result = 1; while( b-- ) result = (result*a)%1000; return result; } // 使用快速幂 unsigned a_pow_b_mod_1000( unsigned a, unsigned b ) { unsigned result = 1; for( unsigned base=a; b; b>>=1, base=(base*base)%1000 ) if( b%2 != 0 ) result = (result*base)%1000; return result; }