题目链接 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. python 监控日志

    #需求: #1.每分钟监控服务器日志,ip请求超过200次的,加入黑名单 #1.读文件,获取到每行的内容 open readlines # 178.210.90.90 - - [04/Jun/2017 ...

  2. 阿里大鱼短信发送 FOR DT

    //增加了参数$action 来标志发送的是什么短信 注册短信 验证码短信 提示短信等 function send_sms($mobile, $message, $word = 0, $time = ...

  3. 我的Python分析成长之路6

    模块:本质就是.py结尾的文件.从逻辑上组织python代码. 包: 本质就是一个目录,带有__init__.py文件,从逻辑上组织模块. 模块的分类: 1.标准库(内置的模块) 2.开源库(第三方库 ...

  4. leetcode-26-exercise_linked-list

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. 解题思路: 需要检查before和afte ...

  5. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  6. Mysql进入数据库

    进入某个数据库: use db_name; //db_name为数据库名称 mysql> use db_name Database changed

  7. flask-用户资料

    首先创建User模型 class User(UserMixin,db.Model): __tablename__ = 'users' #.. name = db.Column(db.String(64 ...

  8. ccna 闫辉单臂路由 和 acl access control list

    ccna 闫辉单臂路由 和  acl   access control list 一单臂路由     当前园区网设计很少用到       成本低  小型的.局域网可用         二ACL acc ...

  9. Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识

    今日内容: 1.hash模块2.xml模块3.configparser模块4.sheve 模块5.shutil模块 知识点一:hash什么是hash: hash是一种算法,该算法接受传入的的内容,经过 ...

  10. 学习C++的第四天

    1.头文件中的 #ifndef/#define/#endif 防止该头文件被重复引用” //文件路径名:el_1\hello.h #ifndef _HELLO /////如果没有定义 _HELLO文件 ...