山东省第四届ACM大学生程序设计竞赛 A^X mod P
来源:互联网 发布:自助餐软件 编辑:程序博客网 时间:2024/05/21 09:57
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的指数表但是直接打会超内存
依据题意知道最大求a的1e9次方
然后sqrt(1e9)=3k多
所以我们打两个表一个表存4000个两个表相乘就能达到1e9
ACcode:
#include <map>#include <queue>#include <cmath>#include <cstdio>#include <cstring>#include <stdlib.h>#include <iostream>#include <algorithm>#define mm 40000#define ll long longusing namespace std;ll n,A,K,a,b,m,P,ans;int t,loop=1;ll powa[mm+10],powb[mm+10];void init(){ powa[0]=powb[0]=1; powa[1]=A; for(int i=2;i<=mm;++i) powa[i]=(powa[i-1]*A)%P; powb[1]=powa[mm]; for(int i=1;i<=mm;++i) powb[i]=(powb[i-1]*powb[1])%P;}int main(){ //cout<<(1e9/mm)<<'\12'; scanf("%d",&t); while(t--){ scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&n,&A,&K,&a,&b,&m,&P); init(); ans=0; for(int i=1;i<=n;++i){ ans=(ans+powa[K%mm]*powb[K/mm])%P; K=(a*K+b)%m; } printf("Case #%d: %lld\n", loop++, ans); } return 0;}/*23 2 1 1 1 100 1003 15 123 2 3 1000 107*/ /************************************** Problem id : SDUT OJ 2605 User name : acmer Result : Accepted Take Memory : 1152K Take Time : 800MS Submit Time : 2016-04-09 11:55:06 **************************************/
0 0
- 2013年山东省第四届ACM大学生程序设计竞赛——A^X mod P
- 山东省第四届ACM大学生程序设计竞赛 A^X mod P
- 2013年山东省第四届ACM大学生程序设计竞赛 A^X mod P
- A^X mod P(山东省第四届ACM大学生程序设计竞赛 )
- 山东省第四届ACM C :A^X mod P
- 第四届 山东省ACM大学生程序设计竞赛
- 第四届acm A^X mod P
- 第四届 山东省ACM A^X mod P (分解优化=哈希+打表)
- 2013年山东省第四届ACM大学生程序设计竞赛
- 山东省第四届ACM大学生程序设计竞赛 Rescue The Princess
- 山东省第四届ACM大学生程序设计竞赛 Mountain Subsequences dp
- 山东省第四届ACM大学生程序设计竞赛Mountain Subsequences
- 山东省第四届ACM大学生程序设计竞赛 Alice and Bob
- 山东省第四届ACM大学生程序设计竞赛 Rescue The Princess
- Mountain Subsequences(山东省第四届ACM大学生程序设计竞赛)
- 第四届山东省赛 A^X mod P [预处理]【思维】
- 山东省第七届ACM大学生程序设计竞赛 A Julyed
- 山东省第三届ACM大学生程序设计竞赛 n a^o7 !
- JavaScript/JQuery自执行函数
- 代码中进行RelativeLayout布局的修改添加
- application/x-www-form-urlencoded与multipart/form-data
- 使用PULL解析XML文件
- ios下获得文件的路径
- 山东省第四届ACM大学生程序设计竞赛 A^X mod P
- 2016-4-12杂学
- SpringMVC中添加Velocity的配置
- 【JS】:JS中如何实现对任意区间的取整
- android jni 引用第三方 so
- string类成员函数的使用方法(一)
- 深入理解iOS API系列(一) textField:shouldChangeCharactersInRange:replacementString:
- 连接Oracle数据库的Hibernate配置文件
- Android学习之程序创建桌面快捷方式