[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
相关文章推荐
- 【转自EMC中文支持论坛】探秘I/O队列对磁盘性能的影响
- 栈的弹出、压入序列
- leetcode第53题-Maximum Subarray
- 关于genymotion模拟器无法启动的问题
- leetcode[111]:Minimum Depth of Binary Tree
- vb.net使用Newtonsoft.JSON解析JSON字串心得
- 进程的通信方式及特点
- mac 安装wget
- 前端技术(1) :JQuery学习笔记
- RANSAC算法笔记
- Linux中nat模式上不了网的问题怎么解决?
- 文本编辑器Vi和Vim(1)
- The Pilots Brothers' refrigerator
- 为Ubuntu安装SSH服务
- The Pilots Brothers' refrigerator 分类: POJ 2015-06-15 19:34 12人阅读 评论(0) 收藏
- 数据库的事务隔离级别
- 九、模板方法模式
- 3D数学基础--向量
- 3D数学基础--向量
- Android简单数据存储SharedPreferences