poj 3735Training little cats(矩阵快速幂 完成模拟 注意优化)

poj 3735 Training little cats 构造矩阵+稀疏矩阵加速连乘+矩阵快速幂 poj 3735 Training little cats 构造矩阵+稀疏矩阵加速连乘 题目链接:http://poj.org/problem?id=3735 题面描述: Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13366 阅读详情
Training little cats
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12392 Accepted: 3049

Description

Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make the cats to do some exercises. Facer has well designed a set of moves for his cats. He is now asking you to supervise the cats to do his exercises. Facer's great exercise for cats contains three different moves:
g i : Let the ith cat take a peanut.
e i : Let the ith cat eat all peanuts it have.
s i j : Let the ith cat and jth cat exchange their peanuts.
All the cats perform a sequence of these moves and must repeat it m times! Poor cats! Only Facer can come up with such embarrassing idea. 
You have to determine the final number of peanuts each cat have, and directly give them the exact quantity in order to save them.

Input

The input file consists of multiple test cases, ending with three zeroes "0 0 0". For each test case, three integers n, m and k are given firstly, where n is the number of cats and k is the length of the move sequence. The following k lines describe the sequence.
(m≤1,000,000,000, n≤100, k≤100)

Output

For each test case, output n numbers in a single line, representing the numbers of peanuts the cats have.

Sample Input

3 1 6
g 1
g 2
g 2
s 1 2
g 3
e 2
0 0 0

Sample Output

2 0 1

Source


/*
难点在与怎么将这三种操作转换为矩阵的操作,以题目上的测试数据为例,定义表示猫咪的花生的向量A:

1 0 0 0
A[id]表示第id只猫的初始花生数量,初始时猫咪没有花生,所以都是0。当然,首个元素固定是1,这是为了做乘法用的。

定义单位初始方阵T:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
定义 R = A * T,那么R[id]表示第id只猫的最终花生数量。上面向量A那个突兀的1就是为了取矩阵T第1行各个值的。

那么

g i   := ++T[0][i]

e i   := set T[i][i] = 0 即将矩阵的元素(i,i)置为0

s i j := swap(T[t][i], T[t][j]) (0<=t<=height) 即将矩阵的i列和j列互换。

T执行m次,结果R = A * Tm 。轮到矩阵的幂出场了,将上次的模板改改即可:
*/
 
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
typedef long long ll;
using namespace std;
struct matrix{
	ll mat[150][150];
	matrix()
	{
		memset(mat,0,sizeof(mat));
	}
};
matrix ret,d;
ll n,m,k;
matrix mul(matrix a,matrix b)
{
	memset(ret.mat,0,sizeof(ret.mat));
	for(int i=0;i<=n;i++)
	{
	
		for(int k=0;k<=n;k++)//优化 防止超时 
		if(a.mat[i][k])
		{
		for(int j=0;j<=n;j++)
		{
			ret.mat[i][j]+=a.mat[i][k]*b.mat[k][j];	
		}	
		}
	}
	return ret;
}
matrix ks(matrix a,long long k)
{
	
	if(k==1)
	return a;
	matrix e;
	for(int i=0;i<=n;i++)
	{
		e.mat[i][i]=1;
	}
	if(k==0)
	return e;
	while(k)
	{
		if(k&1)
		{
			e=mul(e,a);
		}
		a=mul(a,a);
		k>>=1;
	}
	return e;
}
int main()
{
	while(~scanf("%lld%lld%lld",&n,&m,&k))
	{
		if(!n&&!m&&!k)break;
		ll a,b;
		memset(d.mat,0,sizeof(d.mat));
		for(int i=0;i<=n;i++)
		d.mat[i][i]=1;
		char op[5];
		while(k--)
		{
			scanf("%s",op);
			if(op[0]=='g')//  第一行表示每只猫的豆豆数  当输入g的时候a猫的豆豆加一  当输入e时 把a猫的所有豆豆清空  交换时把两列的数据全部交换 
			{
				scanf("%lld",&a);
				d.mat[0][a]++;
			}
			else if(op[0]=='e')
			{
				scanf("%lld",&a);
				for(int i=0;i<=n;i++)
				d.mat[i][a]=0;
			}
			else {
				scanf("%lld%lld",&a,&b);
				for(int i=0;i<=n;i++)
				{
					ll t=d.mat[i][a];
					d.mat[i][a]=d.mat[i][b];
					d.mat[i][b]=t;
				}
			}
		}
		matrix ans=ks(d,m);
		printf("%lld",ans.mat[0][1]);
		for(int i=2;i<=n;i++)
		{
			printf(" %lld",ans.mat[0][i]);
		}
		printf("\n");
	}
	return 0;
} 


