hdu 2276 Kiki & Little Kiki 2 矩阵快速幂
n个灯围成一圈, 1左边是n。 有两种状态, 1是亮, 0是不亮。 如果一个灯, 它左边的灯是亮的, 那么下一时刻这个灯就要改变状态, 1变为0, 0变为1。 给出初始状态和时间t, 问t时刻每个灯的状态是什么。
ai = (a(i-1)+ai)%2, 根据这个构建矩阵。
/*
1 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0
0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 1 1
*/
// 对这个矩阵进行快速幂, 结果与初始状态相乘就好。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int n;
struct Matrix
{
int a[][];
Matrix() {
mem(a);
}
};
Matrix operator * (Matrix a, Matrix b) {
Matrix c;
for(int i = ; i<n; i++) {
for(int j = ; j<n; j++) {
for(int k = ; k<n; k++) {
c.a[i][j] += a.a[i][k]*b.a[k][j];
c.a[i][j] %= ;
}
}
}
return c;
}
Matrix operator ^ (Matrix a, ll b) {
Matrix tmp;
for(int i = ; i<n; i++)
tmp.a[i][i] = ;
while(b) {
if(b&)
tmp = tmp*a;
a = a*a;
b>>=;
}
return tmp;
}
int main()
{
int m;
string s;
while(cin>>m) {
cin>>s;
n = s.size();
Matrix tmp;
for(int i = ; i<n; i++) {
tmp.a[i][i] = ;
tmp.a[i][(i-+n)%n] = ;
}
Matrix b = tmp^m;
Matrix a;
for(int i = ; i<s.size(); i++) {
a.a[i][] = s[i]-'';
}
Matrix c = b*a;
for(int i = ; i<s.size(); i++) {
printf("%d", c.a[i][]);
}
cout<<endl;
}
return ;
}
hdu 2276 Kiki & Little Kiki 2 矩阵快速幂的更多相关文章
- HDU 2855 斐波那契+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...
- HDU 5950:Recursive sequence(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...
- HDU 3292 【佩尔方程求解 && 矩阵快速幂】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...
- HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...
- hdu 4565 So Easy! (共轭构造+矩阵快速幂)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
- HDU 2256 Problem of Precision 数论矩阵快速幂
题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- HDU 5171 GTY's birthday gift 矩阵快速幂
GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- EXT.NET常用属性
Ext_数字输入框_Ext.form.NumberField: <mce:script type="text/javascript"><!-- /* Ext. ...
- 如何让tableView展示数据
设置数据源对象 self.tableView.dataSource = self; 数据源对象要遵守协议 @interface ViewController () <UITableViewDat ...
- Apache Tiles 2.x 应用指南(转)
转自:http://jaymsimusic.iteye.com/blog/1138906 Apache Tiles 2.x 应用指南 博客分类: Apache Tiles Jakarta Tile ...
- 记一次phpStudy apache启动后自动关闭 修改过程
第一种可能原因:路径包含中文 .添加站点 2.重启服务 3.遇见问题 apache 刚启动,1秒钟中后就停止 4.解决问题 发现是自己添加的网站中包含中文路径的问题,建议不要在自己的网站目录下包含中文 ...
- mysql优化(1) 观察服务器周期性变化
先把nginx和memcached环境开启 /usr/local/nginx/sbin/nginx /usr/local/fastphp/sbin/php-fpm /usr/local/memcach ...
- mysql中select distinct的用法
在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但 往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...
- Java 按值传递
http://guhanjie.iteye.com/blog/1683637 http://www.zhihu.com/question/31203609 String类 1.String 类按照 J ...
- Hibernate摘记
原理: 1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.xml中的<mapping reso ...
- JavaEE Tutorials (23) - 资源适配器和契约
23.1什么是资源适配器362 23.1.1管理契约363 23.1.2通用工作上下文契约364 23.1.3出站和入站契约36423.2元数据注解36523.3公共客户端接口36623.4对Java ...
- POJ 2976 Dropping tests(二分答案)
[题目链接] http://poj.org/problem?id=2976 [题目大意] 给出每门成绩的总分和得分,去除k门成绩之后 使得剩余的成绩分数和除以总分得到的数字最大,要求精度在三位小数之 ...