第四届 山东省ACM A^X mod P (分解优化=哈希+打表)
来源:互联网 发布:杭州西湖软件测试 编辑:程序博客网 时间:2024/05/23 20:44
A^X mod P
Time Limit: 5000MS Memory Limit: 65536KB
Submit Statistic
Problem 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.
Example Input
23 2 1 1 1 100 1003 15 123 2 3 1000 107
Example Output
Case #1: 14Case #2: 63
Hint
Author
2013年山东省第四届ACM大学生程序设计竞赛
题意:
题目意思很简单,就是求(A^f[1]+A^f[2]+。。。+A^f[n])%P
题解:
一开始直接扫描一遍结果无情TL,用快速幂计算幂值有很多重复的计算,因此想办法将结果保存在数组里面,dp的思想。显然f[i]=fix*k+j,这样分解是对的,那么选取一个合适的fix,这样数组可以存下需要的解。不妨令fix=31623
,那么A^(fix*k+j)%m,就可以分解成(A^k)^fix*A^j,用dpk[i]保存(A^k)^i,dpj[i]保存A^i,那么
A^f[i]=dpk[i/fix]*dpj[i%fix];
#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
- 第四届 山东省ACM A^X mod P (分解优化=哈希+打表)
- sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)
- 山东省第四届ACM C :A^X mod P
- 第四届acm A^X mod P
- 2013年山东省第四届ACM大学生程序设计竞赛——A^X mod P
- 山东省第四届ACM大学生程序设计竞赛 A^X mod P
- 2013年山东省第四届ACM大学生程序设计竞赛 A^X mod P
- A^X mod P(山东省第四届ACM大学生程序设计竞赛 )
- A^X mod P 山东省赛,打表求解
- 第四届山东省赛 A^X mod P [预处理]【思维】
- A^X mod P 大幂分解求和 打表
- sdut 2605 A^X mod P (分解优化)
- SDUT 2605-A^X mod P(大幂分解求和)
- sduT 2605-A^X mod P(大幂分解求和)
- A^X mod P
- A^X mod P
- A^X mod P
- sdut2605山东省赛第四届/( A^f(1)+A^f(2)+ ... + A^f(n) )% P/存储中间值/将大数打表并用两个数组组合表示
- 注销 重启 关机 定时
- Java代码实现数组中插入数据
- 远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)
- 毕业设计开发过程遇到的问题与解决方案记录
- 虚函数
- 第四届 山东省ACM A^X mod P (分解优化=哈希+打表)
- POJ2231奶牛叫
- Servlet生命周期与工作原理
- Puma560 机器人DH变换
- angular.js $parse的用法demo
- 怎么让MacBook从U盘启动
- 做企业培训要注意的那些问题
- 用链表写的一个队列
- 初入mybatis的几个坑