poj 3735 Training 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: 10350 Accepted: 2471

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 nm 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


k个命令执行m次,求最后每个猫的花生数。

构造矩阵:http://blog.csdn.net/magicnumber/article/details/6217602


构造的是(n+1)*(n+1)矩阵,最后一列存的猫的花生数,然后矩阵快速幂,加个稀疏矩阵的优化:注意,ans.ma[i]

[k]=(ans.ma[i][k]+x.ma[i][j]*y.ma[j][k]);才可以优化,ans.ma[i][j]=(ans.ma[i][j]+x.ma[i][k]*y.ma[k][j])的不可以优

化,在这个点坑了好久。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct matrix
{
    long long ma[130][130];
}a;

int n,t;
long long m;
matrix multi(matrix x,matrix y)//矩阵相乘
{
    matrix ans;
    memset(ans.ma,0,sizeof(ans.ma));
    for(int i=1;i<=n+1;i++)
    {
        for(int j=1;j<=n+1;j++)
        {
            if(x.ma[i][j])//稀疏矩阵优化
            for(int k=1;k<=n+1;k++)
            {
                ans.ma[i][k]=(ans.ma[i][k]+x.ma[i][j]*y.ma[j][k]);
            }
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%d%I64d%d",&n,&m,&t)&&(n+m+t))
    {
        for(int i=1;i<=n+1;i++)
        {
            for(int j=1;j<=n+1;j++)
            {
                if(i==j)
                a.ma[i][j]=1;
                else
                a.ma[i][j]=0;
            }
        }
        char s[10];
        int x,y;
        for(int i=0;i<t;i++)
        {
            scanf("%s",s);
            if(s[0]=='g')
            {
                scanf("%d",&x);
                a.ma[x][n+1]++;
            }
            if(s[0]=='e')
            {
                scanf("%d",&x);
                for(int j=1;j<=n+1;j++)
                {
                    a.ma[x][j]=0;
                }
            }
            if(s[0]=='s')
            {
                scanf("%d%d",&x,&y);
                for(int j=1;j<=n+1;j++)
                {
                    long long temp=a.ma[x][j];
                    a.ma[x][j]=a.ma[y][j];
                    a.ma[y][j]=temp;
                }
            }
        }
        matrix ans;
        for(int i=1;i<=n+1;i++)//单位矩阵
        {
            for(int j=1;j<=n+1;j++)
            {
                if(i==j)
                ans.ma[i][j]=1;
                else
                ans.ma[i][j]=0;
            }
        }
        while(m)//矩阵快速幂
        {
            if(m&1)
            {
                ans=multi(ans,a);
            }
            a=multi(a,a);
            m=(m>>1);
        }
        for(int i=1;i<=n;i++)
        printf("%I64d ",ans.ma[i][n+1]);
        printf("\n");
    }
    return 0;
}




LSSS线性秘密共享方案详细构造方法与原理解释 关于LSSS https://zhuanlan.zhihu.com/p/21664741 1 LSSS英文精确定义如下: 翻译后: 一个关于参与者集合P\RhoP关于秘密分享方案Π\PiΠ在ZpZ_pZp​上是线性的,则需要满足以下条件: 1.每一个参与者的分享份额(见2点)构成ZpZ_pZp​上的一个向量; 2.存在一个关于Π\PiΠ的秘密生成矩阵MMM,这个矩阵有mmm行ddd列,对于i=1,2,....mi=1,2,....mi=1,2,....m,MMM的第iii行MiM_iMi​被一个参与者 阅读详情

相关推荐

线性秘密共享方案(LSSS

介绍线性秘密共享方案(LSSS)相关知识,共享矩阵如何构造,加密,解密方法。

线性秘密共享方案(LSSS构造与解密

秘密共享体制自从Shamir和Blakley在1979年各自独立提出后(Shamir提出的方案基于插值法,Blakley提出的方案基于高斯消元法),作为现代密码学的重要工具之一,在实际中有很多应用。在信息系统中使用的秘密共享,可以防止系统密钥的遗失、损坏和来自地方的攻击,减小秘密保存者的责任。在(t,n)秘密共享体制中,秘密分发者将一个秘密信息分成n个秘密份额,分发给n个人,当需要恢复秘密信息时,任意少于t个的秘密保存者都得不到该秘密的任何信息。现目前进行秘密共享的主流方案有基于访问控制树和秘密共享矩阵的。

e小王同学V的博客 3万+

线性秘密共享方案(LSSS矩阵构造

线性密钥分享方案(LSSS矩阵构造 基于属性加密(Attribute-Based Encryption, ABE)中有一个很重要的概念就是访问控制策略。具体到密文策略属性基加密(Ciphertext-Policy Attribute-Based Encryption, CP-ABE)方案中:方案存在着若干的属性,明文在一定的访问控制策略下被加密成密文;属性满足密文策略的用户就可以对密文进行解密。

hhh_2333 2万+

POJ-3735-Training little cats-构造矩阵+矩阵快速幂+稀疏矩阵乘法优化

http://poj.org/problem?id=3735 题意: n只猫,三种命令: 1、第i只猫吃掉所有花生; 2、第i只猫得到一个花生; 3、交换第i,j只猫的花生; 先由k个 这些命令组成一个操作序列 然后重复操作序列m次, n,k m的次数那么大,可以用构造矩阵,然后用快速幂的方法 引用大神http://blog.csdn.net/magicnumber/artic

缺氧 752

[POJ 3735] Training little cats (构造矩阵矩阵快速幂

POJ 3735 Training little cats 矩阵快速幂 矩阵构造

SIOFive的专栏 970

POJ 3735 Training little cats矩阵快速幂+矩阵构造好题)

题目:POJ 3735 Training little cats  PS:做完了大路边上的题目,剩下的都是些难想难敲的题目了。所以看书是一方面,做题又是另一方面。这个题目给了我两个收获, 一个是代码上的优化,另一个是矩阵的非递推关系形式的构造。关于代码优化,这个题目靠普通的高代定义写法是过不去的,需要优 化,而这种写法不是从数学的角度,而是从程序优化的角度给了一种符合定义的新的写法。关键代码

m0_37772713的博客 286

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

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

街角的花街糖 685

POJ 3735 Training little cats 解题报告(矩阵构造+快速幂优化)

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

SF-_- 的 ACM 博客 918

POJ 3735 Training little cats矩阵的快速求幂】

POJ 3735 Training little cats 算法核心:矩阵建模,矩阵快速幂 大意:已知有n只猫咪,开始时每只猫咪有花生米0颗,先有一组操作:由下面三个中的k个操作组成:g i 给i只猫咪一颗花生米e i 让第i只猫咪吃掉它拥有的所有花生米s i j 将猫咪i与猫咪j的拥有的花生米交换 现将上述操作做m次后,问每只猫咪有多少颗花生米? 分析:因m的数据范...

a48634331的博客 208

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 4393

poj 3735 Training little cats 题解(构造矩阵

原题链接: poj 题意简述 给定一个长度为n(&lt;=100)n(&lt;=100)n(<=100)的序列,和一组操作。一组操作由k(&lt;=100)k(&lt;=100)k(<=100)个操作组成,有三种,分别是: 交换两个数 某个单独位置++ 某个单独位置=0 重复m(&lt;=1e9)m(&lt;=1e9)m(<=1...

LightningUZ的博客 279

poj3735-Training little cats

poj3735 Training little cats Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14750Accepted: 3648 Description Facer’s pet cat just gave birth to a brood of little

weixin_40433319的博客 355

[poj3735] Training little cats_矩乘快速幂

Training little cats poj-3735     题目大意:给你n个数,k个操作,将所有操作重复m次。     注释:三种操作,将第i个盒子+1,交换两个盒子中的个数,将一个盒子清空。$1\le m \le 10^9$ , $1\le n , k \le 100$。       想法:定义开始是的矩阵是n+1行,1列,除了最底下的数是1剩下全是0。然后加法操作就是...

dianan0938的博客 161

POJ - 3735 Training little cats矩阵快速幂

矩阵快速幂

The Blog Of Uncle_Sugar 451

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

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

qq_18661257的专栏 738

poj 3735 Training little cats矩阵乘法】

http://poj.org/problem?id=3735 题目大意:重复训练一只猫m轮,在每轮中都有k次操作;有三种不同的操作类型: 1、给第i只猫多一颗豆 2、让第i只猫把它所拥有的都全吃了 3、交换两只猫手中的豆   用矩阵乘法,开一个(n+1)*(n+1)大的数组A。用A.a[0][i]记录第i只猫的实际豆拥有量,则 1、给第i只猫多一颗豆 → A.a[0]...

bantengbang8949的博客 120

POJ-3735 Training little cats

我们看到这个题,很容易想到矩阵快速幂的做法。因为题目中两个条件实在太显眼: All the cats perform a sequence of these moves and must repeat it mmm times! m≤1010m\leq 10^{10}m≤1010 一个相同的操作,做 101010^{10}1010 次,肯定是矩阵快速幂,问题就是我们如何把三个操作转化为 (n+1)×(n+1)(n+1)\times (n+1)(n+1)×(n+1)矩阵 optoptopt,opt

huayucaiji的博客 204
上一篇: poj 3411 Paid Roads(dfs)
下一篇: poj 3233 Matrix Power Series(矩阵二分,快速幂)
AC_way
博客等级 码龄12年 55粉丝 248原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值