[HDOJ2276]Kiki & Little Kiki 2

Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2175    Accepted Submission(s): 1110

Problem Description

There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and the left of light k (1< k<= n) is the light k-1.At time of 0, some of them turn on, and others turn off. Change the state of light i (if it‘s on, turn off it; if it is not on, turn on it) at t+1 second (t >= 0), if the left of light i is on !!! Given the initiation state, please find all lights’ state after M second. (2<= n <= 100, 1<= M<= 10^8)

Input

The input contains one or more data sets. The first line of each data set is an integer m indicate the time, the second line will be a string T, only contains ‘0‘ and ‘1‘ , and its length n will not exceed 100. It means all lights in the circle from 1 to n.
If the ith character of T is ‘1‘, it means the light i is on, otherwise the light is off.

Output

For each data set, output all lights‘ state at m seconds in one line. It only contains character ‘0‘ and ‘1.

Sample Input

1
0101111
10
100000001

Sample Output

1111000
001000010

Source

HDU 8th Programming Contest Site(1)

  用矩阵快速幂来优化DP问题的状态转移,思路如下:

代码如下:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <cstdio>
 5 #include <cmath>
 6
 7 using namespace std;
 8
 9 #define MOD 2
10 #define MAXN 101
11
12 typedef struct MAT
13 {
14     int d[MAXN][MAXN];
15     int r, c;
16     MAT()
17     {
18         r = c = 0;
19         memset(d, 0, sizeof(d));
20     }
21 }MAT;
22
23 MAT mul(MAT m1, MAT m2, int mod)
24 {
25     MAT ans = MAT();
26     ans.r = m1.r;
27     ans.c = m2.c;
28     for(int i = 0; i < m1.r; i++)
29     {
30         for(int j = 0; j < m2.r; j++)
31         {
32             if(m1.d[i][j])
33             {
34                 for(int k = 0; k < m2.c; k++)
35                 {
36                     ans.d[i][k] = (ans.d[i][k] + m1.d[i][j] * m2.d[j][k]) % mod;
37                 }
38             }
39         }
40     }
41     return ans;
42 }
43
44 MAT quickmul(MAT m, int n, int mod)
45 {
46     MAT ans = MAT();
47     for(int i = 0; i < m.r; i++)
48     {
49         ans.d[i][i] = 1;
50     }
51     ans.r = m.r;
52     ans.c = m.c;
53     while(n)
54     {
55         if(n & 1)
56         {
57             ans = mul(m, ans, mod);
58         }
59         m = mul(m, m, mod);
60         n >>= 1;
61     }
62     return ans;
63 }
64
65 int main() {
66     int t;
67     while(scanf("%d", &t) != EOF && t) {
68         char T[105];
69         scanf("%s", T);
70         int n = strlen(T);
71         MAT A, B, ans;
72         A.r = 1, A.c = n;
73         B.r = n, B.c = n;
74         for(int i = 0; i < n; i++) {
75             A.d[0][i] = T[i] - ‘0‘;
76             B.d[i][i] = 1;
77             B.d[i][(i+1)%n] = 1;
78         }
79         ans = quickmul(B, t, MOD);
80         ans = mul(A, ans, MOD);
81         for(int i = 0; i < n; i++) {
82             printf("%d", ans.d[0][i]);
83         }
84         printf("\n");
85     }
86     return 0;
87 }

时间: 2024-10-09 21:23:41

[HDOJ2276]Kiki & Little Kiki 2的相关文章

HDU 2276 Kiki &amp; Little Kiki 2 (位运算+矩阵快速幂)

HDU 2276 Kiki & Little Kiki 2 (位运算+矩阵快速幂) ACM 题目地址:HDU 2276 Kiki & Little Kiki 2 题意: 一排灯,开关状态已知,每过一秒:第i个灯会根据刚才左边的那个灯的开关情况变化,如果左边是开的,它就会变化,如果是关的,就保持原来状态.问m秒后的状态. 第1个的左边是最后一个. 分析: 转移不好想啊... 变化是这样的: 原来 左边 变化 1 1 0 1 0 1 0 1 1 0 0 0 然后想到 (~原来)^(左边)=变化

HDOJ Kiki &amp; Little Kiki 2 2276【位运算+矩阵快速幂】

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2213    Accepted Submission(s): 1137 Problem Description There are n lights in a circle numbered from 1 to n. The left of li

NYOJ 300 &amp;&amp; hdu 2276 Kiki &amp; Little Kiki 2 (矩阵快速幂)

Kiki & Little Kiki 2 时间限制:5000 ms  |  内存限制:65535 KB 难度:4 描述 There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and the left of light k (1< k<= n) is the light k-1.At time of 0, some of them turn on, and others t

HDU 2276 Kiki &amp; Little Kiki 2(矩阵快速幂)

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2265    Accepted Submission(s): 1146 Problem Description There are n lights in a circle numbered from 1 to n. The left of li

hdu 2276 Kiki &amp; Little Kiki 2矩阵快速幂

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2300    Accepted Submission(s): 1175 Problem Description There are n lights in a circle numbered from 1 to n. The left of li

HDU - 2276 Kiki &amp; Little Kiki 2 矩阵快速幂

题目大意:给出一个由0,1组成的字符串,每一秒的时候,如果该位字符左边是1的话,那么该字符就要变换(由0变1,或者由1变0,第一个的左边是最后一个),问M秒后这个字符串的状态 解题思路:用0,1矩阵来表示变化,具体的请看代码,现在没法给出矩阵,后面会补的 #include<cstdio> #include<cstring> const int N = 110; char str[N]; struct Matrix{ int mat[N][N]; }A, B, tmp; int n,

hdu 2275 Kiki &amp; Little Kiki 1

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2275 题意:n个操作 Push 入容器 Pop弹出一个 满足<=该数的最大的数(若没有输出No Element!) 用两个堆模拟. 1 #include<functional> 2 #include<algorithm> 3 #include<iostream> 4 #include<string> 5 #include<cstdlib> 6

HDU 2276 Kiki &amp; Little Kiki 2

矩阵快速幂. 0 1-> 第二个数字会变成1 0 0-> 第二个数字会变成0 1 0-> 第二个数字会变成1 1 1-> 第二个数字会变成0 根据这四个特点,就可以写转移矩阵了. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algori

HDU2276 - Kiki &amp; Little Kiki 2(矩阵快速幂)

题目链接 题意:有n盏灯,编号从1到n.他们绕成一圈,也就是说,1号灯的左边是n号灯.如果在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯的状态.输入m和初始灯的状态.输出m秒后,所有灯的状态. 思路:其实每盏灯的状态之和前一盏和自己有关,所以可以得到一个关系矩阵.假设有6盏灯,因此可以得到关系矩阵如下: (1, 0, 0, 0, 0, 1) (1, 1, 0, 0, 0, 0) (0, 1, 1, 0, 0, 0) (0, 0, 1, 1, 0, 0) (0, 0,