【题解】HDU Homework(倍增)
【题解】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(倍增)的更多相关文章
- [题解]hdu 1009 FatMouse' Trade(贪心基础题)
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- HDU 2586 倍增法求lca
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 6107--Typesetting(倍增)
题目链接 Problem Description Yellowstar is writing an article that contains N words and 1 picture, and t ...
- 题解 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 ...
- 题解 HDU 5279 YJC plays Minecraft
题目传送门 题目大意 给出\(n\)以及\(a_{1,2,...,n}\),表示有\(n\)个完全图,第\(i\)个完全图大小为\(a_i\),这些完全图之间第\(i\)个完全图的点\(a_i\)与\ ...
- 题解-bzoj4320 Homework
Problem bzoj4320 Solution 前置技能:分块+线段树+卡常+一点小小的数学知识 考试时A的 这种题无论怎么处理总有瓶颈,套路分块,设\(k\)以下的插入时直接暴力预处理,查询时直 ...
- 题解——HDU 4734 F(x) (数位DP)
这道题还是关于数位DP的板子题 数位DP有一个显著的特征,就是求的东西大概率与输入关系不大,理论上一般都是数的构成规律 然后这题就是算一个\( F(A) \)的公式值,然后求\( \left [ 0 ...
- 题解——HDU 2089 不要62(数位DP)
最近在学数位DP 应该是入门题吧 设\( dp[i][0/1] \)表示到第\( i \)位时,前一位是否是6的满足条件的数的个数 然后就是套路 注意\( limit \)的限制条件以及转移时候信息的 ...
- 题解——HDU 1848 Fibonacci again and again
一道组合游戏的题目 SG函数的板子题 预处理出SG函数的值然后回答询问即可 代码 #include <cstdio> #include <algorithm> #include ...
随机推荐
- 走近科学,探究阿里闲鱼团队通过数据提升Flutter体验的真相
背景 闲鱼客户端的flutter页面已经服务上亿级用户,这个时候Flutter页面的用户体验尤其重要,完善Flutter性能稳定性监控体系,可以及早发现线上性能问题,也可以作为用户体验提升的衡量标准. ...
- Centos6.9部署ORTS5.0.22
1.安装数据库 为了使用默认InnoDB引擎,Centos6.9上默认yum安装mysql5.1.73版本的,orts在初始化数据库时要求log大小要大于250M以上,因此干净安装Centos后,先安 ...
- git 本地仓库操作
一.git对象模型和存储 二.常用命令 1)git checkout branch 切换分支 假设现在有两个分支,master和dev分支 i dev分支上没有readme.txt 在master分支 ...
- PageHelper实现分页查询
PageHelper是基于拦截器实现的myBatis分页插件 PageHelper的Github主页 : https://github.com/pagehelper/Mybatis-PageHelpe ...
- .net core 控制台下使用HttpClientFactory封装
HttpClientFactory封装,如有错误请指出,谢谢! using System; using System.Collections.Generic; using System.Net.Htt ...
- HDU1686 Oulipo 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给你一个子串t和一个母串s,求s中有多少个子串t. 题目分析:KMP模板题. cal_ ...
- Spring Boot 集成日志logback + 控制台打印SQL
一: 控制台打印SQL application.properties中添加如下即可在控制台打印sql logging.level.com.fx.fxxt.mapper=debug 二:日志 因为Spr ...
- Python--day66--Django模板语言关于静态文件路径的灵活写法
静态文件路径的灵活写法: 正规的讲解: 静态文件相关 {% static %} {% load static %} <img src="{% static "images/h ...
- RBF神经网络通用函数 newrb, newrbe
RBF神经网络通用函数 newrb, newrbe 1.newrb 其中P为输入向量,T为输出向量,GOAL为均方误差的目标,SPREED为径向基的扩展速度.返回值是一个构建好的网络,用newrb ...
- 2018-8-10-如何入门-C++-AMP-教程
title author date CreateTime categories 如何入门 C++ AMP 教程 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...