山东第四届省赛C题: A^X mod P
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3232
Problem C:A^X mod P
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 10 Solved: 2
[Submit][Status][Discuss]
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
和昨天做的福大的很像,但不是。
题目意思很简单,比赛用了枚举的方法,知道会超时,40组测试数据。
时间复杂度 o(n)=10^6*40*logn ==>10^9 每计算一个a^p%m logn
怎么做?
( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P.
看来别人的思路,才想到拆分。有了这个思想,就容易起来了,就是哈希。保存结果,每一次遇到,直接读取。避免了重复计算。
A^n=A^(k*x+y);
n=k*x+y;==>(A^k)^x * A^y;
A^k和A^y可以用一个dp1[ ]数组保存起来。这样就解决一个了。
(A^k)^x也用一个数组 dp2[ ]保存起来。
式子就可以转化为 A^n= dp2[n/k]*dp1[n%k]; 对比一下 (A^k)^x * A^y;
K取多大呢???10^6? 太大了,超了,没有必要这么大。
因为:
f(x) = (a*f(x-1) + b)%m , x > 1
f(x)最大是10^9 所以K=33333就足够了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define HH 33000
#define GG 33000
using namespace std; long long dp1[];
long long dp2[]; void make_ini(long long A,long long P)
{
long long i;
dp1[]=dp2[]=;
dp1[]=A;
for(i=;i<=HH;i++)
dp1[i]=(dp1[i-]*A)%P;//A^k dp2[]=dp1[HH];
for(i=;i<=GG;i++)
dp2[i]=(dp1[HH]*dp2[i-])%P;
} long long make_then(long long K,long long n,long long A,long long a,long long b,long long m,long long P)
{
long long ans=,i,j,tmp;
tmp=K;
for(i=;i<=n;i++)
{
ans=(ans+dp2[tmp/HH]*dp1[tmp%HH])%P;
tmp=(a*tmp+b)%m;
}
return ans;
} int main()
{
long long T,n,A,K,a,b,m,P,i,tmp;
while(scanf("%lld",&T)>)
{
for(i=;i<=T;i++)
{
scanf("%lld%lld%lld%lld%lld%lld%lld",&n,&A,&K,&a,&b,&m,&P);
make_ini(A,P);
tmp=make_then(K,n,A,a,b,m,P);
printf("Case #%lld: %lld\n",i,tmp);
}
}
return ;
}
山东第四届省赛C题: A^X mod P的更多相关文章
- 山东第四届省赛: Boring Counting 线段树
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec ...
- 第八届山东ACM省赛F题-quadratic equation
这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...
- 2019山东ACM省赛L题题解(FLOYD传递闭包的变形)
本题地址 https://cn.vjudge.net/contest/302014#problem/L Median Time Limit: 1 Second Memory Limit: 6 ...
- 振兴中华(蓝桥杯13年第四届省赛真题 JAVA-B组)
思路:因为只能横向或纵向跳到相邻的格子里,所以到'华'字有两种方法:①从左边的中横向跳过来 ②从上边的中纵向跳过来 直接递推即可. 标题: 振兴中华 小明参加了学校的趣味运动会,其中的一个项目是:跳格 ...
- 2013杭州现场赛B题-Rabbit Kingdom
杭州现场赛的题.BFS+DFS #include <iostream> #include<cstdio> #include<cstring> #define inf ...
- 2017年第六届数学中国数学建模国际赛(小美赛)C题解题思路
这篇文章主要是介绍下C题的解题思路,首先我们对这道C题进行一个整体的概括,结构如下: C题:经济类 第一问:发现危险人群. 发现:欺诈的方式开始.雇佣或浪漫的承诺. 数据→确定特定的经济萧条地区→确定 ...
- 2013年山东省赛F题 Mountain Subsequences
2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...
- 2013年省赛H题
2013年省赛H题你不能每次都快速幂算A^x,优化就是预处理,把10^9预处理成10^5和10^4.想法真的是非常巧妙啊N=100000构造两个数组,f1[N],间隔为Af2[1e4]间隔为A^N,中 ...
- 2013年省赛I题 Thrall’s Dream
2013年省赛I题判断单向联通,用bfs剪枝:从小到大跑,如果遇到之前跑过的点(也就是编号小于当前点的点),就o(n)传递关系. bfs #include<iostream> #inclu ...
随机推荐
- Ubuntu16.04 - 安装gtk+-3.0和appindicator3-0.1
今天在Ubuntu16.04里面遇到这样的问题: # pkg-config --cflags gtk+-3.0 appindicator3-0.1Package gtk+-3.0 was not fo ...
- series dataframe 的 idxmax()
返回最大值的索引
- 926. Flip String to Monotone Increasing
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...
- PyMysql复习
参考:http://www.cnblogs.com/liwenzhou/p/8032238.html 使用pycharm操作数据库. 填一个数据库名,User:填root 填写要连接的数据库. 建表. ...
- poj3070 Fibonacci(矩阵快速幂)
矩阵快速幂基本应用. 对于矩阵乘法与递推式之间的关系: 如:在斐波那契数列之中 f[i] = 1*f[i-1]+1*f[i-2] f[i-1] = 1*f[i-1] + 0*f[i-2].即 所以, ...
- Sysbench0.5初体验
最近工作中需要测试数据库的OLTP的性能,参考了下MariaDB的benchmark中的测试脚本,发现脚本中已经使用了Sysbench-0.5,可以在这里https://launchpad.net/s ...
- Kafka副本同步机制
引用自:http://blog.csdn.net/lizhitao/article/details/51718185 Kafka副本 Kafka中主题的每个Partition有一个预写式日志文件,每个 ...
- jvm(1)类的加载(三)(线程上下文加载器)
简介: 类加载器从 JDK 1.0 就出现了,最初是为了满足 Java Applet 的需要而开发出来的. Java Applet 需要从远程下载 Java 类文件到浏览器中并执行. 现在类加载器在 ...
- UIKit: Apps for Every Size and Shape
safeArea 即可以正常显示内容的部分.   可以通过 additionalSafeAreaInsets 来调整 safeArea 的大小.  经过调整,范围如下: self.additio ...
- javascript数据结构与算法--二叉树遍历(先序)
javascript数据结构与算法--二叉树遍历(先序) 先序遍历先访问根节点, 然后以同样方式访问左子树和右子树 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...