2219: A^X mod P
Time Limit: 5 Sec Memory Limit: 128 MB Submit: 417 Solved: 68 [Submit][Status][Web Board]
Description
It's easy for ACMer to calculate A^X mod P. Now given seven integers n, A, K, a, b, m, P, and a function f(x) which defined as following.
f(x) = K, x = 1
f(x) = (a*f(x-1) + b)%m , x > 1
Now, Your task is to calculate
( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P.
Input
In the first line there is an integer T (1 < T <= 40), which indicates the number of test cases, and then T test cases follow. A test case contains seven integers n, A, K, a, b, m, P in one line.
1 <= n <= 10^6
0 <= A, K, a, b <= 10^9
1 <= m, P <= 10^9
Output
For each case, the output format is “Case #c: ans”.
c is the case number start from 1.
ans is the answer of this problem.
Sample Input
2
3 2 1 1 1 100 100
3 15 123 2 3 1000 107
Sample Output
Case #1: 14
Case #2: 63
HINT
Source
#include<stdio.h>
typedef long long ll ;
int T ;
int n, A, K, a, b, m, P ;
int small [ << | ] ;
int big [ << | ] ;
int ans ; void solve ()
{
int ret = ;
small[] = % P ;
for (int i = ; i < ( << | ) ; i++) {
small [i] = (ll) small[i - ] * A % P ;
}
big[] = % P ;
for (int i = ; i < ( << ) ; i++) {
big[i] = (ll) big[i - ] * small [ << ] % P ;
}
while (n --) {
ret += (ll) small [K & ( << ) - ] * big [K >> ] % P ;
if (ret >= P) ret -= P ;
K = ((ll) a * K + b) % m ;
}
printf ("Case #%d: %d\n" , ++ans , ret ) ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin );
scanf ("%d" , &T) ;
ans = ;
while (T--) {
scanf ("%d%d%d%d%d%d%d" , &n , &A , &K , &a , &b , &m , &P) ;
solve () ;
}
return ;
}
在求A^X 幂时,快速幂求的话,是O(10^6*log(n)*40) = O(10^9) 肯定会超时,
我们将X转化成 x = i*s + j。
举例来说:
100200= 100000 + 200 ; 如果我们要求A^100200 可以 转换成求 (A^100000 ) * (A^200).
所以我们只需要将 小的数 && 大的数 分别打表存在small[] , big[]中即可。
铭神给的代码里是用二进制表示的。题目里的数据是1 ~ 10^9。所以最大不会超过1 << 30 (10亿7千多万)
所以任何一个f(x) = j + ((1 << 15 ) * i ) 来表示
big[] : A^1 , A^2 , A^ 3 , A^ 4 …… A^s (用s表示 1 << 15)
small[] : A^(s * 1) , A^(s * 2) ,A^( s * 3) ,A^( s * 4) …… A^(s * s)
这样O(1)复杂度内就能找到 A^f(x)
这样每次求A^x,便可以通过这两个数组在O(1)的时间复杂度内求出来,这样时间复杂度就变成了O(10^6*40) = O(4*10^7)了