UVa 10870 & 矩阵快速幂】的更多相关文章

题意: 求一个递推式(不好怎么概括..)的函数的值. 即 f(n)=a1f(n-1)+a2f(n-2)+...+adf(n-d); SOL: 根据矩阵乘法的定义我们可以很容易地构造出矩阵,每次乘法即可求出下一位f(n)的值并在距震中保存f(n)-----f(n-d+1). 像我这种傻逼看错好几次运算法则的人 = = 第一道矩乘对着老人家模板打得几乎一模一样-----只是觉得他的写法比较优雅= =(虽然我感觉那么多memcpy会不会让常数很大...) CODE: /*===============…
给出一个d阶线性递推关系,求f(n) mod m的值. , 求出An-dv0,该向量的最后一个元素就是所求. #include <iostream> #include <cstdio> #include <cstring> using namespace std; ; typedef long long Matrix[maxn][maxn]; typedef long long Vector[maxn]; int d, n, m; void matrix_mul(Mat…
第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; ; int n;…
题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) ..... f(d),求 f(n) = a1*f(n-1) + a2*f(n-2) +....+ ad*f(n-d),计算f(n) % m. 析:很明显的矩阵快速幂,构造矩阵, ,然后后面的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1…
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 n 很大 自然想到是 矩阵快速幂 那么问题就是 怎么构造矩阵 我们想到的一种构造方法是 n = 2 时 n = 3 时 然后大概就能够发现规律了吧 .. AC代码 #include <cstdio> #include <cstring> #include <ctype.h>…
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a): //POJ #include <cstdio> #include <iostream> using namespace std; ; struct matrix { ][]; }ans, base; matrix multi(matrix a, matrix b) { matrix…
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #include <string.h> long long n; int b; struct state { int s[2][2]; state(int a = 0, int b = 0, int c = 0, int d = 0) { s[0][0] = a, s[0][1] = b, s[1][0] =…
题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cstring> #define ll long long #define mod int(1e9+9) struct jz { ll num[][]; jz(){ memset(num, , sizeof(num)); } jz operator*(const jz&p)const { jz ans…
                  Yet another Number Sequence Let’s define another number sequence, given by the following function:f(0) = af(1) = bf(n) = f(n − 1) + f(n − 2), n > 1When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values…
                             Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...) are defined by the recurrence:F0 = 0F1 = 1Fi = Fi−1 + Fi−2 for i > 1Write a program which calculates Mn = Fn mod 2m for given pair of n and…
题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(109). #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include&…
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Google Codejam Round 1A的C题. #include <bits/stdc++.h> typedef long long ll; const int N = 5; int a, b, n, mod; /* *矩阵快速幂处理线性递推关系f(n)=a1f(n-1)+a2f(n-2)+.…
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模)   Input 一个数n,表示长度.(n<1e15) Output 长度为n的非010串的个数.(对1e9+7取模) Input示例 3 Output示例 7 解释: 000 001 011 100 101 110 111 读完题,这样的题目肯定是能找到规律所在的,要不然数据太大根本无法算.假设现在…
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; ; ; int n, m; struct Mat{//矩阵 ll mat[N][N]; }; Mat operator * (Mat a, Mat b){//一次矩阵乘法…
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. 在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他又和他人讨论起了二叉搜索树.什么是二叉搜索树呢?二叉搜索树首先是一棵二叉树.设key[p]表示结点p上的数值.对于其中的每个结点p,若其存在左孩子lch,则key…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2) = b,求f(n) 思路:对矩阵快速幂的了解仅仅停留在fib上,重现赛自己随便乱推还一直算错,快两个小时才a还wa了好几次.... 主要就是构造矩阵:(n+1)^4 = n^4 + 4n^3 + 6n^2 + 4n + 1 |1   2   1   4   6   4   1|     |  …
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输入3个数:A,B,N.数字之间用空格分割.(-10000 <= A, B <= 10000, 1 <= N <= 10^9) Output 输出f(n)的值. Input示例 3 -1 5 Output示例 6题意:f(n) = (A * f(n - 1) + B * f(n - 2)…
题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS:在别的博客粘过来,暂时还不太理解...) 设f(n)为字符串长度为n时复合条件的字符串个数,以字符串最后一个字符为分界点,当最后一个字符为m时前n-1个字符没有限制,即为f(n-1):当最后一个字符为f时就必须去除最后3个字符是fmf和fff的情况,在考虑最后两个字符为mf和ff的情况,显然不行:最…
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d      C D   =   c*A+d*C  c*A+d*C 上代码 struct matrix { ll a[maxn][maxn]; }; matrix matrix_mul(matrix x,matrix y) { matrix temp; ;i<=n;i++) ;j<=n;j++) { tem…
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 238    Accepted Submission(…
So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2286    Accepted Submission(s): 710 Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the ceil…
http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出来了 这里用的是2维 vector #include<iostream> #include<cstdio> #include<vector> using namespace std; typedef vector<int>vec; typedef vector&…
fibonacci数列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, - An alter…
题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮,问最后这n个猫各自有多少坚果. 题解:构造(n+1)*(n+1)的单位矩阵,data[i][j]表示第i个猫与第j个猫进行交换,最后一列的前n项就是每个猫的坚果数目,s操作就交换对应行,矩阵快速幂时间复杂度O(n^3*log2(m))会超时,我们注意到在n*n的范围内每一行只有一个1,利用稀疏矩阵的…
分析:假设g(g(g(n)))=g(x),x可能非常大,但是由于mod 10^9+7,所以可以求出x的循环节 求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也可能非常大,但是由x的循环节可以求出y的循环节 所以最终结果只要进行矩阵快速幂即可求出 循环节 #include<stdio.h> ;//第一次是MOD=1000000007 找出循环节是222222224 //第二次是MOD=222222224,找出循环节183120 int main() {…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ 解题思路: 题目挺吓人的.先把完整组合数+Fibonacci展开来. 利用Fibonacci的特性,从第一项开始消啊消,消到只有一个数: $S(0)=f(0)$ $S(1)=f(2)$ $S(2)=f(4)$ $S(n)=f(2*n)$ 这样矩阵快速幂就可以了,特判$n=0$时的情况. 快速幂矩阵…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个环可以取下或放上,cost=1.求最小cost.MOD 200907. 解题思路: 递推公式 题目意思非常无聊,感觉是YY的. 设$dp[i]$为取第i个环时的总cost. $dp[1]=1$,$dp[2]=2$,前两个环取下是没有条件要求的. 从i=3开始,由于条件对最后的环限制最大,所以从最后一…
Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 30    Accepted Submission(s): 20 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, the…
link: http://acm.hdu.edu.cn/showproblem.php?pid=4686 构造出来的矩阵是这样的:根据题目的ai * bi = ……,可以发现 矩阵1 * 矩阵3 = 矩阵2.然后就是矩阵快速幂了. 1 1 ai bi ai*bi Si 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 ai+1 bi+1 ai+1*bi+1 Si+1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 1 AY…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写出方程: D = c1 c2 ``` c[h-1] c[h] 1 0 ``` 0 0 0 1 ``` 0 0 0 0   0 0 0 0   1 0 V[x] = f[x] f[x-1] ` ` f[x-h+1] 显然有V[x+1] = D*V[x].D是由系数行向量,一个(h-1)*(h-1)的单…