2 2 4 1 3 7 2
Case #1: 6 Case #2: 87
快速幂模!
AC码:
#include<stdio.h> long long n,p,m; long long Mi(long long mm) { long long a=n,b=p,result=1; while(b) { if(b&1) result=(result*a)%mm; a=(a*a)%mm; b=b/2; } return result%mm; } int main() { long long T,i,mm; int count=1; scanf("%lld",&T); while(T--) { scanf("%lld%lld%lld",&n,&p,&m); mm=1; for(i=1;i<=m;i++) mm*=10; printf("Case #%d: %lld\n",count++,Mi(mm)); } return 0; }
二分法!
AC码:
#include<stdio.h> long long fun(long long n,long long p,long long mm) { long long result; if(p==1) return n; result=fun(n,p/2,mm); if(p%2==1) return (((result*result)%mm)*n)%mm; else return (result*result)%mm; } int main() { int T,i,j; long long n,p,m,mm; scanf("%lld",&T); for(i=1;i<=T;i++) { scanf("%lld%lld%lld",&n,&p,&m); mm=1; for(j=1;j<=m;j++) mm*=10; printf("Case #%d: %lld\n",i,fun(n,p,mm)); } return 0; }
原文地址:http://blog.csdn.net/u012804490/article/details/25738953