博客
关于我
A^X mod P(简单数论 + 思维打表)
阅读量:253 次
发布时间:2019-03-01

本文共 989 字,大约阅读时间需要 3 分钟。

一.题目链接:

二.题目大意:

给出 T,n, A, K,a, b, m, P.

1 \leq n\leq 10^{6}

0 \leq A, K, a, b \leq 10^{9}

1 \leq m, P \leq 10^{9}

T 组样例.

 f(x) = \left\{\begin{matrix}1\;\;\;x=1 & & & & & & & & \\(a \times f(x - 1) + b)\;(mod\;\;m)\;\;\;x > 1 & & & & & & & & \end{matrix}\right.

求 A^{f(1)}+A^{f(2)}+....+A^{f(n)} \;\;(mod\;\;p).

三.分析:

由于 1 \leq m \leq 10^{9}

所以 1 \leq f(x) \leq 10^{9}

如果用快速幂求和的话会 TLE.

因为 A^{f(x)} = A^{a\times 10^{5} + b}

所以只需要求 sum1[] 和 sum2[].

sum1[i]:A^{i}\;\;(mod\;\;p)

sum2[i]:A^{10^{5}i}\;\;(mod\;\;p)

所以 A^{f(x)} = sum1[i \;mod\;M] \times sum2[i\;/\;M]

详见代码.

四.代码实现:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define eps 1e-6#define PI acos(-1.0)#define ll long long intusing namespace std;const int M = (int)1e5;ll n, A, K, a, b, m, p;ll sum1[M + 5];ll sum2[M + 5];void init(){ sum1[0] = sum2[0] = 1; for(int i = 1; i <= M; ++i) sum1[i] = sum1[i - 1] * A % p; sum2[1] = sum1[M]; for(int i = 1; i <= M / 10; ++i) sum2[i] = sum2[i - 1] * sum2[1] % p;}ll f(){ ll fx = K; ll sum = 0; for(ll i = 1; i <= n; ++i) { sum = (sum1[fx % M] * sum2[fx / M] % p + sum) % p; fx = (a * fx + b) % m; } return sum % p;}int main(){ int T; scanf("%d", &T); for(int ca = 1; ca <= T; ++ca) { cin >> n >> A >> K >> a >> b >> m >> p; init(); ll ans = f(); printf("Case #%d: %lld\n", ca, ans); } return 0;}

 

转载地址:http://tyht.baihongyu.com/

你可能感兴趣的文章
MySQL事务详解结合MVCC机制的理解
查看>>
MySQL事务隔离级别:读未提交、读已提交、可重复读和串行
查看>>
MySQL事务隔离级别:读未提交、读已提交、可重复读和串行
查看>>
webpack css文件处理
查看>>
mysql二进制包安装和遇到的问题
查看>>
MySql二进制日志的应用及恢復
查看>>
mysql互换表中两列数据方法
查看>>
mysql五补充部分:SQL逻辑查询语句执行顺序
查看>>
mysql交互式连接&非交互式连接
查看>>
MySQL什么情况下会导致索引失效
查看>>
Mysql什么时候建索引
查看>>
MySql从入门到精通
查看>>
MYSQL从入门到精通(一)
查看>>
MYSQL从入门到精通(二)
查看>>
mysql以下日期函数正确的_mysql 日期函数
查看>>
mysql以服务方式运行
查看>>
mysql优化--索引原理
查看>>
MySQL优化之BTree索引使用规则
查看>>
MySQL优化之推荐使用规范
查看>>
Webpack Critical CSS 提取与内联教程
查看>>