矩阵快速幂和几个应用

2023-05-16

模板:

#define maxn 1005
struct Matrix 
{
    int m[maxn][maxn];
}a,b,ans,res;
void init()
{
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= n; j ++)
        {
            if(i==j)
                a.m[i][j] = 1;
            else
                a.m[i][j] = 0;
        }
    }
}
Matrix mul(Matrix a,Matrix b)
{
    Matrix c;
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= n; j ++)
        {
            c.m[0][0] = 0;
            for(int k = 1; k <= n; k ++)
                c.m[i][j] += a.m[i][k]*b.m[k][j];
        }
    }
    return c;
}
Matrix q_pow(Matrix a,int b)
{
    res = a;
    while(b)
    {
        if(b&1)
            res = mul(res,a);
        a = mul(a,a);
        b >>= 1;
    }
    return res;
}

Fibonacci

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 alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

Output

For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

Sample Input


0
9
999999999
1000000000
-1  

Sample Output


0  

题意:求斐波那契数列的第n项

解析:利用矩阵快速幂,因为题意给出了

即求这个矩阵的n次幂,然后再输出矩阵的第一列第二个就行啦~

#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<set>
#include<sstream>
#include<queue>
#include<cstdio>
#include<stack>
#include<cmath>
using namespace std;
const int MOD = 10000;
struct Matrix{
	int a[12][12];
};

Matrix Init(){//初始化初始矩阵
	Matrix a;
	a.a[0][0] = 1;
	a.a[0][1] = 1;
	a.a[1][0] = 1;
	a.a[1][1] = 0;
	return a;
}

Matrix Mul(Matrix a, Matrix b, int n){//矩阵乘法
	Matrix c;
	for( int i=0; i<n;i++)  {
        for( int j=0; j<n;j++)  {
            c.a[i][j]=0;
            for( int k=0; k<n;k++)  {
            	c.a[i][j] += a.a[i][k]*b.a[k][j];
            }
            c.a[i][j] %= MOD;
        }
    }
	return c;
}

Matrix Pow(Matrix a, int k,int n){
	Matrix r = Init(),ba = a;
	while(k){
		if(k%2 == 1)
			r = Mul(r,ba,n);
		ba = Mul(ba,ba,n);
		k/=2;
	}
	return r;
}

int main(){
	int k;
	while(scanf("%d",&k) != EOF && k!=-1){
		if(k == 0){
			printf("0\n");
			continue;
		}
		Matrix a = Init();
		Matrix res = Pow(a,k-1,2);
		printf("%d\n",res.a[0][1]%MOD);
	}
	return 0;
}

HDU-5015 233 Matrix

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333...) Besides, in 233 matrix, we got a i,j = a i-1,j +a i,j-1( i,j ≠ 0). Now you have known a 1,0,a 2,0,...,a n,0, could you tell me a n,m in the 233 matrix?

Input

There are multiple test cases. Please process till EOF.

For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).

Output

For each case, output a n,m mod 10000007.

Sample Input


1 1
1
2 2
0 0
3 7
23 47 16  

Sample Output


234
2799
72937

题意:给出一个矩阵的第一行第一列,求出第n行第m列,矩阵中元素有递推式a[i][j] = a[i-1][j]+a[i][j-1]。其中第一行为0,233,2333,23333...,第一列为0,x1,x2,x3...,xi在输入中给出。n<=10,m<=10^9
  

解析:

根据n = 3,m = 3可以构造一个矩阵


233                         10 0 0 0 1       23
x1+233                   10 1 0 0 1       x1
x1+x2+233      =     10 1 1 0 1   *   x2
x1+x2+x3+233       10 1 1 1 1       x3
3                             0  0 0 0 0        3
 

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct matrix
{
	__int64 mat[15][15];

	matrix()
	{
		memset(mat, 0, sizeof(mat));
	}
};
int n;
const int mod = 10000007;
matrix mul(matrix B, matrix A)         //左乘列矩阵
{
	int i, j, k;
	matrix C;
	for (i = 1; i <= n + 2; i++)
		for (j = 1; j <= n + 2; j++)
			for (k = 1; k <= n + 2; k++)
				C.mat[i][j] = (C.mat[i][j] + B.mat[i][k] * A.mat[k][j]) % mod;
	return C;
}
matrix powmul(matrix A, int m)
{
	matrix ans;
	for (int i = 1; i <= n + 2; i++)
		ans.mat[i][i] = 1;
	while (m > 0)
	{
		if (m & 1)
			ans = mul(ans, A);
		A = mul(A, A);
		m >>= 1;
	}
	return ans;
}
int main()
{
	int m;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		matrix A, B;
		A.mat[1][1] = 23;
		for (int i = 1; i <= n; i++)
			scanf("%d", &A.mat[i + 1][1]);
		A.mat[n + 2][1] = 3;

		for (int i = 1; i <= n + 1; i++)
			B.mat[i][1] = 10;
		for (int i = 1; i <= n + 2; i++)
			B.mat[i][n + 2] = 1;
		for (int i = 1; i < n + 2; i++)
			for (int j = 2; j <= i; j++)
				B.mat[i][j] = 1;

		B = powmul(B, m);
		A = mul(B, A);

		cout << A.mat[n + 1][1] << endl;
	}

	return 0;
}

