码迷,mamicode.com
首页 > 其他好文 > 详细

sdutoj 2605 A^X mod P

时间:2015-04-29 23:17:39      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2605

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.

示例输入

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

示例输出

Case #1: 14
Case #2: 63

提示

 

来源

2013年山东省第四届ACM大学生程序设计竞赛
 
 
分析:
 
直接计算即可。
 
 
AC代码:
 
技术分享
 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 }
View Code

 

官方代码:

 

技术分享
 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 }
View Code

 

 

sdutoj 2605 A^X mod P

标签:

原文地址:http://www.cnblogs.com/jeff-wgc/p/4467581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!