UPC2219: A^X mod P

网友投稿 249 2022-09-04

UPC2219: A^X mod P

2219: A^X mod P

Time Limit: 5 Sec   Memory Limit: 128 MB

Submit: 443

Solved: 80

[

​​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

23 2 1 1 1 100 1003 15 123 2 3 1000 107

Sample Output

Case #1: 14Case #2: 63

HINT

Source

#include#include#include#includeusing namespace std;typedef long long LL;const int maxn = 1000005;const int N=100000;LL f[maxn];LL bx[N+10];LL ax[N+10];int main(){ int T,kase=0; cin>>T; while(T--) { LL n,A,K,a,b,m,P; cin>>n>>A>>K>>a>>b>>m>>P; A%=P; f[1]=K; for(int i=2;i<=n;i++) f[i]=(a*f[i-1]+b)%m; bx[0]=1%P; for(int i=1;i<=N;i++) bx[i] = bx[i-1]*A%P; LL Ax=1%P; for(int i=1;i<=N;i++) Ax=(Ax*A)%P; ax[0]=1%P; for(int i=1;i<=N;i++) ax[i]=ax[i-1]*Ax%P; LL ans = 0; for(int i=1;i <=n;i++) { int x = f[i]/N; int y=f[i]%N; LL t = ax[x]*bx[y]%P; ans = (ans+t)%P; } cout<<"Case #"<<++kase<<": "<

我只能说根本看不懂,明天再看2遍吧

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:realloc
下一篇:全媒派:移动优先、用户至上:地市级党报如何挺过转型阵痛期?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~