1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| # include<bits/stdc++.h>
using namespace std; typedef long long int LL; /********************************/
LL Ai[100005]; LL A1e5i[100005];
int main(){ int _ = 1,kcase = 0; scanf("%d",&_); while(_--){ int n, A, K, a, b, m, P; scanf("%d%d%d%d%d%d%d",&n, &A, &K, &a, &b, &m, &P); Ai[0]=1; Ai[1]=A; for(int i=1;i<=100000;i++){ Ai[i]=Ai[i-1]*A%P; } A1e5i[0]=1; A1e5i[1]=Ai[100000]; for(int i=2;i<=100000;i++){ A1e5i[i]=A1e5i[1]*A1e5i[i-1]%P; } LL ans =0,tmp=K; for(int i=1;i<=n;i++){ ans+=Ai[tmp%100000]*A1e5i[tmp/100000]; (ans>P)?ans%=P:true; tmp=tmp*a+b,tmp%=m; }
printf("Case #%d: ",++kcase); printf("%lld\n",ans);
} return 0; }
|