题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题思路:定义f(n)=an+bn,则有f(n)∗(a+b)=(an+bn)∗(a+b)=an+1+abn+ban+bn+1=f(n+1)+abf(n−1), 所以f(n+1)=(a+b)f(n)−abf(n−1),用矩阵高速幂求解. #include <cstdio> #include <cs…
题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) & ab \end{bmatrix} \begin{bmatrix} a^{n-1}+b^{n-1}\\ a^n+b^n \end{bmatrix} = \begin{bmatrix} a^n+b^n\\ a^{n+1}+b^{n+1} \end{bmatrix}\) 注意不要遇到\(p,q\)都为\(0\…
---恢复内容开始--- Given the value of a+b and ab you will have to find the value of an+bn 给出a+b和a*b的值,再给出n求a^n+b^n的值. Input The input file contains several lines of inputs. Each line except the last line contains 3 non-negative integers p, q and n. Here p …
Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Given the value of a+b and ab you will have to find the value of an+bn Input The input file contains several lines of inputs. Each line except the last…
Given the value of a+b and ab you will have to find the value of a n + b n Input The input file contains several lines of inputs. Each line except the last line contains 3 non-negative integers p, q and n. Here p denotes the value of a+b and q denote…
题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) 较小的情况,可以很容易的想到递推: \[ \begin{array}{} \text{令} F(i) & = a ^ n + b ^ n \\ & = (a + b)(a ^ {n - 1} + b ^ {n - 1}) - (ab ^ {n - 1} + a^{n - 1}b) \\ &am…
题目大意:给出a+b的值和ab的值,求a^n+b^n的值. 题目分析:有种错误的方法是这样的:利用已知的两个方程联立,求解出a和b,进而求出答案.这种方法之所以错,是因为这种方法有局限性.联立之后会得到一个二元一次方程,只有当该方程有实数解确切的说是当某个数据满足该方程有实数解时,这种方法得到的结果才有可能正确.显然,题中数据不可能这么片面.正确的方法是这样的: 令a+b=A,ab=B,S(n)=an+bn.则S(n)=an+bn=(a+b)(an-1+bn-1)-abn-1-an-1b=(a+…
https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: 这道题目根据递推式是可以转化为矩阵的: #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<vector> #include<qu…
题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2.看到 a^n+b^n 这个式子就想到二阶齐次递推式的通项公式,然后就想是否能通过通项公式反推回递推式.结果发现a.b的值是不能确定是否相等的,而求二阶齐次递推式的通项公式时,是需要根据根的情况来分类求解的,所以此题不适应. 3.那么,就直接对 a^n+b^n 做一下变形: 4.得到递推式之后,就直接…
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要加的位置值为1.其余位置为0构造出矩阵,进行高速幂就可以 代码: #include <cstdio> #include <cstring> const int N = 55; int t, n, r, a[N]; struct mat { int v[N][N]; mat() {mem…
题意 求解递推式 \(f(n)=a_1*f(n-1)+a_2*f(n-2)+....+a_d*f(n-d)\) 的第 \(n\) 项模以 \(m\). \(1 \leq n \leq 2^{31}-1\) \(1 \leq m \leq 46340\) \(1 \leq d \leq 15\) 思路 矩阵乘法最经典的运用之一.先大致介绍一下矩阵乘法: 对于一个矩阵 \(A_{np}\) ,另一个矩阵 \(B_{pm}\) ,设它们的乘积为 \(C_{n,m}\) ,有 \(C_{i,j}=\di…
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 n 很大 自然想到是 矩阵快速幂 那么问题就是 怎么构造矩阵 我们想到的一种构造方法是 n = 2 时 n = 3 时 然后大概就能够发现规律了吧 .. AC代码 #include <cstdio> #include <cstring> #include <ctype.h>…
                             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…
链接:https://vjudge.net/problem/UVA-11019lrjP218 matrix matcher #include<bits/stdc++.h> using namespace std; #define P pair<int,int> #define ms(x,y) memset(x,y,sizeof x) #define LL long long ; ; *+; ; vector<int> as[maxnode]; ][], ans; int…
layout: post title: 「kuangbin带你飞」专题十九 矩阵 author: "luowentaoaa" catalog: true tags: mathjax: true - kuangbin - 矩阵 传送门 A.CodeForces - 450B Jzzhu and Sequences 题意 水题,主要是拿来试试模板 题解 F[i]=f[i-1]-f[i-2] #include<bits/stdc++.h> using namespace std;…
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //2019.3.18 POJ 2251 Dungeon Master POJ 3278 Catch That Cow  //4.8 POJ 3279 Fliptile POJ 1426 Find The Multiple  //4.8 POJ 3126 Prime Path POJ 3087 Shuffle…
[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find The MultiplePOJ 3126 Prime PathPOJ 3087 Shuffle'm UpPOJ 3414 PotsFZU 2150 Fire GameUVA 11624 Fire!POJ 3984 迷宫问题HDU 1241 Oil Deposit…
专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find The MultiplePOJ 3126 Prime PathPOJ 3087 Shuffle'm UpPOJ 3414 PotsFZU 2150 Fire GameUVA 11624 Fire!POJ 3984 迷宫问题HDU 1241 Oil DepositsHDU 1495 非常可乐HDU 26…
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) ACM总结(fennec) 其实在北京比赛完的时候,我就想写了,不过还是早了点,直到上海比赛结束,大家的心中都不是太好受.郭老师有句话:你们这样做也是对的,不成功就成仁.让我的心也能安慰了不少. 我是从大一下学期开始接触ACM的,那时候我们学校才刚刚起步,siyee,wjiang师兄可以说是我的领路人了…
1: UVa 10887 - Concatenation of Languages map 可以做 ,但是输入实在恶心,有空串之类的HASH模板: int Hash(char *s){   int seed=131,sum=0;   while (*s)   sum=sum*seed+(*s++);   return (sum&0x7FFFFFFF)%N;} void hash_insert(int s){   int h=Hash(c[s]);   int u=head[h];   while…
Machine Learning 这是第一份机器学习笔记,创建于2019年7月26日,完成于2019年8月2日. 该笔记包括如下部分: 引言(Introduction) 单变量线性回归(Linear Regression with One Variable) 线性代数(Linear Algebra) 多变量线性回归(Linear Regression with Multiple Variables) Octave 逻辑回归(Logistic Regression) 正则化(Regularizat…
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2459 http://7xjob4.com1.z0.glb.clouddn.com/6892b750367614576602612088b7c161 题意:将给定n的01矩阵变为偶数矩阵 思路:枚举第一行的01情况,推出其他行的情况,判断是否符合,降复杂度为 O(2^n*n^2); #inc…
典型的两道矩阵快速幂求斐波那契数列 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…
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比如,如图1-6(a)所示的矩阵至少要把3个0变成1,最终如图1-6(b)所示,才能保证其为偶数矩阵. (a)                 (b) [输入格式] 输入的第一行为数据组数T(T≤30).每组数据的第一行为正整数n(1≤n≤15):接下来的n行每行包含n个非0即1的整数,相邻整数间用一…
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category=489&problem=4132&mosmsg=Submission+received+with+ID+13911770" target="_blank" style="">题目链接 题意:给定一个n格的环,如今有个距离d.每次变化把环…
http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem&problem=3143 矩阵变成一行,然后计算位置.lrj给了线段树数组做法 可是我做的线段树空间过大,直接爆掉,所以换方法了 主要还是測试自己的线段树区间更新的模板 各种RE+WA之后AC,,,,. 做的时候出现的几个错误: 1.行和列弄错 2.build初始化的时候,mmin mmax 都初始化为0才对…
UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞.∑kiAi=(1+Ak/2)∑k/2iAi,不断二分就可以 代码: #include <cstdio> #include <cstring> const int N = 45; int n, k; struct mat { int v[N][N]; mat() {memset(v, 0, sizeof(v));} mat operator * (ma…
UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d. 已知前d项求第n项 思路:矩阵高速幂,相应矩阵为 |a1 a2 a3 ... ad| |1 0 0 ... 0 0 0| |0 1 0 ... 0 0 0| |0 0 1 ... 0 0 0| |0 0 0 ... 0 0 0| |0 0 0 ... 1 0 0| |0 0 0…
题目链接: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] =…
题目链接:UVA - 10827 题意描述:给出一个n*n矩阵,把第一行和最后一行粘一起,把第一列和最后一列粘一起,形成一个环面,求出这个环面中最大的矩阵和. 算法分析:首先复制n*n这个矩阵,形成由4个这样小矩阵组成的大矩阵,然后在这个大矩阵里找出最大矩阵和,一看貌似和poj1050这道题有些相似,但是这道题数据大一些,O(150^4)应该会超时吧.我们可以这样想,先枚举这个最大和矩阵的左上角在大矩阵中的位置,然后枚举最大和矩阵的行数和列数,在枚举过程中处理最大和,然后更新答案即可. #inc…