Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:快速幂取模学下再做,模板供参考

Posted by cui1716265146 at 2013-05-11 16:46:46 on Problem 1995
In Reply To:快速幂取模学下再做,模板供参考 Posted by:ZJNU09220229 at 2010-11-24 22:00:31
> #include <cstdlib>
> #include <stdio.h>
> using namespace std;
> long f(long a,long k,long M){//求a^k%M
>     long b=1;
>     while(k>=1){
>         if(k%2==1){
>             b=a*b%M;
>         }
>         a=(a%M)*(a%M)%M;
>         k/=2;
>     }
>     return b;
> }
> int main(int argc, char** argv) {
>     int t,n,i,j;
>     long a,b,M;
>     long z,ans;
>     scanf("%d",&t);
>     while(t--)
>     {
>         scanf("%ld",&M);
>         scanf("%d",&n);
>         ans=0;
>         for(i=0;i<n;i++)
>         {
>             scanf("%ld%ld",&a,&b);
>             z=f(a,b,M);
>             ans=(ans+z)%M;
>         }
>         printf("%ld\n",ans);
>     }
>     return 0;
> }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator