A^X mod P
Time Limit: 5000MS Memory limit: 65536K
题目描述
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.
输入
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
输出
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.
示例输入
23 2 1 1 1 100 1003 15 123 2 3 1000 107
示例输出
Case #1: 14Case #2: 63
提示
来源
2013年山东省第四届ACM大学生程序设计竞赛
示例程序
按照题目描述做即可
要打出a的指数表但是直接打会超内存
依据题意知道最大求a的1e9次方
然后sqrt(1e9)=3k多
所以我们打两个表一个表存4000个两个表相乘就能达到1e9
ACcode:
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define mm 40000
#define ll long long
using namespace std;
ll n,A,K,a,b,m,P,ans;
int t,loop=1;
ll powa[mm+10],powb[mm+10];
void init(){
powa[0]=powb[0]=1;
powa[1]=A;
for(int i=2;i<=mm;++i)
powa[i]=(powa[i-1]*A)%P;
powb[1]=powa[mm];
for(int i=1;i<=mm;++i)
powb[i]=(powb[i-1]*powb[1])%P;
}
int main(){
//cout<<(1e9/mm)<<'\12';
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&n,&A,&K,&a,&b,&m,&P);
init();
ans=0;
for(int i=1;i<=n;++i){
ans=(ans+powa[K%mm]*powb[K/mm])%P;
K=(a*K+b)%m;
}
printf("Case #%d: %lld\n", loop++, ans);
}
return 0;
}
/*
2
3 2 1 1 1 100 100
3 15 123 2 3 1000 107
*/
/**************************************
Problem id : SDUT OJ 2605
User name : acmer
Result : Accepted
Take Memory : 1152K
Take Time : 800MS
Submit Time : 2016-04-09 11:55:06
**************************************/