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 ...
随机推荐
- OOP组合和继续的优缺点
—— 详解继承与组合的优缺点 组合与继承都是提高代码可重用性的手段.在设计对象模型时,可以按照语义来识别类之间的组合关系和继承关系.在有些情况下,采用组合关系或者继承关系能完成同样的任务,组合和继 ...
- Servlet 浅谈(二)
如何获取初始化参数 容器在初始化的时候,会为了这个Servlet创建一个唯一的ServletConfig,容器会从DD读出Servlet的初始化参数,并把这个参数交给ServletConfig,然后S ...
- memcached的安装和linux下memcached服务自启动的配置
关于memcached在windows和linux环境的安装,以及在Linux系统系memcached服务自启动的配置,可以参考我在csdn上下的博客, windows和linux环境下memcach ...
- Android 添加菜单项
刚开始看郭大神的<>,实现以下里面的一些例子.利用Menu给APP添加一个菜单项. 效果图 结构图 在res的menu文件夹下创建一个xml文件,当然你也可以使用系统创建的main.xml ...
- python3.5之string
刚开始学习python变成, 这勉强算是第一个博客吧, 主要记录了一下 字符串 中的方法, 不太准确,或者是错误的地方, 请大家指点 str1 = "GooGle" str2 = ...
- C++ 常用容器or数据结构
queue 队列 参考 1.入队:如q.push(x):将x元素接到队列的末端: 2.出队:如q.pop() 弹出队列的第一个元素,并不会返回元素的值:T 3,访问队首元素:如q.front() 4, ...
- shell提示符显示git当前分支
编辑/etc/profile或者~/.bashrc 在行末添加如下内容 # 获取git当前分支 git_branch() { branch='' cd $PWD if [ -d '.git' ]; t ...
- PVRTC 纹理
iPhone的图形芯片(PowerVR MBX)对一种称为 PVRTC 的压缩技术提供的硬件支持,Apple推荐在开发iPhone应用程序时使用 PVRTC 纹理.他们甚至提供了一篇很好的技术笔记描述 ...
- Delphi使用XmlHttp获取时间
uses ComObj, DateUtils; procedure TForm1.Button1Click(Sender: TObject); var XmlHttp: Variant; datetx ...
- css案例学习之全局声明*{} 与body{}的区别
代码 <html> <head> <title>全局声明</title> <style type="text/css"> ...