ZOJ-2974 Just Pour the Water

 

Shirly is a very clever girl. Now she has two containers (A and B), each with some water. Every minute, she pours half of the water in A into B, and simultaneous pours half of the water in B into A. As the pouring continues, she finds it is very easy to calculate the amount of water in A and B at any time. It is really an easy job :).

But now Shirly wants to know how to calculate the amount of water in each container if there are more than two containers. Then the problem becomes challenging.

Now Shirly has N (2 <= N <= 20) containers (numbered from 1 to N). Every minute, each container is supposed to pour water into another K containers (K may vary for different containers). Then the water will be evenly divided into K portions and accordingly poured into anther K containers. Now the question is: how much water exists in each container at some specified time?

For example, container 1 is specified to pour its water into container 1, 2, 3. Then in every minute, container 1 will pour its 1/3 of its water into container 1, 2, 3 separately (actually, 1/3 is poured back to itself, this is allowed by the rule of the game).

 

Input

 

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. And it will be followed by T consecutive test cases.

Each test case starts with a line containing an integer N, the number of containers. The second line contains N floating numbers, denoting the initial water in each container. The following N lines describe the relations that one container(from 1 to N) will pour water into the others. Each line starts with an integer K (0 <= K <= N) followed by K integers. Each integer ([1, N]) represents a container that should pour water into by the current container. The last line is an integer M (1<= M <= 1,000,000,000) denoting the pouring will continue for M minutes.

 

Output

 

For each test case, output contains N floating numbers to two decimal places, the amount of water remaining in each container after the pouring in one line separated by one space. There is no space at the end of the line.

 

Sample Input

1

2


100.00 100.00
1 2
2 1 2
2
  

 

 

Sample Output

 

 


75.00 125.00
  

Note: the capacity of the container is not limited and all the pouring at every minute is processed at the same time.

题意: 给出 T 组样例,再给出 N 个水杯,后给出 N 个水杯的容量,后给出 N 行,首先第一行是 K,代表要讲这个杯里面的水要分给一下 K 个水杯,后给出 K 个水杯的编号。输出 M 时间后各个水杯剩下的水容量。输出两位小数。

解析: 矩阵快速幂。构成一个 N X N 的矩阵,对这个矩阵就 M 次方最后乘以 N X 1 的矩阵,输出这个 N X 1 的矩阵即为答案。

