题目链接 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(矩阵快速幂)的更多相关文章

  1. A - Number Sequence(矩阵快速幂或者找周期)

    Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...

  2. 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 ...

  3. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  4. 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, ...

  5. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  6. 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 ...

  7. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  8. 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 ...

  9. CF - 392 C. Yet Another Number Sequence (矩阵快速幂)

    CF - 392 C. Yet Another Number Sequence 题目传送门 这个题看了十几分钟直接看题解了,然后恍然大悟,发现纸笔难于描述于是乎用Tex把初始矩阵以及转移矩阵都敲了出来 ...

随机推荐

  1. 怎么删除服务中的mysql服务

    可以进WINDOWS的管理里查看MYSQL的服务,把它停止或以DOS下用命令停止1.如果要卸载MYSQL执行下面命令:DOS下>mysqld -remove mysql2.启动MYSQL: DO ...

  2. Golang map并发 读写锁

    golang并发 一:只有写操作 var ( count int l = sync.Mutex{} m = make(map[int]int) ) //全局变量并发写 导致计数错误 func vari ...

  3. SDUSToj第十一次作业源代码格式问题

    Problem I: 源代码的格式 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1471  Solved: 634 [Submit][Status][W ...

  4. 一步一步解剖Libevent源代码 - 0

    本系列文章将在<Libevent源码深度解剖>的基础上,结合Libevent-2.0.22代码,更新了其中的一些定义和说明,以及加上了bufferevent部分.   一.Libevent ...

  5. [automator篇][9] 列表,找孩子

    private boolean ClickByCollInfo(int CLICK, String classname, String id, String text) { UiSelector ui ...

  6. Game on Tree

    D - Game on Tree Time limit : 2sec / Memory limit : 256MB Score : 1100 points Problem Statement Ther ...

  7. BZOJ-2829 信用卡凸包

    凸包题. 我们先把所有信用卡的四个定点的坐标求出来,然后计算凸包长度,最后加上一个圆的周长就行. #include <cstdlib> #include <cstdio> #i ...

  8. IBM QMF下载

    官网下载页面: http://www-01.ibm.com/support/docview.wss?uid=swg27009383 官方BBS: https://w3-connections.ibm. ...

  9. iOS-通信录

    1.概述: * 对于每一个移动设备而言,都有一个内置的数据库-----通讯录. * 在IOS上,通讯录放在SQLite3数据库中. * 由于不同应用之间不能直接访问,我们想要实现对数据库的访问,必须使 ...

  10. 解决 Could not load hsdis-amd64.dll

    win10下想查看JIT编译的汇编源码 结果提示: Could not load hsdis-amd64.dll; library not loadable; PrintAssembly is dis ...