DESeq2实战:如何用LRT检验分析基因表达随时间变化的模式(附完整代码) 本文详细介绍了如何使用DESeq2中的LRT检验分析基因表达随时间变化的模式,包括实验设计、数据预处理、模型构建、结果提取与可视化等完整流程。通过实战案例和完整代码,帮助生物信息学研究者掌握时间序列RNA-seq分析的核心技术,特别适合需要快速上手的实践者。 阅读详情

相关推荐

LaTeX公式转Word竟这么简单?Python三行代码实现学术论文格式无忧

本文介绍如何利用Python的latex2word库,仅需三行代码即可将LaTeX数学公式精准转换为Word原生Office Math对象,解决学术写作中LaTeX与Word格式割裂的难题。该方法支持批量处理、格式保真,并能与Markdown、Jupyter Notebook等工具集成,构建自动化写作流程,实现学术论文格式无忧。

qsc90123456的博客 298

POJ 3735 E - Training little cats POJ - 3735 矩阵构造

Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make the cats to do some exercises. Facer has well designed a set of move

Marcus-Bao的个人主页 699

Ubuntu20.04/Debian10安装向日葵远程控制踩坑实录(附依赖问题解决方案)

本文详细介绍了在Ubuntu 20.04/Debian 10系统上安装向日葵远程控制软件的全过程,重点解决了常见的依赖问题,特别是libwebkitgtk相关错误。通过自动修复、手动修改.deb包依赖和使用兼容层三种解决方案,帮助用户顺利完成安装并优化远程控制体验。

silver的专栏 754

POJ 3735 Training little cats (矩阵快速幂)

POJ 3735 Training little cats (矩阵快速幂)

Tc的专栏 1938

别再只当代码阅读器了!手把手教你用Understand的实体定位器和信息浏览器,5分钟精准定位代码逻辑

本文深入解析Understand代码分析工具的高级功能,重点介绍实体定位器和信息浏览器的实战应用。通过5分钟快速定位代码逻辑的技巧,帮助开发者从代码阅读者升级为逻辑侦探,提升大型代码库的分析效率。文章包含过滤搜索、调用链追踪等实用方法,是掌握Understand核心功能的必备指南。

weixin_28745821的博客 294

矩阵快速幂

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9785   Accepted: 2340 Description Facer's pet cat just gave birth to a brood of little c

袁汉春 1295

智慧加油站小程序数据库设计文档

【代码】智慧加油站小程序数据库设计文档。

低代码布道师的博客 1406

POJ3735 Training little cats - 矩阵快速幂 -稀疏矩阵乘法优化

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16336   Accepted: 3998 Description Facer’s pet cat just gave birth to a brood of little cats. Having conside...

太阳升起 504

① Django手把手从零开发个人博客-前言

手把手教你从零开始玩转手把手教你从零开始玩转Django,全面学习并了解django的运行机制、开发全流程及线上部署。Django,全面学习并了解django的运行机制、开发全流程及线上部署。 专栏介绍 本套教程遵循Django的设计思想,开门见山,快速切入主题,拒绝拖沓,在实战中学习每一个知识点,提高学习的趣味性以及个人成就感,并且能快速将所学运用到实际项目中。 将从零手把手带着你一步步完成博客的每一个功能,通过实现博客文章列表页、文章详情页、作者页面等功能来学习和了解django的视图查询模块相关知.

轻编程的博客 515

数字盲水印嵌入和提取的实现基于小波变换DWT算法

数字盲水印是指在不知道原始图像的情况下,在其上嵌入水印并在后续的提取过程中能够恢复出原始水印的一种技术。本文介绍的数字盲水印嵌入和提取的实现方法基于小波变换DWT算法。具体来讲,DWT将信号或图像进行二次下采样和卷积滤波,使得高频分量的图像信息被集中在一个小区域内,方便进行嵌入和提取操作。其中 a 表示嵌入强度,而 watermark 则是用于嵌入的水印图像,这里我们嵌入的是一个全灰的图像。通过以上的代码,我们可以实现数字盲水印的嵌入和提取,并在一定程度上保护数字图片的版权安全。

HackWhisper的博客 516

poj 3735 Training little cats(矩阵构造,快速幂)

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10350   Accepted: 2471 Description Facer's pet cat just gave birth to a brood of little

ACway的专栏 1136

