Codechef Eugene and big number(矩阵快速幂)
题目转化为
$f(n) = m * f(n - 1) + a$
$f(n + 1) = m * f(n) + a$
两式相减得
$f(n + 1) = (m + 1) * f(n) - m * f(n - 1)$
求$f(n)$
其中$m$为$10^{k}$ ($k$为$a$的位数)
那么利用矩阵快速幂加速就可以了。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) typedef long long LL; struct Matrix{ LL arr[5][5];} init, unit, Unit; int T;
LL a, n, mod; inline LL Pow(LL a, LL b, LL Mod){
LL ret(1); for (; b; b >>= 1, (a *= a) %= Mod) if (b & 1) (ret *= a) %= Mod; return ret;
} Matrix Mul(Matrix a, Matrix b){
Matrix c;
rep(i, 1, 2) rep(j, 1, 2){
c.arr[i][j] = 0;
rep(k, 1, 2) (c.arr[i][j] += (a.arr[i][k] * b.arr[k][j] % mod)) %= mod;
}
return c;
} Matrix MatrixPow(Matrix a, LL k){
Matrix ret(Unit); for (; k; k >>= 1, a = Mul(a, a)) if (k & 1) ret = Mul(ret, a); return ret;
} inline LL calc(LL n){
LL ret(0); for (; n; n /= 10) ++ret;
return ret;
} int main(){ scanf("%d", &T);
while (T--){
Unit.arr[1][1] = Unit.arr[2][2] = 1;
scanf("%lld%lld%lld", &a, &n, &mod);
if (a == 0LL){
puts("0");
continue;
}
LL exp = calc(a), m = Pow(10, exp, mod);
LL f1 = a % mod, f2 = ((f1 * m) % mod + a) % mod;
LL f3 = ((f2 * m) % mod + a) % mod;
if (n == 1LL){
printf("%lld\n", f1 % mod);
continue;
} if (n == 2LL){
printf("%lld\n", f2 % mod);
continue;
} if (n == 3LL){
printf("%lld\n", f3 % mod);
continue;
} LL num = (2 * mod - m) % mod; init.arr[1][1] = f3, init.arr[1][2] = init.arr[2][1] = f2, init.arr[2][2] = f1;
unit.arr[1][1] = m + 1, unit.arr[1][2] = num, unit.arr[2][1] = 1, unit.arr[2][2] = 0;
Matrix mul = MatrixPow(unit, n - 3);
Matrix ret = Mul(mul, init);
printf("%lld\n", ret.arr[1][1]);
} return 0;
}
Codechef Eugene and big number(矩阵快速幂)的更多相关文章
- A - Number Sequence(矩阵快速幂或者找周期)
Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...
- HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- 2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198【矩阵快速幂】
Problem Description We define a sequence F: ⋅ F0=0,F1=1;⋅ Fn=Fn−1+Fn−2 (n≥2). Give you an integer k, ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- CF - 392 C. Yet Another Number Sequence (矩阵快速幂)
CF - 392 C. Yet Another Number Sequence 题目传送门 这个题看了十几分钟直接看题解了,然后恍然大悟,发现纸笔难于描述于是乎用Tex把初始矩阵以及转移矩阵都敲了出来 ...
随机推荐
- 多进程 multiprocessing 多线程Threading 线程池和进程池concurrent.futures
multiprocessing.procsess 定义一个函数 def func():pass 在if __name__=="__main__":中实例化 p = process( ...
- Java-basic-1
1. Java Standard Edition (Java SE) Java Enterprise Edition (Java EE): geared toward developing large ...
- 更改activity切换方式
overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ...
- github仓库主页介绍
- 03008_ServletContext
1.什么是ServletContext? (1)ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象 内部封装是该web应用的信息,Servle ...
- Jenkins自动化搭建测试环境(一)
Jenkins基础 首先上官网jenkins.io上下载最新的Jenkins war包 将下载完成的war包解压 java -jar jenkins.war 接下来使用浏览器访问localhost:8 ...
- Notepad++ 使用正则表达式查找替换字符串
最近在改一些别人写的PHP代码,由于之前的代码可维护性比较差,因此工作量比较多,因此想找一些高效的方式改一些有规律的代码. 比如: 我想将一些类似$rowss[MaxGetCash_num], ...
- python学习-- django 2.1.7 ajax 请求
#--------------views.py---------------------- def add(request): a = request.GET['a'] print(a) b = re ...
- WordPress后台添加侧边栏菜单
add_action('admin_menu', 'register_custom_menu_page'); function register_custom_menu_page() { add_me ...
- python ConfigParser 的小技巧
$ cat format.conf [DEFAULT] conn_str = %(dbn)s://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s dbn = mysq ...