zoukankan      html  css  js  c++  java
  • poj 3735 Training little cats矩阵快速幂

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

    PKU Campus 2009 (POJ Monthly Contest – 2009.05.17), Facer
    按着建矩阵的方法写也超时了,后来才发现有加速的方法。。
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 int n, t, m;
     7 struct mat {
     8     long long a[102][102];
     9     mat() {
    10         memset(a, 0, sizeof(a));
    11     }
    12     mat operator *(const mat &o) const {
    13         mat t;
    14         for(int i = 0; i <= n; i++) {
    15             for(int j = 0; j <= n; j++)
    16                 for(int k = 0; k <= n; k++) {
    17                         if(a[i][k]){
    18                             t.a[i][j] = (t.a[i][j] + a[i][k]*o.a[k][j]);
    19                         }
    20                 }
    21         }
    22         return t;
    23     }
    24 };
    25 
    26 int main() {
    27 
    28     while(~scanf("%d%d%d" , &n, &m, &t)) {
    29         if(n == 0 && m == 0 && t == 0) break;
    30         mat a, b, c;
    31         for(int i = 0; i <= n; i++) a.a[i][i] = 1;
    32         for(int i = 0; i <= n; i++) b.a[i][i] = 1;
    33         for(int i = 0; i <= n; i++) c.a[i][i] = 1;
    34         //  c.a[0][0] = 1;
    35         while(t--){
    36             char s[20];
    37             int x, y;
    38             scanf("%s", s);
    39             if(s[0] == 's'){
    40                 scanf("%d%d", &x, &y);
    41                 for(int i = 0; i <= n; i++){
    42                     swap(b.a[i][x], b.a[i][y]);
    43                 }
    44             }else if(s[0] == 'g'){
    45                 scanf("%d", &x);
    46                 b.a[0][x]++;
    47             }else {
    48                 scanf("%d", &x);
    49                 for(int i = 0; i <= n; i++) b.a[i][x] = 0;
    50             }
    51         }
    52 
    53         while(m > 0) {
    54             if(m&1) {
    55                 a = a*b;
    56                 m--;
    57             }
    58             m >>= 1;
    59             b = b*b;
    60         }
    61         a = c*a;
    62         for(int i = 1; i <= n; i++){
    63             if(i == 1) printf("%lld", a.a[0][i]);
    64             else printf(" %lld", a.a[0][i]);
    65         }
    66         printf("
    ");
    67     }
    68     return 0;
    69 }
     
  • 相关阅读:
    cad.net DeepCloneObjects WasErased
    cad.net 更改高版本填充交互方式为低版本样子
    日志篇 VS Gitee码云
    测试篇 c#遍历所有安装程序 获取所有已经安装的程序
    cad.net 设置Acad2008默认启动 win10设置默认cad2008默认启动 20190923修改
    cad.net cad启动慢? cad2008启动慢? cad启动延迟? cad卡住? cad98%卡? 默认打印机!!
    测试篇 c#多线程实现ping 制作一个备份器
    cad.net 利用win32api实现不重复打开dwg路径的文件夹(资源管理器)
    cad.net 利用win32api实现一个命令开关参照面板 20190910修改浩辰部分问题,完美.
    cad.net 在cad2008引用了错误的com接口的dll导致出现了
  • 原文地址:https://www.cnblogs.com/cshg/p/5936416.html
Copyright © 2011-2022 走看看