【题解】HDU Homework(倍增)

矩阵题一定要多多检查一下是否行列反了...

一百个递推项一定要存101个

说多了都是泪啊

一下午就做了这一道题因为实在是太菜了太久没写这种矩阵的题目...

设一个行向量\(e\),和一个增逛矩阵\(A\),他们咋定义的见我那篇讲线性递推博客

现在我们再预处理\(st\)矩阵数组,其中\(st_i=A^{2^i}\)。

考虑这样一种做法,我们考虑让\(e\)总共有101个值,然后当第一个值被增逛为\(f_{q.n-100}\)时,暴力将\(e\)中的第\(101\)项修改。中间的转移直接利用这个倍增数组。中间的转移由于是倍增,而且是行乘以一个矩阵,这样的话复杂度就是\(O(m^2)\)。考虑一个询问就要处理一次最终复杂度\(O(m^3\log n+qm^2\log n)\)

细节要处理一下

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector> using namespace std; typedef long long ll;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
const int maxn=1e2+3;
const int mod=1e9+7;
int n,m,q,T;
const int M=101;
struct MAT{
int data[maxn][maxn];
MAT(){memset(data,0,sizeof data);}
inline int*operator [](int x){return data[x];}
inline MAT operator *(MAT&f){
MAT ret;
for(int k=1;k<=M;++k)
for(int t=1;t<=M;++t)
for(int i=1;i<=M;++i)
ret[t][i]=(ret[t][i]+1ll*data[t][k]*f[k][i])%mod;
return ret;
}
}st[31]; struct line{
int data[maxn];
line(){memset(data,0,sizeof data);}
inline int&operator[](int x){return data[x];}
inline line operator *(MAT&f){
line ret;
for(int i=1;i<=M;++i)
for(int t=1;t<=M;++t)
ret[t]=(ret[t]+1ll*data[i]*f[i][t])%mod;
return ret;
}
}e; int init[maxn];
pair<pair<int,int>,vector<int> > Q[maxn]; inline void pow(const int&num){
for(int t=0;t<30;++t)
if(num>>t&1)
e=e*st[t];
} int main(){
int cnt=0;
while(~scanf("%d%d%d",&n,&m,&q)){
memset(st[0].data,0,sizeof st[0].data);
memset(e.data,0,sizeof e.data);
for(int t=1;t<=m;++t) e[t]=init[t]=qr()%mod;
for(int t=1;t<M;++t) st[0][t+1][t]=1;
T=qr();
for(int t=1;t<=T;++t) st[0][M-t+1][M]=qr()%mod;
for(int t=1;t<30;++t) st[t]=st[t-1]*st[t-1];
for(int t=1;t<=q;++t){
Q[t].first.first=qr();
Q[t].first.second=qr();
Q[t].second.clear();
Q[t].second.push_back(0);
for(int i=1;i<=Q[t].first.second;++i) Q[t].second.push_back(qr());
}
sort(Q+1,Q+q+1);
int l=1;
for(int t=m+1;t<=M;++t){
if(t==Q[l].first.first&&l<=q){
int ret=0;
for(int i=1;i<=Q[l].first.second;++i)
ret=(ret+1ll*init[t-i]*Q[l].second[i])%mod;
init[t]=ret;
++l;
}
else {
int ret=0;
for(int i=1;i<=T;++i)
ret=(ret+1ll*init[t-i]*st[0][M-i+1][M])%mod;
init[t]=ret;
}
}
for(int t=1;t<=M;++t) e[t]=init[t];
if(n<=M) {printf("Case %d: %d\n",++cnt,init[n]); continue;}
int cur=1;
for(;l<=q;++l){
if(Q[l].first.first>n) break;
pow(Q[l].first.first-(cur+M-1));
cur+=Q[l].first.first-(cur+M-1);
e[M]=0;
for(int t=1;t<=Q[l].first.second;++t)
e[M]=(e[M]+1ll*Q[l].second[t]*e[M-t])%mod;
}
pow(n-cur);
printf("Case %d: %d\n",++cnt,e[1]);
}
return 0;
}

