p87 人见人爱A^B

xiaoxiao2021-02-28  15

将B使用二进制编码即可,每次取最后一位。你懂的,中间是左乘法而不是加法,两个中间变量每次都要取余1000!

#include <stdio.h> using namespace std; int main() { int a,b;//使用a就行了,不需要中间变量 while(scanf("%d %d",&a,&b)!=EOF) { if(a==0&&b==0)break; int ans=1; while(b) { if(b%2==1) { ans*=a;//每次ans是乘以而不是加上a ans%=1000; } b/=2; a*=a;//a每次要平方,a,a^2,a^4... a%=1000; } printf("%d\n",ans); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2632951.html

最新回复(0)