So Easy!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4673    Accepted Submission(s): 1539

Problem Description

A sequence Sn is defined as:

Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
  You, a top coder, say: So easy! 

Input

There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.

Output

For each the case, output an integer Sn.

Sample Input

2 3 1 2013 2 3 2 2013 2 2 1 2013

Sample Output

4 14 4
根据条件可证(a-√b)n小于1,(a+√b)n向上取整即为求(a+√b)n + (a-√b)n,问题又转换为a^n+b^n的形式。
 1 //2017-08-05
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #define ll long long
 7
 8 using namespace std;
 9
10 ll a, b, n;
11 ll p, q;
12 const int N = 5;
13 ll MOD;
14
15 struct Matrix
16 {
17     ll a[N][N];
18     int r, c;
19 }ori, res;
20
21 void init()
22 {
23     memset(res.a, 0, sizeof(res.a));
24     res.r = 1; res.c = 2;
25     res.a[1][1] = p;
26     res.a[1][2] = 2;
27     ori.r = 2; ori.c = 2;
28     ori.a[1][1] = p;
29     ori.a[1][2] = 1;
30     ori.a[2][1] = -q;
31     ori.a[2][2] = 0;
32 }
33
34 Matrix multi(Matrix x, Matrix y)
35 {
36     Matrix z;
37     memset(z.a, 0, sizeof(z.a));
38     z.r = x.r, z.c = y.c;
39     for(int i = 1; i <= x.r; i++)
40     {
41         for(int k = 1; k <= x.c; k++)
42         {
43             if(x.a[i][k] == 0) continue;
44             for(int j = 1; j<= y.c; j++)
45                 z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]) % MOD + MOD) % MOD;
46         }
47     }
48     return z;
49 }
50
51 void Matrix_pow(int n)
52 {
53     while(n)
54     {
55         if(n & 1)
56             res = multi(res, ori);
57         ori = multi(ori, ori);
58         n >>= 1;
59     }
60     printf("%lld\n", res.a[1][1] % MOD);
61 }
62
63 int main()
64 {
65     while(scanf("%lld%lld%lld%lld", &a, &b, &n, &MOD)!=EOF){
66         p = 2*a;
67         q = a*a-b;
68         init();
69         if(n == 0)printf("2\n");
70         else if(n == 1)printf("%lld\n", p);
71         else Matrix_pow(n-1);
72     }
73
74     return 0;
75 }

Source
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现

转载于:https://www.cnblogs.com/Penn000/p/7291671.html

HDU4565(SummerTrainingDay05-C 矩阵快速幂)相关推荐

  1. 矩阵快速幂+构造方法

    与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...

  2. 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂

    原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...

  3. 快速幂 + 矩阵快速幂

    快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...

  4. HDU4549(矩阵快速幂+快速幂)

    f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...

  5. [HNOI2008]GT考试[矩阵快速幂+kmp优化的dp]

    解题思路:假如说我们用f[i]表示长度为i的串能组合成无不吉利数字的组合的个数的话我们无法找到f[i]和f[i+1]的关系,就是我们下一位填某个数字会不会出现不吉利串,这就和你前面的串末尾于不吉利串重 ...

  6. I-Matrix Power Series POJ - 3233 矩阵快速幂+分治

    I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...

  7. H - Fibonacci POJ - 3070 (矩阵快速幂)

    H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...

  8. HDU 6185 Covering 矩阵快速幂 递推

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6185 题目描述: 一个4*n的矩形, 你用1*2的矩形覆盖有多少种方案, n <= 1e18 ...

  9. bzoj 1409 Password 矩阵快速幂+欧拉函数

    可以发现,该数组的mi就是斐波那契数列 所以要矩阵快速幂搞出第n位 但是斐波那契数列上涨的很快,这就需要欧拉定理了 p^phi(q)%q=1(gcd(p,q)==1) p是素数,所以可以用 然后需要5 ...

  10. POJ 2778 DNA Sequence [AC自动机 + 矩阵快速幂]

    http://poj.org/problem?id=2778 题意:给一些只由ACGT组成的模式串,问有多少种长度为n且不含有给出的模式串的DNA序列. 自动机的状态转换可以看成一个有向图(有重边的) ...

最新文章

  1. (循环练习题) 五只猴子分桃子
  2. poj3122(二分算法)
  3. zookeeper版本更新_Zookeeper归纳
  4. python自学要多久 知乎-怎么自学python,大概要多久?
  5. “63个国外优秀测试站点链接”和其他相关资料,排除了目前已失效的网站和资料链接。...
  6. 百度的索引真的比雅虎多么?
  7. Cacheable注解使用详解
  8. mysql基础知识整理_MYSQL基础知识整理
  9. Failed to find Build Tools revision 26.0.1
  10. 智能会议系统(35)---深入浅出sip协议
  11. $.ajax()方法
  12. 蚂蚁集团换帅!胡晓明辞任 CEO
  13. 2019春Python程序设计测试(20190611--20190611)
  14. 2 什么样的顾客会选择离开
  15. NetMeeting不能共享桌面的解决办法
  16. 网易云IM(即时通讯) 登录指南(Android)
  17. vue-video-player 断点续播
  18. Python爬虫技术及PyQt5界面编程实现12306火车票查询
  19. Bmob后端云实现无后端开发APP
  20. int定义源码 python_【图片】Python 源码:int【西安网星软件吧】_百度贴吧

热门文章

  1. Code Snippets for Windows Mobile 5 in C#
  2. 学习笔记: 委托解析和封装,事件及应用
  3. 兔子问题or斐波那契数列
  4. Vim vimrc配置
  5. JavaScript if(x),==和===解析(翻译整理)
  6. spring 容器技术入门
  7. Java中Dom解析xml文档
  8. 关于值传递和指针传递
  9. 使用Qtip2来开发功能强大的删除和信息提示功能
  10. Linux入门-安装篇(Debian 服务器版)