【题解】HDU Homework(倍增)的更多相关文章

  1. [题解]hdu 1009 FatMouse' Trade(贪心基础题)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  2. HDU 2586 倍增法求lca

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 6107--Typesetting(倍增)

    题目链接 Problem Description Yellowstar is writing an article that contains N words and 1 picture, and t ...

  4. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  5. 题解 HDU 5279 YJC plays Minecraft

    题目传送门 题目大意 给出\(n\)以及\(a_{1,2,...,n}\),表示有\(n\)个完全图,第\(i\)个完全图大小为\(a_i\),这些完全图之间第\(i\)个完全图的点\(a_i\)与\ ...

  6. 题解-bzoj4320 Homework

    Problem bzoj4320 Solution 前置技能:分块+线段树+卡常+一点小小的数学知识 考试时A的 这种题无论怎么处理总有瓶颈,套路分块,设\(k\)以下的插入时直接暴力预处理,查询时直 ...

  7. 题解——HDU 4734 F(x) (数位DP)

    这道题还是关于数位DP的板子题 数位DP有一个显著的特征,就是求的东西大概率与输入关系不大,理论上一般都是数的构成规律 然后这题就是算一个\( F(A) \)的公式值,然后求\( \left [ 0 ...

  8. 题解——HDU 2089 不要62(数位DP)

    最近在学数位DP 应该是入门题吧 设\( dp[i][0/1] \)表示到第\( i \)位时,前一位是否是6的满足条件的数的个数 然后就是套路 注意\( limit \)的限制条件以及转移时候信息的 ...

  9. 题解——HDU 1848 Fibonacci again and again

    一道组合游戏的题目 SG函数的板子题 预处理出SG函数的值然后回答询问即可 代码 #include <cstdio> #include <algorithm> #include ...

随机推荐

  1. @bzoj - 4356@ Ceoi2014 Wall

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给出一个N*M的网格图,有一些方格里面存在城市,其中首都位于网格 ...

  2. Oracle中组合索引的使用详解(转)

    在Oracle中可以创建组合索引,即同时包含两个或两个以上列的索引.在组合索引的使用方面,Oracle有以下特点: 1. 当使用基于规则的优化器(RBO)时,只有当组合索引的前导列出现在SQL语句的w ...

  3. Python深入:Distutils安装包

    一:简介 尽管Python标准库已经支持很多的功能,但是有时还是会需要以第三方模块的形式添加新的功能到Python中. 在最简单的场景中,是你要安装的模块,已经有了针对你的平台的安装版本,因此只需要像 ...

  4. 【阿里云新品发布·周刊】第13期:链路追踪 Tracing Analysis 商业化首发

    点击订阅新品发布会! 新产品.新版本.新技术.新功能.价格调整,评论在下方,下期更新!关注更多内容,了解更多 最新发布 链路追踪 Tracing Analysis 商业化首发 2019年6月12日15 ...

  5. 一个简单的hexo搭建博客网站的故事

    首先安装hexo mkdir hexo #创建一个文件夹 cd hexo #切换到hexo目录下面 npm install -g hexo-cli npm install hexo --save 然后 ...

  6. [转][ASP.NET Core 3框架揭秘] 跨平台开发体验: Windows [上篇]

    微软在千禧年推出 .NET战略,并在两年后推出第一个版本的.NET Framework和IDE(Visual Studio.NET 2002,后来改名为Visual Studio),如果你是一个资深的 ...

  7. Python--day70--ORM一对一表结构

    ORM一对一表结构:

  8. chrome浏览器频繁卡死

    输入chrome:flag 把对应的GPU选项关掉 或者重装 ,从360软件管理器上装

  9. spring boot The request was rejected because the URL was not normalized

    升级spring boot 1.5.10.RELEASE 版本后,突然发现之前能Nginx代理能请求的地址抛如下异常: org.springframework.security.web.firewal ...

  10. H3C 建立邻接关系