poj 3753 Training little cats_矩阵快速幂
题意: 通过各种操作进行,给第i只猫花生,第i只猫吃光花生,第i只猫和第j只猫互换花生,问n次循环操作后结果是什么
很明显是构建个矩阵,然后矩阵相乘就好了
#include <iostream>
#include <cstdio>
#include<cstring>
using namespace std;
#define LL long long
#define N 110
LL n,m,d;
struct node{
LL mat[N][N];
node operator*(const node &x){
node tmp;
int i,j,k;
memset(tmp.mat,0,sizeof(tmp.mat));
for(i=0;i<=n;i++)
for(k=0;k<=n;k++){
if(mat[i][k]){
for(j=0;j<=n;j++){
tmp.mat[i][j]+=mat[i][k]*x.mat[k][j];
}
}
}
return tmp;
}
}cat;
node solve(){
int i;
if(m==1)
return cat;
node bcat;
memset(bcat.mat,0,sizeof(bcat.mat));
for(i=0;i<=n;i++)
bcat.mat[i][i]=1;
if(m==0)
return bcat;
while(m){
if(m&1) bcat=bcat*cat;
m>>=1;
cat=cat*cat;
}
return bcat;
}
int main(int argc, char** argv) {
int i,j,k;
LL a,b;
char str[10];
while(scanf("%lld%lld%lld",&n,&m,&d)!=EOF){
if(!n&&!m&&!d)
break;
memset(cat.mat,0,sizeof(cat.mat));
for(i=0;i<=n;i++){
cat.mat[i][i]=1;
}
while(d--){
scanf("%s",str);
if(str[0]=='g'){
scanf("%lld",&a);
cat.mat[0][a]++;
}else if(str[0]=='e'){
scanf("%lld",&a);
for(i=0;i<=n;i++)
cat.mat[i][a]=0;
}else{
scanf("%lld%lld",&a,&b);
for(i=0;i<=n;i++){
LL t=cat.mat[i][a];
cat.mat[i][a]=cat.mat[i][b];
cat.mat[i][b]=t;
}
}
}
node sum=solve();
for(i=1;i<n;i++)
printf("%lld ",sum.mat[0][i]);
printf("%lld\n",sum.mat[0][i]);
}
return 0;
}
poj 3753 Training little cats_矩阵快速幂的更多相关文章
- poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化
题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...
- Training little cats_矩阵快速幂
Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health ...
- 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 2888 Magic Bracelet(Polya+矩阵快速幂)
Magic Bracelet Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 4990 Accepted: 1610 D ...
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- POJ 3233 Matrix Power Series 矩阵快速幂
设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...
- poj 3613 Cow Relays【矩阵快速幂+Floyd】
!:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...
- POJ 3631 Cow Relays Floyd+矩阵快速幂
题目描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race ...
随机推荐
- [PowerShell] Backup Folder and Files Across Network
## This Script is used to backup folder/files and delete the old backup files. ## Author: Stefanie # ...
- VS2010下WPF开发ARCGIS ENGINE 10的带Ribbon控件项目
原文 http://blog.sina.com.cn/s/blog_47522f7f0100nq5t.html 题目好长,但是集目前最新的工具于一身..VS是最新的2010版,不过用的是.net3.5 ...
- 常用数据结构及复杂度 array、LinkedList、List、Stack、Queue、Dictionary、SortedDictionary、HashSet、SortedSet
原文地址:http://www.cnblogs.com/gaochundong/p/data_structures_and_asymptotic_analysis.html 常用数据结构的时间复杂度 ...
- 【总结】OJ练习,进行的一些编程语言方面总结
1.STL vector只有四个构造函数 ) explicit vector (const allocator_type& alloc = allocator_type()); fill () ...
- 剑指offer-面试题22.栈的压入,弹出序列
题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第 二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等. 例如序列1.2.3.4.5是某栈的压栈序列,序列4.5.3.2.1 是该压栈 ...
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- mongoDB 在windows平台下安装成系统服务
1,用如截图所示命令将mongoDB安装成windows系统服务,byRuiy Tips[Note,logpath and dbpath根据你命令行指导的路径,这些目录一定要已经创建]
- 异常:ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed...
ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed: com.movie.类 放到lib 包下 \W ...
- ACM学习-POJ-1125-Stockbroker Grapevine
菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1125-Stockbroker Grapevine Stockbroker Grapevine Time Li ...
- oracle中的rowid--伪列-删除表中的重复内容-实用
1.rowid是一个伪列,是用来确保表中行的唯一性,它并不能指示出行的物理位置,但可以用来定位行. 2.rowid是存储在索引中的一组既定的值(当行确定后).我们可以像表中普通的列一样将它选出来. 3 ...