题意:一个递推式第n项%b是多少。

递推式:

构造矩阵:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; long long MOD;
long long n; struct Matrix
{
long long A[][];
int R, C;
Matrix operator*(Matrix b);
}; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix b)
{
Matrix c;
memset(c.A, , sizeof(c.A));
int i, j, k;
for (i = ; i <= R; i++)
for (j = ; j <= b.C; j++)
for (k = ; k <= C; k++)
c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j]) % MOD) % MOD;
c.R = R; c.C = b.C;
return c;
} void init()
{
memset(X.A, , sizeof X.A);
memset(Y.A, , sizeof Y.A);
memset(Z.A, , sizeof Z.A); Z.R = ; Z.C = ;
Z.A[][] = Z.A[][] = Z.A[][] = ; X.R = ; X.C = ;
X.A[][] = ; X.A[][] = ; X.A[][] = ;
X.A[][] = ; X.A[][] = ; X.A[][] = ;
X.A[][] = ; X.A[][] = ; X.A[][] = ; Y.R =; Y.C = ;
for (int i = ; i <= ; i++) Y.A[i][i] = ;
} void work()
{
while (n)
{
if (n % == ) Y = Y*X;
n = n >> ;
X = X*X;
}
Z = Z*Y; printf("%lld\n", Z.A[][]);
} int main()
{
int Case = ;
while (~scanf("%lld%lld", &n, &MOD))
{
if (!n&&!MOD) break;
init();
printf("Case %d: %lld %lld ", Case++, n, MOD);
work();
}
return ;
}

UVA 10518 How Many Calls?的更多相关文章

  1. uva 10518 - How Many Calls?(矩阵快速幂)

    题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...

  2. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  3. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  4. ACM--[kuangbin带你飞]--专题1-23

    专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. UVA 1456 六 Cellular Network

    Cellular Network Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  7. UVA 1619 Feel Good(DP)

    Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...

  8. POJ 2796[UVA 1619] Feel Good

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16786   Accepted: 4627 Case T ...

  9. uva live 6827 Galaxy collision

    就是给出非常多点,要求分成两个集合,在同一个集合里的点要求随意两个之间的距离都大于5. 求一个集合.它的点数目是全部可能答案中最少的. 直接从随意一个点爆搜,把它范围内的点都丢到跟它不一样的集合里.不 ...

随机推荐

  1. svn删除用户

    如果安装有svn客户端,右键打开svn settings 然后就可以重新登录svn用户了 或者删除此文件

  2. Android自动化测试之环境搭建

    Android自动化测试之环境搭建 一.Android-sdk介绍 SDK(Software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台. ...

  3. angular.js简介

    angularJS是一个javaScript框架.可通过<script>标签添加到HTML页面 angular通过指令扩展了HTML,且通过表达式绑定数据到html 当网页加载完毕,Ang ...

  4. IDL 建立影像金字塔

    形成按目录放好的,类似于Google Map Tile的金字塔瓦片Jpg. 1: ; 2: pro tsplit 3: ; 读入Jpeg格式文件 4: szFile = 'e:\test.jpg'; ...

  5. nginx slab内存管理

    本来这一篇作为nginx系列的开头是不合适的,不过由于nginx进程框架自己的梳理还没完成,这部分又刚好整理完了,就从这开始吧.这儿谈的是nginx的slab的内存管理方式,这种方式的内存管理在ngi ...

  6. Chapter 1 First Sight——10

    Instead, I was ivory-skinned, without even the excuse of blue eyes or red hair, despite the constant ...

  7. MJRefresh

    automaticallyChangeAlpha 下拉或上拉时,文字颜色逐渐加深

  8. led.c驱动框架

    Makefile: obj-m += led.o ################################################ KERNEL = /home/linux--FS21 ...

  9. Hadoop集群搭建的密钥配置SSH实现机制

  10. 杀死进程kill和fuser

    1 kill -9 id 2   不行的话使用    fuser -k -SIGHUP /opt/bre/cookiemapping/wsapi/123 fuser有一个特别的用法在于它可以一次杀死那 ...