poj 3735 Training little cats(构造矩阵)
http://poj.org/problem?id=3735
大致题意:
有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成:
1. g i 给i仅仅猫一颗花生米
2. e i 让第i仅仅猫吃掉它拥有的全部花生米
3. s i j 将猫i与猫j的拥有的花生米交换
现将上述一组操作循环m次后,问每仅仅猫有多少颗花生?
非常明显,要先构造矩阵。构造一个(n+1)*(n+1)的矩阵a,初始化为单位矩阵。
g i : a[i][n+1] += 1;
e i : a[i][j] = 0;(1 <= j <= n+1)
s i j : swap(a[i][k],a[j][k])(1 <= k <= n+1)
然后矩阵高速幂就ok了。
注意m非常大,k也不小,要用long long .
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
#define C 240
#define S 20
using namespace std;
const int maxn = 110;
int n,m,k; struct matrix
{
LL mat[maxn][maxn];
void init()
{
memset(mat,0,sizeof(mat));
for(int i = 1; i <= n+1; i++)
mat[i][i] = 1;
}
}a,b; matrix mul(matrix a, matrix b)
{
matrix ans;
memset(ans.mat,0,sizeof(ans.mat));
for(int i = 1; i <= n+1; i++)
{
for(int k = 1; k <= n+1; k++)
{
if(a.mat[i][k] == 0) continue;
for(int j = 1; j <= n+1; j++)
ans.mat[i][j] += a.mat[i][k] * b.mat[k][j];
}
}
return ans;
} matrix pow(matrix a, int nn)
{
matrix ans;
ans.init();
while(nn)
{
if(nn&1)
ans = mul(ans,a);
a = mul(a,a);
nn >>= 1;
}
return ans;
} int main()
{
char ch[3];
int x,y;
while(~scanf("%d %d %d",&n,&m,&k))
{
if(n == 0 && m == 0 && k == 0) break;
a.init();
memset(b.mat,0,sizeof(b.mat));
b.mat[n+1][1] = 1;
while(k--)
{
scanf("%s",ch);
if(ch[0] == 'g')
{
scanf("%d",&x);
a.mat[x][n+1] += 1;
}
else if(ch[0] == 'e')
{
scanf("%d",&x);
for(int i = 1; i <= n+1; i++)
a.mat[x][i] = 0;
}
else
{
scanf("%d %d",&x,&y);
for(int i = 1; i <= n+1; i++)
swap(a.mat[x][i],a.mat[y][i]);
}
}
a = pow(a,m);
matrix ans = mul(a,b);
for(int i = 1; i <= n; i++)
{
printf("%lld",ans.mat[i][1]);
if(i == n) printf("\n");
else printf(" ");
}
}
return 0; }
poj 3735 Training little cats(构造矩阵)的更多相关文章
- [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: 11787 Accepted: 2892 ...
- POJ 3735 Training little cats(矩阵乘法)
[题目链接] http://poj.org/problem?id=3735 [题目大意] 有一排小猫,给出一系列操作,包括给一只猫一颗花生, 让某只猫吃完所有的花生以及交换两只猫的花生, 求完成m次操 ...
- poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)
题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...
- 矩阵快速幂 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: 13488 Accepted: ...
- 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
题意 维护一个向量, 有三种操作 将第\(i\)个数加1 将第\(i\)个数置0 交换第\(i\)个数和第\(j\)个数 Solution 矩阵乘法/快速幂 Implementation 我们将向量写 ...
随机推荐
- GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2016-07-20 16:27:34.873, end time: 2016-07-20 16:27:39.895, client elapsed: 0 ms
方案一: 重启dubbo连接 zookeeper 方案二: 经压测,greys跟踪得知,是dubbo的monitor的问题.主要超时的方法是dubbo的getIP方法,monitor每次收集数据的时候 ...
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
- Office WORD如何关闭自动检查语法
只要把打钩的地方全部去掉即可.
- Web 前端攻防(2014版)-baidu ux前端研发部
http://fex.baidu.com/articles/page2/ Web 前端攻防(2014版) zjcqoo | 20 Jun 2014 禁止一切外链资源 外链会产生站外请求,因此可以被利用 ...
- Python3.2官方文档翻译--标准库概览(一)
7.1 操作系统接口 Os模块提供主要很多与操作系统交互的函数. >>> import os >>> os.getcwd() # Return the curren ...
- 虚拟机下linux迁移造成MAC地址异常处理办法
虚拟机下linux迁移造成MAC地址异常处理办法 Linux无法启用网卡:Device eth0 has different MAC address than expected,ignoring解决 ...
- NI License Activator 用法
双击打开后,看到这种界面,将白色方格通过鼠标右击点绿就能够了. NI <wbr>License <wbr>Activator <wbr>用法 可能会出现这样的情况, ...
- 建maven私服nexus
1.下载nexus 2.配置java环境 3.安装 C:\Program Files\nexus\nexus-2.11.4-01\bin\jsw\windows-x86-64 4.配置 http:// ...
- 【Linux】文件描述符与重定向
重定向符号 符号 描述 > 输出重定向到一个文件或设备 覆盖原来的文件 >! 输出重定向到一个文件或设备 强制覆盖原来的文件 >> 输出重定向到一个文件或设备 追加原来的文件 ...