2219: A^X mod P

Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 417  Solved: 68 [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

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

Sample Output

Case #1: 14
Case #2: 63

HINT

 

Source

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

 #include<stdio.h>
typedef long long ll ;
int T ;
int n, A, K, a, b, m, P ;
int small [ << | ] ;
int big [ << | ] ;
int ans ; void solve ()
{
int ret = ;
small[] = % P ;
for (int i = ; i < ( << | ) ; i++) {
small [i] = (ll) small[i - ] * A % P ;
}
big[] = % P ;
for (int i = ; i < ( << ) ; i++) {
big[i] = (ll) big[i - ] * small [ << ] % P ;
}
while (n --) {
ret += (ll) small [K & ( << ) - ] * big [K >> ] % P ;
if (ret >= P) ret -= P ;
K = ((ll) a * K + b) % m ;
}
printf ("Case #%d: %d\n" , ++ans , ret ) ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin );
scanf ("%d" , &T) ;
ans = ;
while (T--) {
scanf ("%d%d%d%d%d%d%d" , &n , &A , &K , &a , &b , &m , &P) ;
solve () ;
}
return ;
}

在求A^X 幂时,快速幂求的话,是O(10^6*log(n)*40) = O(10^9) 肯定会超时,

我们将X转化成 x = i*s + j。

举例来说:

100200= 100000 + 200 ; 如果我们要求A^100200 可以 转换成求 (A^100000 ) * (A^200).

所以我们只需要将   小的数 && 大的数   分别打表存在small[] , big[]中即可。

铭神给的代码里是用二进制表示的。题目里的数据是1 ~ 10^9。所以最大不会超过1 << 30 (10亿7千多万)

所以任何一个f(x) = j         +        ((1 << 15 ) * i )       来表示

big[]     :       A^1 ,     A^2 ,     A^ 3  ,     A^ 4 …… A^s  (用s表示 1 << 15)

small[]  : A^(s * 1) , A^(s * 2) ,A^( s * 3)  ,A^( s * 4) …… A^(s * s)

这样O(1)复杂度内就能找到 A^f(x)

这样每次求A^x,便可以通过这两个数组在O(1)的时间复杂度内求出来,这样时间复杂度就变成了O(10^6*40) = O(4*10^7)了

 附代码:

upc.2219: A^X mod P(打表 && 超越快速幂(in some ways))的更多相关文章

  1. HDU-6030 Happy Necklace 打表+矩阵快速幂

    Happy Necklace 前天个人赛规律都找出来了,n的范围是\(10^{18}\),我一想GG,肯定是矩阵快速幂,然后就放弃了. 昨天学了一下矩阵快速幂. 题意 现在小Q要为他的女朋友一个有n个 ...

  2. UPC 2219: A^X mod P

    题形:另类快速幂 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 Now, Your task is to calculate ( A^( ...

  3. [原]sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)

    本文出自:http://blog.csdn.net/svitter 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 求出( A^(f(1) ...

  4. What day is that day?(快速幂,打表找周期,或者求通项公式)

    有些题怎么都解不出来,这时候可以打表,找规律,求通项公式等,这些方法让人拍手叫绝,真不错…… Description It's Saturday today, what day is it after ...

  5. HDU4887_Endless Punishment_BSGS+矩阵快速幂+哈希表

    2014多校第一题,当时几百个人交没人过,我也暴力交了几发,果然不行. 比完了去学习了BSGS才懂! 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4887 ...

  6. ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies (打表找规律+快速幂)

    题目链接:https://nanti.jisuanke.com/t/31716 题目大意:有n个孩子和n个糖果,现在让n个孩子排成一列,一个一个发糖果,每个孩子随机挑选x个糖果给他,x>=1,直 ...

  7. Nowcoder 练习赛 17 C 操作数 ( k次前缀和、矩阵快速幂打表找规律、组合数 )

    题目链接 题意 :  给定长度为n的数组a,定义一次操作为: 1. 算出长度为n的数组s,使得si= (a[1] + a[2] + ... + a[i]) mod 1,000,000,007: 2. ...

  8. FZU 1752 A^B mod C(快速加、快速幂)

    题目链接: 传送门 A^B mod C Time Limit: 1000MS     Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了 ...

  9. 2^x mod n = 1(欧拉定理,欧拉函数,快速幂乘)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. AutoLayout 约束与frame、bounds的设置

    介绍 关于AutoLayout的介绍可参考: 使用解读: https://segmentfault.com/a/1190000004386278 iOS 屏幕适配,autoResizing autoL ...

  2. 百度地图ip定位,不算bug的bug

    做为一个入行不足两年的菜鸟,能在博客园写下第一篇博客,是需要多大的勇气啊.主要还是怕大神们喷啊.其次自己文笔实在太差了. 哈哈~还请各位大神,口下留情啊. 首先说下我的需求:一个需要城市分站的手机站. ...

  3. JavaScript并非“按值传递”

    置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<拥Bootstrap入怀--模态框(modal)篇> 作者主页:myvin 博主QQ:85139910 ...

  4. 成都普华永道税务开发的offer

    首先这是一个.net税务开发的offer,我是做开发的. 有没有人在成都普华永道的,最近收到普华永道的offer,如果有的话请联系我.想知道里面的情况.最想知道里面的加班情况,薪资还是有点诱惑的.毕竟 ...

  5. spring cloud教程之使用spring boot创建一个应用

    <7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...

  6. JqueryEasyUI教程入门篇

    什么是jQueryEasyUI? JqueryUI是一组基于jQuery的UI插件集合 学习jQueryEasyUI的条件? 必须掌握Jquery的基本语法知识 jQueryEasyUI的特点? 1. ...

  7. Object C学习笔记23-继承,重写,重载

    前面的学习都一直在使用Object C对象,但是没有具体总结过Object C中的对象使用特性,这里简单总结一下. 一.  继承 在面向对象编程中,子类可以通过继承得到父类的可以继承的的属性和方法,在 ...

  8. [USACO2002][poj1944]Fiber Communications(枚举)

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3804   Accepted: 1 ...

  9. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

  10. [转载]NSString中判断中文,英文,数字

    曾有需求做个用户名中非法字符的判断,要求是只能输入中英文和数字,其他字符一律非法,故写了下边一个程序mark一下吧~~ NSString *testString = @"春1mianBU觉晓 ...