一.题目链接:
A^X mod P
二.题目大意:
给出 T,n, A, K,a, b, m, P.
T 组样例.
求
三.分析:
由于
所以
如果用快速幂求和的话会 TLE.
因为
所以只需要求 sum1[] 和 sum2[].
sum1[i]:
sum2[i]:
所以
详见代码.
四.代码实现:
#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define PI acos(-1.0)
#define ll long long int
using namespace std;
const int M = (int)1e5;
ll n, A, K, a, b, m, p;
ll sum1[M + 5];
ll sum2[M + 5];
void init()
{
sum1[0] = sum2[0] = 1;
for(int i = 1; i <= M; ++i)
sum1[i] = sum1[i - 1] * A % p;
sum2[1] = sum1[M];
for(int i = 1; i <= M / 10; ++i)
sum2[i] = sum2[i - 1] * sum2[1] % p;
}
ll f()
{
ll fx = K;
ll sum = 0;
for(ll i = 1; i <= n; ++i)
{
sum = (sum1[fx % M] * sum2[fx / M] % p + sum) % p;
fx = (a * fx + b) % m;
}
return sum % p;
}
int main()
{
int T;
scanf("%d", &T);
for(int ca = 1; ca <= T; ++ca)
{
cin >> n >> A >> K >> a >> b >> m >> p;
init();
ll ans = f();
printf("Case #%d: %lld\n", ca, ans);
}
return 0;
}