数学--数论--HDU 2802 F(N) 公式推导或矩阵快速幂

Giving the N, can you tell me the answer of F(N)?
Input
Each test case contains a single integer N(1<=N<=10^9). The input is terminated by a set starting with N = 0. This set should not be processed.
Output
For each test case, output on a line the value of the F(N)%2009.
Sample Input
1
2
3
0
Sample Output
1
7
20
打了个表 4018一循环
#include <bits/stdc++.h>
using namespace std;
int f[6000];
int main()
{
f[1] = 1;
f[2] = 7;
for (int i = 3; i < 4020; i++)
{
f[i] = f[i - 2] + 3 * i * i - 3 * i + 1;
f[i] %= 2009;
}
int n;
while (scanf("%d", &n), n)
{
printf("%d\n", f[n % 4018]);
}
}
或者矩阵快速幂分奇数偶数
#include "bits/stdc++.h"
using namespace std;
const int MOD = 2009;
const int MAT[][4] = {
{1, 3, 3, 1},
{0, 1, 4, 4},
{0, 0, 1, 2},
{0, 0, 0, 1}
};
const int TABLE1[] = {1, 4, 2, 1};
const int TABLE2[] = {7, 9, 3, 1};
struct Mat {
int mat[4][4];
Mat() {
memset(mat, 0, sizeof(mat));
}
friend Mat operator * (Mat n, Mat m) {
Mat res;
for (int k = 0; k < 4; k++)
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
res.mat[i][j] = (res.mat[i][j] + n.mat[i][k] * m.mat[k][j]) % MOD;
return res;
}
} m;
Mat mat_pow(Mat n, int k) {
Mat res;
for (int i = 0; i < 4; i++) {
res.mat[i][i] = 1;
}
while (k) {
if (k & 1) {
res = res * n;
}
n = n * n;
k >>= 1;
}
return res;
}
int main() {
int n;
while (scanf("%d", &n) && n) {
if (n == 1) {
puts("1");
continue;
}
if (n == 2) {
puts("7");
continue;
}
memmove(m.mat, MAT, sizeof(m.mat));
m = mat_pow(m, n - 1 >> 1);
int res = 0;
if (n & 1) {
for (int i = 0; i < 4; i++) {
res = (res + m.mat[0][i] * TABLE1[i]) % MOD;
}
} else {
for (int i = 0; i < 4; i++) {
res = (res + m.mat[0][i] * TABLE2[i]) % MOD;
}
}
printf("%d\n", res);
}
return 0;
}
数学--数论--HDU 2802 F(N) 公式推导或矩阵快速幂的更多相关文章
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2855 斐波那契+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...
- HDU 5950:Recursive sequence(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...
- HDU 5171 GTY's birthday gift 矩阵快速幂
GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 3292 【佩尔方程求解 && 矩阵快速幂】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...
- HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)
M斐波那契数列 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...
- hdu 4565 So Easy! (共轭构造+矩阵快速幂)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
随机推荐
- C语言中 sinx cosx 的用法
#include<stdio.h> #include<math.h> int main() { double pi=acos(-1.0); double ang ...
- Django 配置访问顺序 ->MTV开发模式
框架模式mvc m-->model 数据库 v-->view 视图 c-->controller 控件逻辑 mtv(django) m-->model 数据库 t--> ...
- Go语言 可变参数
最近与同事讨论时,提到Go语言的可变参数,之前没有总结过相关知识点,今天我们介绍一下Go语言的可变参数. 可变参数(Variable Parameters):参数数量可变的函数称之为可变参数函数,主要 ...
- Java正则表达式基础知识及实例说明
众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学习及使用正则表达式,便成了解决这一矛 ...
- Github star 1.7k 的项目源码解析
先拜读源码,最后总结,以及其他实现思路.如有错误,欢迎指正! 项目介绍 名称:Darkmode.js 功能:给你的网站添加暗色模式 项目链接:https://github.com/sandoche/D ...
- Linux-设备-磁盘
磁盘的每个扇区为512bytes.磁盘的第一个扇区记录了整块磁盘的重要信息,包含有主引导分区(MBR):可以安装引导加载程序的地方,有446bytes:分区表(partition table):记录整 ...
- Docker常用命令--ps/attach/run
ps查看container 若查看正在运行的container docker ps 查看所有的container docker ps -a run启动容器 第一次启动container docker ...
- 前后端分离下用jwt做用户认证
0 前后端分离下的用户信息认证 前端使用Vue+axios,后端使用SpringBoot+SpringSecurity. 为了解决http无状态的问题,我采用jwt(json web token)保存 ...
- 【题解】P3349 [ZJOI2016]小星星 - 子集dp - 容斥
P3349 [ZJOI2016]小星星 声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 题目描述 小 \(Y\) 是一个心灵手巧 ...
- Java标识符中常见的命名规则
标识符:就是给类,接口,方法,变量等起名字.组成规则:A:英文字母大小写B:数字字符C:$和_注意事项:A:不能以数字开头B:不能使Java中的关键字C:Java语言严格区分大小写常见的命名规则:见名 ...