sduT 2605-A^X mod P(大幂分解求和)

来源:互联网 发布:剑灵捏脸数据导入图 编辑:程序博客网 时间:2024/06/05 12:05

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.

示例输入

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

示例输出

Case #1: 14Case #2: 63

提示

 

来源

2013年山东省第四届ACM大学生程序设计竞赛


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

思路:第一眼看上去,我糙,赤裸裸的一个快速幂,兴冲冲地去写了发,结果超时了\(^o^)/\(^o^)/\(^o^)/\(^o^)/
结果看了大神的博客之后,秒懂,居然是恶心得打表。
好吧,万恶的打表题,我的确不太会相关的一些应用。
此处每个f[i]表示成如下形式;
f[i]=a*333333+b;
把这两部分打表出来不就ok了嘛;
TMD,当时智障了一下
代码:
#include<stdio.h>#include<string.h>#include <iostream>#include<algorithm>using namespace std;const int maxn=1111111;const int xun=333333;const int mod=2012;typedef long long ll;ll f[maxn];ll db[maxn],dbs[maxn];int main(){    int T,n,A,K,a,b,m,P;    scanf("%d",&T);    for(int t=1;t<=T;t++)    {        cin>>n>>A>>K>>a>>b>>m>>P;        f[1]=K;        db[0]=dbs[0]=1;        for(int i=1;i<=xun;i++)            db[i]=(db[i-1]*A)%P;        dbs[1]=db[xun];        for(int i=2;i<=xun;i++)            dbs[i]=dbs[i-1]*dbs[1]%P;        ll res=dbs[f[1]/xun]*db[f[1]%xun]%P;        res%=P;        for(int i=2;i<=n;i++)        {            f[i]=f[i-1]*a+b;            f[i]%=m;            res+=dbs[f[i]/xun]*db[f[i]%xun]%P;        }        res%=P;        cout<<"Case #"<<t<<": "<<res<<endl;    }    return 0;}



1 0
原创粉丝点击