第四届山东省赛 A^X mod P [预处理]【思维】
来源:互联网 发布:砸炮纸淘宝能买到吗 编辑:程序博客网 时间:2024/05/12 14:47
题目链接:http://acm.upc.edu.cn/problem.PHP?id=2219
————————————————————————————————————
2219: A^X mod P
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 142 Solved: 28
[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
————————————————————————————————————
题目大意:
就是给你7个数:
一个公式:
问:
解题思路:
其实思路很好构建 ,
只要求出
但是这题卡了
对于求一次幂 用快速幂会非常快 但是求多次就不是很快了
我们要先预处理出所有
发现根本存不下 ,
但是我们可以预处理出
和
这样我们就可以通过一次相乘,开快速求出
这样下来复杂度就变成了
附本题代码
——————————————————————————————————————————————————————————————
#include<bits/stdc++.h>using namespace std;typedef long long int LL;/********************************/LL Ai[100005];LL A1e5i[100005];int main(){ int _ = 1,kcase = 0; scanf("%d",&_); while(_--){ int n, A, K, a, b, m, P; scanf("%d%d%d%d%d%d%d",&n, &A, &K, &a, &b, &m, &P); Ai[0]=1; Ai[1]=A; for(int i=1;i<=100000;i++){ Ai[i]=Ai[i-1]*A%P; } A1e5i[0]=1; A1e5i[1]=Ai[100000]; for(int i=2;i<=100000;i++){ A1e5i[i]=A1e5i[1]*A1e5i[i-1]%P; } LL ans =0,tmp=K; for(int i=1;i<=n;i++){ ans+=Ai[tmp%100000]*A1e5i[tmp/100000]; (ans>P)?ans%=P:true; tmp=tmp*a+b,tmp%=m; } printf("Case #%d: ",++kcase); printf("%lld\n",ans); } return 0;}
- 第四届山东省赛 A^X mod P [预处理]【思维】
- 山东省第四届ACM C :A^X mod P
- 2013年山东省第四届ACM大学生程序设计竞赛——A^X mod P
- 山东省第四届ACM大学生程序设计竞赛 A^X mod P
- 第四届 山东省ACM A^X mod P (分解优化=哈希+打表)
- 2013年山东省第四届ACM大学生程序设计竞赛 A^X mod P
- A^X mod P(山东省第四届ACM大学生程序设计竞赛 )
- 第四届acm A^X mod P
- A^X mod P 山东省赛,打表求解
- sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)
- A^X mod P (2013山东省赛)(用拆分思想求解重复问题)
- UPC:2219 A^X 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
- TCP和UDP协议
- Win64 驱动内核编程-12.回调监控进线程创建和退出
- 数组的reduce方法和redux
- javax.servlet.http.HttpServlet was not found on the Java
- django 快速搭建blog
- 第四届山东省赛 A^X mod P [预处理]【思维】
- 交换两个变量的值
- Web+spring容器的生命周期与 各种 Listener
- HTTP 协议格式 和 HTTP Header
- C语言C++集成编辑环境
- 每日十道面试题(五)
- Android6.0动态加载权限
- 删数问题
- Spring+基于AXIS2 的 werservice