upc.2219: A^X mod P(打表 && 超越快速幂(in some ways))
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
#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))的更多相关文章
- HDU-6030 Happy Necklace 打表+矩阵快速幂
Happy Necklace 前天个人赛规律都找出来了,n的范围是\(10^{18}\),我一想GG,肯定是矩阵快速幂,然后就放弃了. 昨天学了一下矩阵快速幂. 题意 现在小Q要为他的女朋友一个有n个 ...
- 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^( ...
- [原]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) ...
- What day is that day?(快速幂,打表找周期,或者求通项公式)
有些题怎么都解不出来,这时候可以打表,找规律,求通项公式等,这些方法让人拍手叫绝,真不错…… Description It's Saturday today, what day is it after ...
- HDU4887_Endless Punishment_BSGS+矩阵快速幂+哈希表
2014多校第一题,当时几百个人交没人过,我也暴力交了几发,果然不行. 比完了去学习了BSGS才懂! 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4887 ...
- ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies (打表找规律+快速幂)
题目链接:https://nanti.jisuanke.com/t/31716 题目大意:有n个孩子和n个糖果,现在让n个孩子排成一列,一个一个发糖果,每个孩子随机挑选x个糖果给他,x>=1,直 ...
- Nowcoder 练习赛 17 C 操作数 ( k次前缀和、矩阵快速幂打表找规律、组合数 )
题目链接 题意 : 给定长度为n的数组a,定义一次操作为: 1. 算出长度为n的数组s,使得si= (a[1] + a[2] + ... + a[i]) mod 1,000,000,007: 2. ...
- FZU 1752 A^B mod C(快速加、快速幂)
题目链接: 传送门 A^B mod C Time Limit: 1000MS Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了 ...
- 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 ...
随机推荐
- 2.SQLAlchemy文档-SQLAlchemy ORM(中文版)
接下来,我们将会对对象关系映射器进行全面的介绍和描述.如果你想用它为你的应用程序构建更高层次的SQL操作模式,以及为你的Python对象提供自动化的持久性功能,那么首先进行下列教程的学习吧. 首先请看 ...
- Python3常用内置函数
数学相关 abs(a) : 求取绝对值.abs(-1) max(list) : 求取list最大值.max([1,2,3]) min(list) : 求取list最小值.min([1,2,3]) su ...
- 第二章 Js函数
函数的定义二种定义 ①function myfunc () { console("hello"); }; ②var myfunc = function () { console ...
- Orchard内置特性(以模块来说的)
本文链接:http://www.cnblogs.com/souther/p/4539169.html 主目录 Orchard中有很多可以直接和多次使用的特性,这些东西在官方的Gallery中可以找到. ...
- WPF中资源引用方式汇总
在WPF应用程序开发中,总是难以记住各种访问资源的方法,遂逐一记下. 先从资源是否编译到程序集分类 一.程序集资源 资源在编译的时候嵌入到程序集中.WPF中的XAML会被编译为BAML,图片等其他资源 ...
- 微信支付PHP SDK —— 公众号支付代码详解
在微信支付 开发者文档页面 下载最新的 php SDK http://mch.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1 这里假设你已经申请完微 ...
- easyUI icon中文命名图片无法在浏览器中访问处理方法
本身的原因是Tomcat没有设置URIencoding,导致无法识别 在Tomcat 文件夹 conf 中 server.xml中加 URIEncoding="utf-8" 可以 ...
- 在CentOS上装 ElasticSearch
参考官方文档:Install Elasticsearch with RPM ElasticSearch依赖Java,所以需要先安装Java: 到Oracle官网找到下载链接 http://www.or ...
- 在Eclipse 中下载 开源中国码云上的项目
功能:使用开源中国代码托管(码云)来托管代码,本地的使用Eclipse,该如何配置? 步骤: 1/ 在码云 上建一个工程,(为了访问托管工程的权限) 2/ 在eclipse中打开名字叫做“Git ...
- Oracle中的伪列
分页查询中,需要用到伪列rownum,代码如下: select * from (select rownum rn, name from cost where rownum <= 6) where ...