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

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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值