poj 3070 && nyoj 148 矩阵快速幂
poj 3070 && nyoj 148 矩阵快速幂
题目链接
poj: http://poj.org/problem?id=3070
nyoj: http://acm.nyist.net/JudgeOnline/problem.php?pid=148
思路:
- 矩阵快速幂
- 直接求取

代码:
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int mod = 10000;
struct mat {
int a[2][2];
mat() {
a[0][1]=a[1][0]=1;
a[1][1]=a[0][0]=0;
}
};
mat qpow(mat& x, mat& y) {
mat z;
z.a[0][1]=z.a[1][0]=0;
for(int i=0;i<=1;++i) {
for(int j=0;j<=1;++j) {
for(int k=0;k<=1;++k) {
z.a[i][j]=(z.a[i][j]%mod+(x.a[i][k]%mod*y.a[k][j])%mod)%mod;
}
}
}
return z;
}
int slove(int n) {
mat x,res;
res.a[0][1]=res.a[1][0]=0;
res.a[1][1]=res.a[0][0]=1;
x.a[0][0]=1;
while(n) {
if(n&1) res=qpow(res,x);//二进制分解
x=qpow(x,x);
n>>=1;
}
return res.a[0][1];
}
int main() {
int n;
while(~scanf("%d",&n)&&n!=-1) {
printf("%d\n",slove(n));
}
return 0;
}
poj 3070 && nyoj 148 矩阵快速幂的更多相关文章
- poj 3070 Fibonacci(矩阵快速幂,简单)
题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂
It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...
- poj 2778 AC自动机+矩阵快速幂
题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...
- Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)
题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...
- POJ 3734 Blocks (矩阵快速幂)
题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...
- POJ 3734 Blocks(矩阵快速幂+矩阵递推式)
题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 . 设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...
- POJ 2778 DNA Sequence (矩阵快速幂 + AC自动鸡)
题目:传送门 题意: 给你m个病毒串,只由(A.G.T.C) 组成, 问你生成一个长度为 n 的 只由 A.C.T.G 构成的,不包含病毒串的序列的方案数. 解: 对 m 个病毒串,建 AC 自动机, ...
随机推荐
- LeetCode 661. Image Smoother (图像平滑器)
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- 前端性能优化jQuery性能优化
一.使用合适的选择器 $("#id"); 1.使用id来定位DOM元素无疑是最佳提高性能的方式,因为jQuery底层将直接调用本地方法document.getElementById ...
- Python - 单步调试
Python 有一个单步调试器模块,能实现基本的调试效果!详情请看Python标准文档说明:https://docs.python.org/2/library/pdb.html 调试例子: >& ...
- Oil Deposits
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- html-webpack-plugin的使用
使用前第一步:npm install 安装html-webpack-plugin --save--dev || --save (tips:--save--dev跟--save最大的区别就是--dev ...
- 如何成为一个javascript高手【转载】
原文网址: http://www.cnblogs.com/keva/p/how-to-become-a-javascript-badass.html 英文网址:http://www.clientc ...
- HTML学习笔记 cs2D3D展示基础 第十四节 (原创) 参考使用表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SQLServer2008数据库安装图解
SQLServer2008数据库安装图解... ======================================= 解压下载的安装包,右键运行Setup.exe文件 =========== ...
- unity下跨平台excel读写
这是以前写的跨windows和ios读写excel的工具,因为原来导表工具引用的第三方读写excel的dll只能在windos下使用,造成要在mac机器上跑PC端或者打包的时候,每次都要先在windo ...
- 特征提取算法的综合实验(多种角度比较sift/surf/brisk/orb/akze)
一.基本概念: 作用:特征点提取在"目标识别.图像拼接.运动跟踪.图像检索.自动定位"等研究中起着重要作用: 主要算法: •FAST ,Machine Learning forHi ...