#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 100
struct Matrix{
    double m[maxn][maxn];
}p,ans,a,b;
int n;
void init()
{
    fill(p.m[0],p.m[maxn],0);
    for(int i = 0; i < maxn; i ++)
    {
        p.m[i][i] = 1;
    }
}
Matrix mul(Matrix a,Matrix b)
{
    Matrix c;
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < n; j ++)
        {
            c.m[i][j] = 0;
            for(int k = 0; k < n; k ++)
                c.m[i][j] += a.m[i][k] * b.m[k][j];
        }
    }
    return c;
}
Matrix q_pow(Matrix a,long long int b)
{
    Matrix ans = p;
    while(b)
    {
        if(b&1)
            ans = mul(ans,a);
        a = mul(a,a);
        b>>=1;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    init();
    while(t--)
    {
        scanf("%d",&n);
        fill(a.m[0],a.m[n],0);
        for(int i = 0; i < n; i ++)
            scanf("%lf",&a.m[0][i]);
        fill(b.m[0],b.m[n],0);
        for(int i = 0; i < n; i ++)
        {
            int k;
            scanf("%d",&k);
            if(!k)
                b.m[i][i] = 1;
            for(int j = 0; j < k; j ++)
            {
                int x;
                scanf("%d",&x);
                b.m[i][x-1] = 1.0/k;
            }
        }
        long long int m;
        scanf("%lld",&m);
        b = q_pow(b,m);
        ans = mul(a,b);
        printf("%.2f",ans.m[0][0]);
        for(int i = 1; i < n; i ++)
            printf(" %.2f",ans.m[0][i]);
        printf("\n");
    }
    return 0;
}


 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

矩阵快速幂和几个应用 的相关文章

  • 读书笔记-深度学习推荐系统4-推荐与embedding

    本篇结合了书籍 深度学习推荐系统 和吴恩达老师的视频课程 Natural Language Processing and Word Embeddings embedding技术是深度学习的一种基础核心操作 xff0c 有很多的应用场景 1
  • VNC 密码修改遇到的问题

    记一下今天在修改vnc密码的时候 xff0c 使用vncpasswd xff0c 修改完死活不成功 xff0c 后来切换到对应的账号再修改就成功了
  • 我的2014

    2014 xff0c 不 xff0c 应该是先说说现在的2015吧 xff0c 2015年1月18号 xff0c 我刚刚注册了CSDN的博客账号 xff0c 相对来说 xff0c 我是个新手 xff0c 其实以前都没有写博客的习惯 xff0
  • 平衡车+速度/位置pid+野火上位机移植+Freertos+cubemx(一)

    平衡小车 43 野火pid上位机移植 程序源码已经上传 xff0c 需要的可以下载 一 首先下载STM32CUBEMX 二 配置相关单片机和相关功能 1 配置时钟和debug引脚2 开启freertos3 相关功能以及引脚的配置这里使用的相
  • 百度笔试题2

    一 xff0c 简答题 30分 1 xff0c 当前计算机系统一般会采用层次结构存储数据 xff0c 请介绍下典型计算机存储系统一般分为哪几个层次 xff0c 为什么采用分层存储数据能有效提高程序的执行效率 xff1f xff08 10分
  • 2014华为机试题目

    1 输入摸一个数 xff0c 然后将其倒过来相加 xff0c 如果和不是回文串 xff0c 那么将和再采取同样的操作 xff0c 在判断得到的是否为回文串 xff0c 这样往返7次 xff0c 如果其中有一次是回文就直接打出来 xff0c
  • 2014小米,百度,pptv,去哪儿笔试题目回忆

    今天一共笔试了这四家 xff0c 真累啊 xff0c 上午10点小米 xff0c 下午2点百度 xff0c 下午3点PPTV xff0c 下午5点去哪儿 xff0c 今天右手太酸了 xff0c 打的都话了50左右 xff0c 如果没面试通知
  • 质数因子

    功能 输入一个正整数 xff0c 按照从小到大的顺序输出它的所有质数的因子 xff08 如180的质数因子为2 2 3 3 5 xff09 思路 xff1a 传统的思维是从2到n遍历一遍 xff08 稍微优化一下可以到根号n xff09 x
  • OVS于DVS

    撰写时间 xff1a 2022 2 28 分布式虚拟交换机 xff08 DVS 注意 xff1a DVS是二层交换机 DVS特点 xff1a 1 集中管理 xff1a 通过统一的Portal页面进行集中管理 xff0c 简化用户配置 2 基
  • 如何同时使用maven-replacer-plugin和maven-assembly-plugin插件

    页面css和js缓存是前端常见的问题 xff0c maven有专门的插件maven assembly plugin可以处理 参考https blog csdn net weixin 34336292 article details 9197
  • Ubuntu上两台服务器利用nfs实现共享文件夹

    碰到的一个问题是 xff0c 一台服务器A放不下所有的数据 xff0c 部分数据只能放到另一台服务器B上 xff0c 那么就涉及到如何把服务器B上的数据共享给服务器A xff0c 使得A可以看到B上的内容 xff0c 需要用的是nfs文件共
  • Unbuntu16.04 虚拟机 安装win7以及文件共享

    KVM虚拟机的模版导出 xff0c 通常都是直接用qemu img命令可以将默认的raw格式或者qcow2格式的磁盘文件压缩后导出 xff0c 指令如下 xff1a 将默认raw格式的磁盘 xff0c 简单压缩转换成qcow2格式 qemu
  • 报错:RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED

    一般显存溢出报out of memory之类 xff0c 修改了代码中batch size大小 xff08 忘记自己已经配置过默认参数 xff09 未解决 所以便认为是cuda配置问题 xff0c 多方检查确认cuda cudnn配置无误
  • js delete删除key

    var a 61 a a 61 1 a b 61 2 delete a 34 a 34 console log a b 2 delete a b console log a js 的delete可以根据key删除对象中的元素
  • 2014跌跌撞撞--伴我成长

    2014跌跌撞撞 伴我成长 上眼皮是正月 xff0c 下眼皮是腊月 xff0c 一转眼一年就过去了 没有轰轰烈烈 xff0c 也不是平淡无奇 xff0c 或许应该说是跌跌撞撞地走过来 叶子不断地从生命之树飘落 xff0c 不知不觉中岁月已在
  • stm32f103rb升级到stm32f103rc时代码移植注意事项

    1 由于stm32f103RC RD RE系列单片机芯片级的bug xff0c 代码中用到重映射相关函数的地方 xff0c 在其后面添加 HAL AFIO REMAP SWJ NOJTAG 语句 xff0c 如下所示 xff1a HAL A
  • OpenFlow所面临的挑战与创新方案

    1 OpenFlow控制面的挑战 2 OpenFlow转发面的挑战 3 芯片厂商的犹豫 一 OpenFlow控制面的挑战 OpenFlow在控制面存在以下不足 xff1a 1 master和slavecontroller的选举机制不够成熟
  • apt-get软件管理工具(软件安装、重装、卸载)

    apt get软件管理工具 下面讲解 xff0c linux系统下如何进行软件的管理 xff0c 包括软件的索引安装 更新 卸载删除 本地存储介中软件的安装 系统升级等操作 更多优质文章 xff0c 请访问博主个人网站 xff1a www
  • Ubuntu 系统下如何安装pip3工具

    一 导读 Ubuntu 系统内置了 Python2 和 Python3 两个版本的开发环境 xff0c 却没有内置相应的 pip3 管理工具 xff0c 本文将介绍如何在Ubuntu下如何快速安装 pip3 工具 xff0c 并升级到最新可
  • 包编译卡住的终极解决办法

    在数据库开发过程中 xff0c 经常遇到一个很烦躁的现象 xff1a 刚修改好的包一编译就卡死了 xff0c PL SQL变成一片空白 xff0c 又不忍心关闭 xff0c 这可是耗死多少脑细胞才写出来的 xff01 xff01 xff01

随机推荐

  • 正则表达式3,\A,\Z,\b,\B,\d,\D,\s,\S,\w.\W,re.compile

    1701H1 穆晨 180201 第114天总结 我爱学习 xff0c 学习使我快乐 A匹配输入字符串的开始位置 Z匹配输入字符串的结束位置 xff08 脱字符 xff09 匹配输入字符串的开始位置 xff0c 如果设置了re MULTIL
  • L1正则为什么更容易获得稀疏解

    L1和L2正则常被用来解决过拟合问题 而L1正则也常被用来进行特征选择 xff0c 主要原因在于L1正则化会使得较多的参数为0 xff0c 从而产生稀疏解 xff0c 将0对应的特征遗弃 xff0c 进而用来选择特征 但为什么L1正则会产生
  • VQA在CLEVR上的简单实现

    前言 Visual Question Answering是多模态学习的一个领域 xff0c 模型通过结合图像与问题 xff0c 推理出正确的答案 xff0c 由于问题问的是图像中出现物品的方位 xff0c 大小 xff0c 形状等等 xff
  • 对比学习损失篇,从L-softmax到AM-softmax到circle-loss到PCCL

    前言 对比学习是一种比较学习 xff0c 其主要目的为让模型学习到不同类别之间的特征 xff0c 其被广泛应用于人脸识别 xff0c 文本检索 xff0c 图像分类等领域 对比学习的主要思想是增大不同类别间的距离 xff0c 缩小相同类别间
  • 【ASP.NET】-Cookie、Session与Token机制

    前几天学习Session与cookie的时候想起来有一次技术分享时候 xff0c 提到了Token机制 xff0c 心里想着他们都是状态保持机制 xff0c 有什么关系和区别呢 xff0c 今天查了下简单有个认识 xff1b Cookie
  • layui 引入第三方插件 day.js

    layui模块化的规范 测试用例 xff1a test js layui define function exports var obj 61 method2 function a b console log 34 调用方法method2
  • 【Caffe学习笔记】一 、环境安装 Caffe + cuda + windows10 + VS2015 安装笔记, win7也适用

    1 下载cuda8 0 cudnn5 anaconda https developer nvidia com cuda 80 ga2 download archive https developer nvidia com cudnn htt
  • Windows中公用网络与专用网络的区别

    当我们第一次打开一个Windows网络应用程序时 xff0c 会弹出选择网络类型 xff1a 专用网络 xff0c 公用网络 这个的确令人费解 xff0c 相信很多人都不知所措过 有的人干脆都选上 xff0c 这样就避免了被防火墙挡住 这里
  • 针对Android MediaCodec解码延时问题的替代解决方案

    如题 xff0c 本人在jni层实现了avc hevc的解码 xff0c 避免了在java上层调用系统的MediaCodec解码出现的延时问题 xff0c 完美支持1080P xff0c 4K xff08 具体看手机性能 xff09 xff
  • 系统环境变量path的列表不见了

    如题 xff0c 在编辑系统环境变量时 xff0c 发现path的环境变量原先是列表显示的 xff0c 看起来比较清晰 xff0c 而现在变成了一个文本框了 xff0c 就不那么一目了然了 于是在网上找到下面这个文章 xff0c 能很好解决
  • 禁用小米手机系统应用的方法

    有时候我们想要删除对我们无用的已安装的应用 xff0c 长按应用图标 xff0c 进行卸载即可 但这个只对非系统应用有效 xff0c 如果是系统内建的应用 xff0c 就无法卸载 xff0c 这是很令人恼火的 但通过pm命令可以禁用它 xf
  • gl的矩阵模式及其相应的矩阵变换函数

    以Android的GL10为例 xff0c 说明一下矩阵模式及其相应的矩阵变换函数 矩阵模式一共分为两种 xff1a gl glMatrixMode GL10 GL MODELVIEW 和 gl glMatrixMode GL10 GL P
  • 阿里云轻量应用服务器使用教程远程连接、开端口和操作系统修改方法

    阿里云轻量应用服务器怎么用 xff1f 轻量服务器相对于云服务器ECS使用更简单 xff0c 轻量服务器远程连接 搭建网站 开放端口等详细操作流程 xff0c 阿里云百科来详细说下阿里云轻量应用服务器使用教程 xff1a 阿里云轻量应用服务
  • 对md5sum程序的修改

    linux下自带md5sum工具 xff0c 可以对文件计算md5值 xff0c 但这个命令行工具不能直接对字符串求md5 xff0c 而对一个字符串求md5是一个比较有用的需求 xff0c 比如计算签名 于是对源码md5sum c修改了一
  • 门电路逻辑符号大全(三态门,同或门,异或门,或非门,与或非门, 传输门,全加器,半加器等)

    最近要研究一下滤波器设计的无乘法器的实现 xff0c 所以要学习一下加法器的电路 xff0c 丢了一段时间 xff0c 忘的差不多了 xff0c 这里罗列一下常用的门电路的符号 这是一个1位全加器的数字电路组成 xff1a 以下两幅图可以复
  • 实函数傅里叶变换的奇偶虚实特性

    本文内容来源于他人的PPT xff0c 经本人整理而成 xff0c 算是对数字信号处理的复习吧 而实偶函数的傅里叶变换仍然是一个实偶函数的性质正是DCT的基础 xfeff xfeff
  • 多面体及欧拉公式及广义欧拉公式

    像正方体 xff0c 四棱锥这样的平面多面体属于简单多面体 xff0c 它们可以与球拓扑同构 xff0c 即可以连续拓扑变换成一个球 它们满足欧拉公式 xff1a v e 43 f 61 2 其中v是顶点 xff08 vertex xff0
  • mysql在表的某一位置增加一列的命令

    如果想在一个已经建好的表中添加一列 xff0c 可以用诸如 xff1a alter table t1 add column addr varchar 20 not null 这条语句会向已有的表t1中加入一列addr xff0c 这一列在表
  • tar命令中的-C作用

    tar xzvf abc tar gz C tmp 上面的命令将abc tar gz这个压缩包解压到当前目录下的tmp目录下 xff0c 而不是当前目录下 xff0c 这就是 C选项的作用
  • 矩阵快速幂和几个应用

    模板 xff1a define maxn 1005 struct Matrix int m maxn maxn a b ans res void init for int i 61 1 i lt 61 n i 43 43 for int j