Sdut 2605-a^x MoD P (large power decomposition summation)

Source: Internet
Author: User

A^x MoD P Time limit:5000ms Memory limit:65536k have questions? Dot here ^_^ Title 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 was to calculate

(a^ (f (1)) + a^ (f (2)) + a^ (f (3)) + ... + a^ (f (n))) Modular P.

Enter the first line there are an integer T (1 < T <=), 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 format is ' case #c: ans ' for each case.

The case number of C is the 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 2013 ACM University student Program Design competition in Shandong Province

Test instructions: a^x mod P's and.

PS: Due to kneel, with a quick power to definitely not, time out, and then use a quick power + fast multiplication or time-out, has been optimized optimization is also good, had to see the next puzzle, really to kneel.

Idea: This used the Decomposition method, the a^f in the F decomposition into I * k + j form. Save it in an array, just look it up when you use it.

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map>using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const double pi= ACOs ( -1.0); const int maxn=33333; LL X[MAXN+10],Y[MAXN+10];    LL n,a,k,a,b,m,p;void Init () {int i;    X[0]=1;    for (i=1;i<=maxn;i++) {x[i]= (x[i-1]*a)%P;    } LL TMP=X[MAXN];    Y[0]=1;    for (i=1;i<=maxn;i++) {y[i]= (y[i-1]*tmp)%P;    }}void Solve (int icase) {int i;    LL fx=k;    LL res=0;        for (i=1;i<=n;i++) {res= (res+ (Y[FX/MAXN]*X[FX%MAXN])%P)%P;    fx= (a*fx+b)%m; } printf ("Case #%d:%lld\n", icase,res);}    int main () {int t,icase;    scanf ("%d", &t); for (icase=1;icase<=t;icase++) {scanf ("%lld%lld%lld%lld%lld%lld%lld", &n,&a,&k,&a,&b,&am        P;M,&AMP;P);   Init ();     Solve (icase); }}




Sdut 2605-a^x MoD P (large power decomposition summation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.