SDUT 2605-A^X mod P(大幂分解求和)
来源:互联网 发布:新手程序员刚进公司 编辑:程序博客网 时间:2024/06/04 19:19
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大学生程序设计竞赛
题意:求A^X mod P的和。
PS:妥妥的给跪了,用快速幂肯定不行,超时,然后用了一下快速幂+快速乘法还是超时,一直优化优化也优化好,只好去看了下题解,真心给跪了。
思路:这个用到了分解的方法,将A^f中的 f分解为 i * k + j的形式 。保存在数组中,用的时候直接找就好了。
#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,&m,&P); Init(); Solve(icase); }}
0 0
- SDUT 2605-A^X mod P(大幂分解求和)
- sduT 2605-A^X mod P(大幂分解求和)
- A^X mod P 大幂分解求和 打表
- sdut 2605 A^X mod P (分解优化)
- SDUT 2605 A^X mod P
- sdut 2605 预处理A^N mod p
- A^X mod P
- A^X mod P
- A^X mod P
- UPC2219: A^X mod P
- C:A^X mod P
- 1038 X^A Mod P
- 第四届 山东省ACM A^X mod P (分解优化=哈希+打表)
- 第四届acm A^X mod P
- sdut2605——A^X mod P
- 51Nod-1038-X^A Mod P
- hdu 3930 X^N=a(mod) p 求X
- X^2 Mod P
- 按键监听类KeyListener及适配器改良方案
- RPCA以及LRR
- JAVA实现的批量更改文件后缀名的程序
- 数据库调优教程(三)记录慢查询
- JS 立即执行的函数表达式(function)写法
- SDUT 2605-A^X mod P(大幂分解求和)
- Xcode_使用小技巧(内存,快捷键)
- [前端] requireJS—详解
- “互联网+”时代,移动应用安全解决之道
- Android 编译错误:/usr/bin/ld: cannot find -luuid,/usr/bin/ld: cannot find -llzo2
- Android的服务(Service)(三)Service客户端的绑定与跨进程
- UPC2226: Contest Print Server
- OSPF
- 安装Ubuntu时的硬盘分区方案