POJ 3735: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 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
题意是有很多小猫,每个猫一开始有0个花生。
Facer做出g i的动作就是给第i只小猫一个花生。
e i的动作就是让第i只小猫吃掉所有的花生。
s i j的意思是让第i只小猫与第j只小猫交换它们的花生。
这一系列的动作要做m次,问最终每个猫的花生数量。
这个题实际上就是对一个一维数组赋值,交换 经过这样多次的操作之后,问最终这个一位数组的值。
之前没接触过这样的题目,转成二维觉得很新鲜。
g i 就是把第i列的第0行赋值为1。
e i 就是把第i列的所有值赋值为0。
s i j就是交换第i列与第j列的值。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct matrix {
long long m[105][105];
}; long long n, mo, k;
matrix b; matrix mu(matrix no1, matrix no2)
{
matrix t;
memset(t.m, 0, sizeof(t.m)); long long i, j, k; for (i = 0; i <= n; i++)
{
for (k = 0; k <= n; k++)
{
if (no1.m[i][k])
{
for (j = 0; j <= n; j++)
{
t.m[i][j] += no1.m[i][k] * no2.m[k][j];
}
}
}
}
return t; } matrix multi(matrix no, long long x)
{
memset(b.m, 0, sizeof(b.m));
long long i;
for (i = 0; i <= n; i++)
{
b.m[i][i] = 1;
}
while (x > 0)
{
if (x & 1)
b = mu(b, no);
x = x >> 1;
no = mu(no, no);
}
return b;
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); long long i, j, temp;
string test;
matrix no; while (cin >> n >> mo >> k)
{
if (n == 0 && mo == 0 && k == 0)
break;
memset(no.m, 0, sizeof(no.m)); for (i = 0; i <= n; i++)
no.m[i][i] = 1;
for (i = 1; i <= k; i++)
{
cin >> test >> temp;
if (test == "g")
{
no.m[0][temp]++;
}
else if (test == "s")
{
long long temp2;
long long b;
cin >> temp2; for (j = 0; j<= n; j++)
{
b = no.m[j][temp];
no.m[j][temp] = no.m[j][temp2];
no.m[j][temp2] = b;
}
}
else if (test == "e")
{
for (j = 0; j <= n; j++)
{
no.m[j][temp] = 0;
}
}
} no = multi(no, mo); cout << no.m[0][1]; for (i = 2; i <= n; i++)
{
cout << " " << no.m[0][i];
}
cout << endl;
}
//system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3735:Training little cats 联想到矩阵相乘的更多相关文章
- poj 3735 Training little cats(构造矩阵)
http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...
- 矩阵快速幂 POJ 3735 Training little cats
题目传送门 /* 题意:k次操作,g:i猫+1, e:i猫eat,s:swap 矩阵快速幂:写个转置矩阵,将k次操作写在第0行,定义A = {1,0, 0, 0...}除了第一个外其他是猫的初始值 自 ...
- [POJ 3735] Training little cats (结构矩阵、矩阵高速功率)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9613 Accepted: 2 ...
- POJ 3735 Training little cats<矩阵快速幂/稀疏矩阵的优化>
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13488 Accepted: ...
- POJ 3735 Training little cats(矩阵快速幂)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11787 Accepted: 2892 ...
- POJ 3735 Training little cats(矩阵乘法)
[题目链接] http://poj.org/problem?id=3735 [题目大意] 有一排小猫,给出一系列操作,包括给一只猫一颗花生, 让某只猫吃完所有的花生以及交换两只猫的花生, 求完成m次操 ...
- POJ 3735 Training little cats 矩阵快速幂
http://poj.org/problem?id=3735 给定一串操作,要这个操作连续执行m次后,最后剩下的值. 记矩阵T为一次操作后的值,那么T^m就是执行m次的值了.(其实这个还不太理解,但是 ...
- poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化
题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...
- poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)
题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...
随机推荐
- Webshell免杀研究
前言 不想当将军的士兵不是好士兵,不想getshell的Hacker不是好Hacker~有时候我们在做攻防对抗时经常会碰到可以上传webshell的地方,但是经常会被安全狗.D盾.护卫神.云锁等安全软 ...
- 通过Java读取xml文件内容
读取XML中的内容就需要对XML进行解析,目前对XML进行解析的方法分为四种: 下面解析的方法是DOM4J,需要下载jar包dom4j:https://dom4j.github.io/ package ...
- Python 基础之循环结构 while
一.while循环介绍 while 循环 可以提高代码的效率,减少代码的冗余 while 条件表达式: code1 code2如果条件表达式成立,返回Ture,就执行其中的代码块 1.基本 ...
- 如何在ubuntu14.04(64位)编译运行32位程序
sudo -i cd /etc/apt/sources.list.d echo "deb http://archive.ubuntu.com/ubuntu/ raring main rest ...
- jetson nano 安装 snowboy 遇到的问题及处理
Snowboy 是 KITT.AI 开发的一个高度可定制的热词检测引擎,当笔者的 jetson nano 加上话筒后,就立马尝试安装,但在安装过程中却发生了错误,所以把处理方式记录了下来以作备忘. 首 ...
- 测试人员如何使用Git
测试人员如何使用Git? 首先Git的安装,这里不多做阐述,直接去Git官方网站下载后并傻瓜式安装即可. 如何判定已安装好Git呢? ------------- 随便打开一个目录,鼠标右键点击可看到 ...
- Docker 基础入门
Docker是一个开放的平台,将应用和基础设施分隔开来, 方便快速的交付软件.利用docker的提供的方法可以快速的测试和部署代码,显著的减少写代码和部署直接的延迟. Docker 平台(The Do ...
- 【剑指Offer面试编程题】题目1352:和为S的两个数字--九度OJ
题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输入: 每个测试案例包括两行: 第一行包含一个整数n和k, ...
- ch6 列表和导航条
为列表添加定制的项目符号 可使用list-style-image属性:缺点是对项目符号图像的位置的控制能力不强. 常用的方法:使用list-style-type来关闭项目符号,将定制的项目符号作为背景 ...
- 103、Java中String类之compareTo()方法
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...