UVA - 10870 Recurrences 【矩阵快速幂】
2024-08-29 20:33:17
题目链接
https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553
题意
给出一个表达式 算法 f(n)
思路
n 很大 自然想到是 矩阵快速幂
那么问题就是 怎么构造矩阵
我们想到的一种构造方法是
n = 2 时
n = 3 时
然后大概就能够发现规律了吧 。。
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
#define syn_close ios::sync_with_stdio(false);cin.tie(0);
#define sp system("pause");
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 10;
const int MOD = 142857;
int d, n, m;
ll a[20], b[20];
struct Matrix
{
ll a[20][20];
Matrix() {}
Matrix operator * (Matrix const &b)const
{
Matrix res;
CLR(res.a, 0);
for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++)
for (int k = 0; k < d; k++)
res.a[i][j] = (res.a[i][j] + this->a[i][k] * b.a[k][j]) % m;
return res;
}
};
Matrix pow_mod(Matrix ans, int n)
{
Matrix base;
CLR(base.a, 0);
for (int i = 0; i < d; ++i)
{
base.a[i][0] = a[i];
}
for (int i = 0; i < d; ++i)
{
base.a[i][i + 1] = 1;
}
while (n > 0)
{
if (n & 1)
ans = ans * base;
base = base * base;
n >>= 1;
}
return ans;
}
int main()
{
while (scanf("%d %d %d", &d, &n, &m) && (d || n || m))
{
for (int i = 0; i < d; i++)
scanf("%lld", &a[i]);
for (int i = 0; i < d; i++)
scanf("%lld", &b[i]);
if (n <= d)
{
printf("%lld\n", b[n - 1] % m);
continue;
}
Matrix ans;
for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++)
ans.a[i][j] = b[d - j - 1];
ans = pow_mod(ans, n - d);
printf("%lld\n", ans.a[0][0]);
}
return 0;
}
最新文章
- H-1B身份六年后的延期问题
- 多语言架构下如何正确的使用SQL视图
- Codeforces 389B(十字模拟)
- retire or not retire ? is a question.
- jquery的.detach()方法
- poj2125Destroying The Graph(最小割+输出方案)
- linux系统下,警告:warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] 和 warning: the `gets&#39; function is dangerous and should not be used. 的由来和解决方法。
- hdu2262 Where is the canteen
- Linux实战案例(3)创建和删除用户
- 使用foreach需要判空。
- Python 防止mysql 注入的两种方式
- 使用基本MVC2模式创建新闻网站
- SpringBoot 多数据源分布式事务
- Azure CosmosDB (4) 在一致性(Consistency)可用性(Availability)和性能(Performance)之间的权衡
- Nginx 防盗链 secure_link 模块
- 盘点 React 16.0 ~ 16.5 主要更新及其应用
- SpringBoot2.x配置文件讲解
- mybatis例子
- hdu2069-2071
- 利用HBuilder打包前端开发webapp为apk
热门文章
- Android Shader 颜色、图像渲染 paint.setXfermode
- 【Excle数据透视表】如何按照地区交替填充背景颜色
- 01-2制作U盘启动盘--装机助理工具
- Android分享功能的一点总结
- springboot学习(二) 第一个springboot项目:Hello World!
- 常见Linux/Unix开发辅助命令什锦
- 在Less中使用条件判断
- NGINX不允许向静态文件提交POST方式的请求,否则报405错误(apache中没有出现)
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)
- 快速用CMD打开当前目录