题目: http://poj.org/problem?id=1995

题目解析:求(A1B1+A2B2+ ... +AHBH)mod M.

大水题。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int n,mod,sum;
int main()
{
    int T,a[45010],b[45010];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&mod,&n);
        int sum=0,t;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a[i],&b[i]);
            t=1;
            while(b[i])
            {
                if(b[i]&1)
                    t=(t*a[i])%mod;
                b[i]>>=1;
                a[i]=((a[i]%mod)*(a[i]%mod))%mod;
            }
            sum=(sum+t)%mod;
        }
        printf("%d\n",sum);
    }
    return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.