您的位置:首页 > 其它

[HDOJ2276]Kiki & Little Kiki 2

2015-06-15 19:37 239 查看

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


[align=left]Problem Description[/align]
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)

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each data set, output all lights' state at m seconds in one line. It only contains character '0' and '1.

[align=left]Sample Input[/align]

1
0101111
10
100000001

[align=left]Sample Output[/align]

1111000
001000010

[align=left]Source[/align]
HDU 8th Programming Contest Site(1)

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

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>

using namespace std;

#define MOD 2
#define MAXN 101

typedef struct MAT
{
int d[MAXN][MAXN];
int r, c;
MAT()
{
r = c = 0;
memset(d, 0, sizeof(d));
}
}MAT;

MAT mul(MAT m1, MAT m2, int mod)
{
MAT ans = MAT();
ans.r = m1.r;
ans.c = m2.c;
for(int i = 0; i < m1.r; i++)
{
for(int j = 0; j < m2.r; j++)
{
if(m1.d[i][j])
{
for(int k = 0; k < m2.c; k++)
{
ans.d[i][k] = (ans.d[i][k] + m1.d[i][j] * m2.d[j][k]) % mod;
}
}
}
}
return ans;
}

MAT quickmul(MAT m, int n, int mod)
{
MAT ans = MAT();
for(int i = 0; i < m.r; i++)
{
ans.d[i][i] = 1;
}
ans.r = m.r;
ans.c = m.c;
while(n)
{
if(n & 1)
{
ans = mul(m, ans, mod);
}
m = mul(m, m, mod);
n >>= 1;
}
return ans;
}

int main() {
int t;
while(scanf("%d", &t) != EOF && t) {
char T[105];
scanf("%s", T);
int n = strlen(T);
MAT A, B, ans;
A.r = 1, A.c = n;
B.r = n, B.c = n;
for(int i = 0; i < n; i++) {
A.d[0][i] = T[i] - '0';
B.d[i][i] = 1;
B.d[i][(i+1)%n] = 1;
}
ans = quickmul(B, t, MOD);
ans = mul(A, ans, MOD);
for(int i = 0; i < n; i++) {
printf("%d", ans.d[0][i]);
}
printf("\n");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: