DecodingGenome(CodeForces-222E)【矩阵快速幂】
题目链接:https://vjudge.net/contest/333591#problem/L
题意:用m个字符构成长度为n的串,其中存在形如“ab”(表示a后不能放置b)的条件约束,问共有多少种构造方法。
思路:矩阵快速幂,建立一个数组num[53][53],num[i][j]=1表示i号字符的下一个字符可以是j号字符,num[i][j]=0表示i号字符下一个字符不能为j号字符,计算该矩阵的(n-1)次幂,再与模为sqrt(m)的m维向量相乘,算出所得向量的所有分量的和,即为答案。
反思:唯一的感想就是——矩阵快速幂太强大了!!!!!(上次用矩阵快速幂是那道计算斐波那契数列的题)
代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
long long n;
const int mo=1e9+;
int m,k;
long long num[];
long long restrict[][];
char tm1,tm2; int pow(long long x){
x--;
long long res[][],ttp[][]={};
for(int i=;i<=m;i++)
res[i][i]=;
while(x!=){
if(x&){
memset(ttp,,sizeof(ttp));
for(int ii=;ii<=m;ii++){
for(int i=;i<=m;i++){
for(int j=;j<=m;j++){
ttp[ii][j]+=res[ii][i]*restrict[i][j]%mo;
ttp[ii][j]%=mo;
}
}
}
for(int i=;i<=m;i++)
for(int j=;j<=m;j++)
res[i][j]=ttp[i][j];
}
x>>=;
memset(ttp,,sizeof(ttp));
for(int ii=;ii<=m;ii++){
for(int i=;i<=m;i++){
for(int j=;j<=m;j++){
ttp[ii][j]+=restrict[ii][i]*restrict[i][j]%mo;
ttp[ii][j]%=mo;
}
}
}
for(int i=;i<=m;i++)
for(int j=;j<=m;j++)
restrict[i][j]=ttp[i][j];
}
long long ans=;
for(int i=;i<=m;i++){
for(int j=;j<=m;j++){
ans+=num[i]*res[i][j]%mo;
ans%=mo;
}
}
return ans;
}
int main(){
scanf("%I64d%d%d",&n,&m,&k);
for(int i=;i<=m;i++){
for(int j=;j<=m;j++)
restrict[i][j]=;
num[i]=;
}
for(int i=;i<=k;i++){
scanf("\n%c%c",&tm1,&tm2);
//printf("%c %c\n",tm1,tm2);
int x1=tm1>'Z'?tm1-'a'+:tm1-'A'+;
int x2=tm2>'Z'?tm2-'a'+:tm2-'A'+;
//printf("%d %d\n",x1,x2);
restrict[x1][x2]=;
}
printf("%d",pow(n));
return ;
}
DecodingGenome(CodeForces-222E)【矩阵快速幂】的更多相关文章
- codeforces 691E 矩阵快速幂+dp
传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...
- Xor-sequences CodeForces - 691E || 矩阵快速幂
Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍 ...
- Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
- Educational Codeforces Round 60 D dp + 矩阵快速幂
https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...
- [递推+矩阵快速幂]Codeforces 1117D - Magic Gems
传送门:Educational Codeforces Round 60 – D 题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...
- CodeForces 185A. Plant (矩阵快速幂)
CodeForces 185A. Plant (矩阵快速幂) 题意分析 求解N年后,向上的三角形和向下的三角形的个数分别是多少.如图所示: N=0时只有一个向上的三角形,N=1时有3个向上的三角形,1 ...
随机推荐
- Friends (ZOJ - 3710)
Problem Alice lives in the country where people like to make friends. The friendship is bidirectiona ...
- Ubuntu14.04 挂载u盘
插入u盘后, $cat /proc/partitions 发现多了 sdb sdb4 sdb是统称,所以新插入的U盘就是/dev/sdb4 $ls /dev |grep sdb sdb sdb4 查看 ...
- shell 显示详细信息
MacdeMacBook-Pro:test macname$ ls -al | more total drwxr-xr-x macname staff : . drwxr-xr-x+ macname ...
- 关于在eclipse中Undefined attribute name (role).解决办法
方案一: 只需要在jsp表头添加一句: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c&q ...
- AGC009C Division into Two
题意 有\(n\)个严格升序的数,请你分成两个集合\(A\)和\(B\),其中一个集合任意两数之差不小于\(x\),另一集合任意两数之差不小于\(y\). 问方案数,集合可以为空. $n \le 10 ...
- FOFA 批量采集url 图形化界面编写
这是脚本 # coding:utf- import requests,re import time import sys import getopt import base64 guizhe='' s ...
- git 优雅的撤销中间某次提交
环境git : 2+ 前言最近两天,公司的git合并代码时,出现了严重的问题,浪费很多时间: 现在记录下: 情况是这样的,一个同事自己的本地分支(远程没有),不知怎么的,有了别人开发分支的代码,而他自 ...
- python flask url参数
python flask url参数 常见 url 传参中都是 xxx?xxx=xxx 问题来了 flask中我没有找到 关于xx? 问号的使用方式 是不是flask就不支持这种方式 如果有 rout ...
- Build Telemetry for Distributed Services之Elastic APM
官网地址:https://www.elastic.co/guide/en/apm/get-started/current/index.html Overview Elastic APM is an a ...
- python3 枚举enum定义和使用
两种方式定义枚举类: 1.直接使用Enum列出多个枚举值来创建枚举类. 2.通过集成Enum基类派生枚举类. 程序示范: 1.直接使用Enum列出多个枚举值来创建枚举类. from enum impo ...