关于对快速幂超时问题的优化

来源:互联网 发布:app下载数据统计 编辑:程序博客网 时间:2024/06/05 21:51

      前天,在做13年山东省赛题目时,遇到一个快速幂取模的题目,当时直接用普通的快速幂做的,结果因为数据太大,TLE了。想了半天也不知道怎么搞,后来是看的别人的解题报告才弄出来,现在拿出来给大家分享一下,也顺便自己复习一下。

题目如下:

Problem C: A^X mod P

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 406  Solved: 66
[Submit][Status][Web Board]

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 is to calculate

( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P. 

Input

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

Output

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.

Sample Input

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

Sample Output

Case #1: 14Case #2: 63



题目大意就是先求出各个f[n]的值,然后用快速幂取模累加求值;

至于求f[n]的值,打表全部求出来就好了。而如果后面一步直接用快速幂取模(时间复杂度是O(logN)),因为。。。好吧,我也没分析出来为什么会超时---目前还是菜鸟一只;

不太会分析复杂度-----反正提交了几次就是超时了----后来一想,打表肯定超不了时。N不过10^6,那肯定是的快速幂有问题了,后来就在网上看到了他的优化方法---分块;

和分治法思想差不多,先把A的M次幂用快速幂求出来,令num = A ^ M,(M的值取一个大一点的数,这里我们取的是50000),然后用一个数组a[i]代表(A^M)^i,a[0] = num;

a[1] = num * a[0]........以此类推。再开一个数组b[i],令b[i] = A ^ i;所以有 A^f[n] ==  A(f[n] / M + f[n] % M) ==  a[f[n] / num] * b[f[n] % M].这样就达到了分块的目的,至于a[i] t与b[i]肯定是要开long long的,而且为了避免数据溢出,把取余运算加到里面就好了。

有关为什么效率会优化的问题现在不是太懂,以后弄懂了回来补充一下-----o(︶︿︶)o 唉,没办法,现在还是小白菜一颗,连快速幂的算法都是从网上粘帖的,不过相信以后

小白菜也会长大的,也希望那些现在正在ACM的 路上艰难前行的小白菜们,你们也要努力,炮灰总有崛起的一天,一起加油!!

具体代码如下所示:有点丑,不要介意~~文章若有问题,欢迎指出,谢谢阅读!

#include <iostream>#include <cstdio>#include <cstring>#define LL long longusing namespace std;LL mem[1000001];int n,A,K,a,b,m,P;LL a5[50001];LL a0[50001];LL ksm(LL a,LL b)//快速幂函数,怕溢出,里面加了取余操作{    LL ans = 1;    while(b)    {        if(b&1)            ans = (ans * a)%P;        a = a * a%P;        b = b >> 1;    }    return ans;}LL digui(LL n)//递归打表,队友写的,但是我觉着没必要这么写,直接用循环就好了,到时候就直接使用数组的值,就不用调用函数了{    if(n==1)    {        return K;    }    if(mem[n]!=-1)    {        return mem[n];    }    else    {        return mem[n]=((a*digui(n-1))%m+b%m)%m;    }}int main(void){    int i,j,t;    LL sum;    scanf("%d",&t);    j=0;    while(t--)    {        memset(mem,-1,sizeof(mem));//数组初始化        sum=0;        scanf("%d%d%d%d%d%d%d",&n,&A,&K,&a,&b,&m,&P);        LL num=ksm(A,50000);        a5[0]=1;//用来存取(A^M)^i        for(i=1; i<=50000; i++)//M取得50000        {            a5[i]= num%P*a5[i-1]%P;//利用了取余的乘法规则,防止溢出        }        a0[0]=1;//用来存取A^i        for(i=1;i<=50000;i++)        {            a0[i]=(A % P * a0[i-1] % P) % P;//防止溢出取余        }        j++;        for(i=1;i<=n;i++)        {            LL temp=digui(i);            sum+=(a5[temp/50000]*a0[temp%50000])%P;//累加        }        cout<<"Case #"<<j<<": "<< sum % P<<endl;    }    return 0;}


好累。。。。但是,加油少年们!



0 0
原创粉丝点击