https://nanti.jisuanke.com/t/31721

题意

有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物:1.这三个食物不能都相同;2.若三种食物都有的情况,巧克力不能在中间;3.如果两边是巧克力,中间不能是肉或鱼。求方案数。

分析

将meat ,  chocolate,fish 用 0 ,1 , 2 表示。

对于 n 来说,我们只关注后两位,因为 若 n - 1 的所有方案解决的话,我们在 n - 1 的方案添加0, 1,2 就行,然后根据禁忌 去除不可能的方案。

我们根据次状态 来更新现状态,然后矩阵快速幂。最后得到的矩阵的总和就是答案了。

另外,暴力推前十几项,然后用BM居然也能过!!黑科技黑科技

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
const int mod = 1e9 + ;
const int N = ;
struct Matrix{
ll a[N][N];
Matrix(){
for(int i=;i<N;i++)
for(int j=;j<N;j++)
a[i][j]=;
}
};
Matrix mul(Matrix x,Matrix y){
Matrix res;
for(int i=;i<N;i++)
for(int j=;j<N;j++)
for(int k=;k<N;k++)
res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;
return res;
}
Matrix qpow(Matrix a,ll b){
Matrix res;
for(int i=;i<N;i++) res.a[i][i]=;
while(b){
if(b&) res=mul(res,a);
b>>=;
a=mul(a,a);
}
return res;
}
int A[][]={
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,
};
int main(){
Matrix tmp;
for(int i=;i<N;i++)
for(int j=;j<N;j++)
tmp.a[i][j]=A[i][j];
int t;
ll n;
scanf("%d",&t);
while(t--){
scanf("%lld",&n);
if(n==) puts("");
else if(n==) puts("");
else{
Matrix ans;
ans=qpow(tmp,n-);
ll res=;
for(int i=;i<N;i++)
for(int j=;j<N;j++)
res=(res+ans.a[i][j])%mod;
printf("%lld\n",res);
}
}
return ;
}

附上杜教BM模板。解决任何线性递推式

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
ll powmod(ll a,ll b) {ll res=;a%=mod; assert(b>=); for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
// head int _,n;
namespace linear_seq {
const int N=;
ll res[N],base[N],_c[N],_md[N]; vector<int> Md;
void mul(ll *a,ll *b,int k) {
rep(i,,k+k) _c[i]=;
rep(i,,k) if (a[i]) rep(j,,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
for (int i=k+k-;i>=k;i--) if (_c[i])
rep(j,,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
rep(i,,k) a[i]=_c[i];
}
int solve(ll n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
// printf("%d\n",SZ(b));
ll ans=,pnt=;
int k=SZ(a);
assert(SZ(a)==SZ(b));
rep(i,,k) _md[k--i]=-a[i];_md[k]=;
Md.clear();
rep(i,,k) if (_md[i]!=) Md.push_back(i);
rep(i,,k) res[i]=base[i]=;
res[]=;
while ((1ll<<pnt)<=n) pnt++;
for (int p=pnt;p>=;p--) {
mul(res,res,k);
if ((n>>p)&) {
for (int i=k-;i>=;i--) res[i+]=res[i];res[]=;
rep(j,,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
}
}
rep(i,,k) ans=(ans+res[i]*b[i])%mod;
if (ans<) ans+=mod;
return ans;
}
VI BM(VI s) {
VI C(,),B(,);
int L=,m=,b=;
rep(n,,SZ(s)) {
ll d=;
rep(i,,L+) d=(d+(ll)C[i]*s[n-i])%mod;
if (d==) ++m;
else if (*L<=n) {
VI T=C;
ll c=mod-d*powmod(b,mod-)%mod;
while (SZ(C)<SZ(B)+m) C.pb();
rep(i,,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
L=n+-L; B=T; b=d; m=;
} else {
ll c=mod-d*powmod(b,mod-)%mod;
while (SZ(C)<SZ(B)+m) C.pb();
rep(i,,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
++m;
}
}
return C;
}
int gao(VI a,ll n) {
VI c=BM(a);
c.erase(c.begin());
rep(i,,SZ(c)) c[i]=(mod-c[i])%mod;
return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
}
}; int main() {
int T;
ll n;
cin>>T; vector<int>v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
while (T--) {
scanf("%lld",&n);
printf("%d\n",linear_seq::gao(v,n-));
}
}

ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(矩阵快速幂,BM)的更多相关文章

  1. ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  2. ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  3. ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  4. ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)

    There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...

  5. ACM-ICPC 2018 焦作赛区网络预赛

    这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...

  6. ACM-ICPC 2018 焦作赛区网络预赛J题 Participate in E-sports

    Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...

  7. ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

    There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry th ...

  8. ACM-ICPC 2018 焦作赛区网络预赛 I题 Save the Room

    Bob is a sorcerer. He lives in a cuboid room which has a length of AA, a width of BB and a height of ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

    Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring won ...

随机推荐

  1. Python FAQ

    1.在函数a中又定义了函数sum,内部函数sum可以引用外部函数a的参数n,不能这样写n=n+1,两个会出错,这样写s=s+n可以 解决: def a(): n = 1 def sum(): nonl ...

  2. 【XSY1476】平凡之路 斜率优化DP

    题目大意 有\(n\)个格子,一开始你在\(1\)号格子.每次你只能往编号更大的格子走.从第\(i\)个格子走到第\(j\)个格子的代价是\(a_i+a_j\times(j-i)\times m\) ...

  3. [poj1160][IOI2000]Post Office【动态规划】

    传送门 https://vjudge.net/problem/POJ-1160#author=SCU2018 题目描述 在一条水平的公路上建有n个小屋,两个小屋间的距离是它们的横坐标之差的绝对值.保证 ...

  4. 解决关于confluence缓慢 字体乱码 宏乱码 编辑不能贴图等问题

    应用场景:Confluence软件不用多说,与Jira一样,都是atlassion的精品软件,不再介绍. 这里因为使用的是破解版的confluence,故遇见一些问题,只能百度谷歌自行解决,也在此记录 ...

  5. Codeforces 1051E. Vasya and Big Integers

    题意:给你N个点M条边,M-N<=20,有1e5个询问,询问两点的最短距离.保证没有自环和重边. 题解:连题目都在提示你这个20很有用,所以如果是颗树的话那任意两点的最短距离就是求一下lca搞一 ...

  6. (转)CDN的作用与基本过程

    背景:积累大型网站开发中需要掌握的技术. CDN的作用与基本过程 https://blog.csdn.net/lihao21/article/details/52808747#comments CDN ...

  7. Java 读数据库字段时发现的一个现象

    早上发现有一个网名叫“帅!是不需要理由”的一个人,在后台只能看到“帅!是不需要理”,“由”字就是不显示出来. 经过分析发现,在Access数据库中,name这个字段的长度是15,因为我知道Access ...

  8. 关于Java中扫描仪next()与nextLine()的区别

    首先,next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后 ...

  9. linux:提取匹配含有小数点的数字(grep函数)

    学艺不精,一开始用了 “grep -ne '46.5743' file.txt” 提取含有46.5743的小数不成功,后面查资料才知道正则表达式中,小数点代表的是:一定有一个任意字节. 正确的写法应该 ...

  10. 对C# .Net4.5异步机制测试

    static void Main(string[] args) { Test(); // 这个方法其实是多余的, 本来可以直接写下面的方法 // await GetName() // 但是由于控制台的 ...