博客
关于我
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 problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql skip-grant-tables_MySQL root用户忘记密码怎么办?修改密码方法:skip-grant-tables
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql sysbench测试安装及命令
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
MySQL Troubleshoting:Waiting on query cache mutex
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>