标签:
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2605
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.
1 <= n <= 10^6
0 <= A, K, a, b <= 10^9
1 <= m, P <= 10^9
c is the case number start from 1.
ans is the answer of this problem.
2 3 2 1 1 1 100 100 3 15 123 2 3 1000 107
Case #1: 14 Case #2: 63
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<queue> 5 #include<iostream> 6 #include<stack> 7 #include<map> 8 #include<string> 9 using namespace std; 10 char name[1010][25]; 11 int num[110]; 12 char temp[100]; 13 int main(){ 14 int n, i, j, tcase, s, x, y, mod; 15 scanf("%d", &tcase); 16 while(tcase--){ 17 scanf("%d%d%d%d%d", &n, &s, &x, &y, &mod); 18 for(i = 0; i < n; i++){ 19 scanf("%s%s%d%s", name[i], temp, &num[i], temp); 20 } 21 int count = 0; 22 for(i = 0; i < n; i++){ 23 //int t = num[i]; 24 count += num[i]; 25 if(count > s){ 26 printf("%d pages for %s\n", num[i]-(count-s), name[i]); 27 s = (s*x+y)%mod; 28 count = 0; 29 i--; 30 } 31 else{ 32 printf("%d pages for %s\n", num[i], name[i]); 33 } 34 } 35 puts(""); 36 } 37 return 0; 38 }
官方代码:
1 #include<cmath> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 typedef long long ll; 9 int n; 10 ll A,K,a,b,m,P; 11 ll powmod(ll a,ll n,ll m){ 12 ll y=1; 13 while(n){ 14 if(n&1) y=y*a%m; 15 n>>=1; 16 a=a*a%m; 17 } 18 return y; 19 } 20 ll solve(){ 21 ll f=K,sum=0,tmp; 22 for(int i=0;i<n;i++){ 23 sum=(sum + powmod(A,f,P))%P; 24 f=(a*f + b)%m; 25 } 26 return sum; 27 } 28 int main() 29 { 30 #ifndef ONLINE_JUDGE 31 freopen("data.in","r",stdin);freopen("out.txt","w",stdout); 32 #endif 33 int T,C=0; 34 scanf("%d",&T); 35 while(T--) 36 { 37 scanf("%d%I64d%I64d%I64d%I64d%I64d%I64d",&n,&A,&K,&a,&b,&m,&P); 38 printf("Case #%d: %I64d\n",++C,solve()); 39 } 40 return 0; 41 }
标签:
原文地址:http://www.cnblogs.com/jeff-wgc/p/4467581.html