自私兽群优化器(SHO)详解:从生物本能到智能优化

自私兽群优化器(SHO)是一种受动物群体自私行为启发的智能优化算法。该算法模拟动物在捕食威胁下的两种行为:聚集行为(全局搜索)和自私行为(局部搜索),将优化问题的解空间映射为生存环境,候选解视为猎物个体。SHO通过安全性评估将猎物分为安全、危险和高危三类,分别采用不同的位置更新策略,并引入捕食者角色增加选择压力。相比传统优化算法,SHO具有无需梯度信息、自适应平衡探索与开发、参数简单等优势,特别适合高维复杂和多模态优化问题。算法通过马尔可夫链理论保证收敛性,已在工程设计和数据挖掘等领域展现出良好性能。

qq_42212808的博客 202

poj 3735 Training little cats 百炼3745 详细分析

题目大意:让一只猫咪去做训练,在此之前任何猫咪没有花生: g i:让第i只小猫拿到一颗花生 e i:让第i只小猫吃掉它所拥有的所有花生 s i j:让第i只小猫与第j只小猫交换它们的花生 所有的猫咪执行这一系列的举措并且必须重复m次!你必须确定最后每只小猫拥有的花生的数量。 解题思路: 我们把刚才那3只猫看做一个矩阵{a,b,c},分别代表他们有的花生个数,显然初始是{0,0,0} 当进行s操作的...

csyifanZhang的博客 276

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

POJ 3735 Training little cats

题目连接:Training little cats题目大意:有n只猫咪,开始时每只猫咪有花生0颗,现有一组操作,由下面三个中的k个操作组成: 1. g i 给i只猫咪一颗花生米 2. e i 让第i只猫咪吃掉它拥有的所有花生米 3. s i j 将猫咪i与猫咪j的拥有的花生米交换 现将上述一组操作做m次

Nemaleswang的博客 359

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

xiaowuga poj3735—Training little cats(特殊操作转化为矩阵操作)

题意:有n只猫,对其进行k次操作,然后反复这样操作m次。    其中g 表示 i 猫加1, e表示 i 猫为0;s表示 i 与 j 猫互换。 解释一下样例: 3 1 6g 1g 2g 2s 1 2g 3e 2 则, (g 1 第一只猫加1):1, 0, 0;=>1,1,0=>1,2,0=>(s 1 2 第一只猫和第二只猫互相交换)2,1,0=>2,1,1=>...

weixin_33973600的博客 87

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

POJ 3735 Training little cats(矩阵快速幂)

Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11787 Accepted: 2892 Description Facer’s pet cat just gave birth to a brood of little cats. Hav...

weixin_30810583的博客 194

poj_3735_Training little cats(矩阵快速幂)

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8468   Accepted: 2033 Description Facer's pet cat just gave birth to a brood of little

Light_starlight 670

POJ 3735:Training little cats 联想到矩阵相乘

Training little cats Time Limit:2000MS Memory Limit:65536K Total Submissions:11208 Accepted:2698 Description Facer's pet cat just gave birth to a brood ...

weixin_34163741的博客 150

poj 3735 Training little cats

<br /><br />Training little cats<br />Time Limit:2000MS<br /> <br />Memory Limit:65536K<br />Total Submissions:5815<br /> <br />Accepted:1423<br />Description<br />Facer's pet cat just gave birth to a brood of little cats. Having considered the health of t

喵呜的Blog 4399

poj 3735 Training little cats(构造矩阵)

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8326   Accepted: 2004 Description Facer's pet cat just gave birth to a brood of little

WEYuLi的专栏 842

poj 3735 Training little cats(矩阵快速幂)

Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make the cats to do some exercises. Facer has well designe

浮躁的人,终究一事无成! 1229

poj 3745 Training little cats (矩阵运算+矩阵快速幂)

poj 3745 Training little cats (矩阵运算+矩阵快速幂)

PKU_ZZY的博客 1118

POJ 3745 : Training little cats(矩阵快速幂加优化)

总时间限制: 2000ms 内存限制: 65536kB 描述 Facer’s pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Face

pku_Coder的博客 562

POJ3735 Training little cats 矩阵快速幂

Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make the cats to do some exercises. Facer has well designed ...

Yerkeys的博客 255
上一篇: poj 3233Matrix Power Series(矩阵快速幂 二分求和 求累乘的和)
下一篇: poj 3984迷宫问题(bfs求最短路径 类似并查集保存上个节点 保存最短路径)
牧马人Eikko
博客等级 码龄11年 140粉丝 94原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值