nyoj 次方求模
http://acm.nyist.net/JudgeOnline/problem.php?pid=102
View Code
1 #include<stdio.h> 2 int main() 3 { 4 long long a,b,n; 5 int t; 6 scanf("%d",&t); 7 while(t--) 8 { 9 long long t=1; 10 scanf("%lld%lld%lld",&a,&b,&n); 11 for(;b;b>>=1,a=a*a%n) 12 { 13 if(b%2) t=t*a%n; 14 } 15 printf("%lld\n",t); 16 } 17 }
标程
View Code
1 2 #include<iostream> 3 using namespace std; 4 template<class IntType> 5 inline IntType ModPow(IntType m,IntType n,IntType p) //m的n次方模p 6 { 7 if (n==1) return m%p; 8 IntType tmp=ModPow(m,n>>1,p); 9 return (tmp*tmp%p)*((n%2?m:1)%p)%p; 10 } 11 int main() 12 { 13 int n; 14 long long a,b,c; 15 cin>>n; 16 while(n--) 17 { 18 cin>>a>>b>>c; 19 cout<<ModPow(a,b,c)<<endl; 20 } 21 }