一、总结
我一开始担心溢出,开了一个无符号的long long,但是直接超时,后来一看它的mod不是很大,于是改成int,直接过了。
二、代码
#include <iostream>
using namespace std;
int H, Z;
int M;
int mulMod(int a, int b, int mod)
{int res = 0;while (b){if (b & 1){res = (res + a) % mod;}a = (a << 1) % mod;b = b >> 1;}return res;
}
int powMod(int a, int b, int mod)
{int res = 1;while (b){if (b & 1){res = mulMod(res, a, mod);}a = mulMod(a, a, mod);b = b >> 1;}return res;
}
void solve()
{scanf("%d", &M);scanf("%d", &H);int a = 0, b = 0;int res = 0;for (int i = 0; i < H; i++){scanf("%d%d", &a, &b);res = (res + powMod(a, b, M)) % M;}printf("%d\n", res);
}
int main()
{int Z = 0;scanf("%d", &Z);for (int i = 0; i < Z; i++){solve();}return 0;
}
三、运行情况