Solution -「洛谷 P4451」「国家集训队」整数的 lqp 拆分
Description
Link.
求
\]
Solution
这是一篇不用 \(\mathbf{OGF}\) 的题解。
设 \(f_{i}\) 为 \(i\) 的 \(\operatorname{lqp}\) 拆分值。
然后有显然的过不了递推式:
1,n=0 \\
\displaystyle
\sum_{i=0}^{n-1}F_{n-i}\times f_{i},n\neq0
\end{cases}
\]
然后传统艺能错位相减操作一下:
f_{n}&=\sum_{i=0}^{n-1}F_{n-i}\times f_{i} \\
f_{n-1}&=\sum_{i=0}^{n-2}F_{n-i-1}\times f_{i} \\
f_{n-2}&=\sum_{i=0}^{n-3}F_{n-i-2}\times f_{i}
\end{aligned}
\Longrightarrow
\begin{aligned}
f_{n}-f_{n-1}-f_{n-2}&=\sum_{i=0}^{n-1}F_{n-i}\times f_{i}-\sum_{i=0}^{n-2}F_{n-i-1}\times f_{i}-\sum_{i=0}^{n-3}F_{n-i-2}\times f_{i} \\
f_{n}-f_{n-1}-f_{n-2}&=(F_{2}-F_{1})\times f_{n-2}+F_{1}\times f_{n-1}
\end{aligned}
\\
\downarrow \\
f_{n}=2f_{n-1}+f_{n-2}
\]
递推公式有了,然后矩阵快速幂:
f_{n} \\
f_{n-1}
\end{bmatrix}
=\begin{bmatrix}
2f_{n-1}+f_{n-2} \\
f_{n-1}
\end{bmatrix}
=\begin{bmatrix}
f_{n-1} \\
f_{n-2}
\end{bmatrix}
\times\begin{bmatrix}
2 & 1 \\
1 & 0
\end{bmatrix}
\]
这样就可以做了(吗?):
(code?)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#define mod ( 1000000007 )
using namespace std;
typedef long long LL;
template<typename _T, typename _P>
_T qkpow( _T bas, _T one, _P fur ){
_T res = one;
while( fur != 0 ){
if( fur % 2 == ( _P )1 ) res = bas * res;
bas = bas * bas;
fur /= 2;
}
return res;
}
template<typename _T>
_T add( _T x, _T y ){ if( y >= mod ) y %= mod; x += y; if( x >= mod ) x -= mod; return x; }
struct bigInt : vector<int>{
bigInt &check( ){
while( ! empty( ) && ! back( ) ) pop_back( );
if( empty( ) ) return *this;
for( unsigned i = 1; i < size( ); ++ i ){ ( *this )[i] += ( *this )[i - 1] / 10; ( *this )[i - 1] %= 10; }
while( back( ) >= 10 ){ push_back( back( ) / 10 ); ( *this )[size( ) - 2] %= 10; }
return *this;
}
bigInt( int tpN = 0 ){ push_back( tpN ); check( ); }
};
istream &operator >> ( istream &is, bigInt &tpN ){
string s;
is >> s; tpN.clear( );
for( int i = s.size( ) - 1; i >= 0; --i ) tpN.push_back( s[i] - '0' );
return is;
}
ostream &operator << ( ostream &os, const bigInt &tpN ){
if( tpN.empty( ) ) os << 0;
for( int i = tpN.size( ) - 1; i >= 0; --i ) os << tpN[i];
return os;
}
bool operator != ( const bigInt &one, const bigInt &another ){
if( one.size( ) != another.size( ) ) return 1;
for( int i = one.size( ) - 1; i >= 0; --i ){
if( one[i] != another[i] ) return 1;
}
return 0;
}
bool operator == ( const bigInt &one, const bigInt &another ){
return ! ( one != another );
}
bool operator < ( const bigInt &one, const bigInt &another ){
if( one.size( ) != another.size( ) ) return one.size( ) < another.size( );
for( int i = one.size( ) - 1; i >= 0; --i ){
if( one[i] != another[i] ) return one[i] < another[i];
}
return 0;
}
bool operator > ( const bigInt &one, const bigInt &another ){ return another < one; }
bool operator <= ( const bigInt &one, const bigInt &another ){ return ! (one > another ); }
bool operator >= ( const bigInt &one, const bigInt &another ){ return ! (one < another ); }
bigInt &operator += ( bigInt &one, const bigInt &another ){
if( one.size( ) < another.size( ) ) one.resize(another.size( ) );
for( unsigned i = 0; i != another.size( ); ++ i ) one[i] += another[i];
return one.check( );
}
bigInt operator + ( bigInt one, const bigInt &another ){ return one += another; }
bigInt &operator -= ( bigInt &one, bigInt another ){
if( one < another ) swap( one, another );
for( unsigned i = 0; i != another.size( ); one[i] -= another[i], ++ i ){
if( one[i] < another[i] ){
unsigned j = i + 1;
while( ! one[j] ) ++ j;
while( j > i ){ -- one[j]; one[--j] += 10; }
}
}
return one.check( );
}
bigInt operator - ( bigInt one, const bigInt &another ){ return one -= another; }
bigInt operator * ( const bigInt &one, const bigInt &another ){
bigInt tpN;
tpN.assign( one.size( ) + another.size( ) - 1, 0 );
for( unsigned i = 0; i != one.size( ); ++ i ){
for( unsigned j = 0; j != another.size( ); ++ j ) tpN[i + j] += one[i] * another[j];
}
return tpN.check( );
}
bigInt &operator *= ( bigInt &one, const bigInt &another ){ return one = one * another; }
bigInt divMod( bigInt &one, const bigInt &another ){
bigInt ans;
for( int t = one.size( ) - another.size( ); one >= another; -- t ){
bigInt tpS;
tpS.assign( t + 1, 0 );
tpS.back( ) = 1;
bigInt tpM = another * tpS;
while( one >= tpM ){ one -= tpM; ans += tpS; }
}
return ans;
}
bigInt operator / ( bigInt one, const bigInt &another ){ return divMod(one, another ); }
bigInt &operator /= ( bigInt &one, const bigInt &another ){ return one = one / another; }
bigInt &operator %= ( bigInt &one, const bigInt &another ){ divMod( one, another ); return one; }
bigInt operator % ( bigInt one, const bigInt &another ){ return one %= another; }
struct matrixS{
int mat[2][2];
matrixS( int x = 0 ){ memset( mat, x, sizeof( mat ) ); }
matrixS operator * ( const matrixS &another ) const{
matrixS res;
for( int i = 0; i < 2; ++ i ){
for( int j = 0; j < 2; ++ j ){
for( int k = 0; k < 2; ++ k ) res.mat[i][j] = add( ( LL )res.mat[i][j], ( LL )mat[i][k] * another.mat[k][j] );
}
}
return res;
}
} unit, erng;
bigInt N;
void progressBaseInformation( ){
int unitS[2][2] = { { 1, 0 }, { 0, 1 } };
memcpy( unit.mat, unitS, sizeof( unitS ) );
int erngS[2][2] = { { 2, 1 }, { 1, 0 } };
memcpy( erng.mat, erngS, sizeof( erngS ) );
}
signed main( ){
ios::sync_with_stdio( 0 ); cin.tie( 0 ); cout.tie( 0 );
progressBaseInformation( );
cin >> N; cout << qkpow( erng, unit, N ).mat[1][0] << '\n';
return 0;
}
不,凉心出题人友好地卡了高精的常数,于是你打开题解,发现 \(f_{n}=f_{n\bmod (10^{9}+6)}\),于是你又行了。
\(\mathcal{Code}\)
#include <cstdio>
#include <cstring>
#include <queue>
#define mod ( 1000000007 )
using namespace std;
typedef long long LL;
template<typename _T>
void read( _T &x ){
x = 0; char c = getchar( ); _T f = 1;
while( c < '0' || c > '9' ){ if( c == '-' ) f = -1; c = getchar( ); }
while( c >= '0' && c <= '9' ){ x = ( ( x << 3 ) + ( x << 1 ) + ( c & 15 ) ) % ( mod - 1 ); c = getchar( ); }
x *= f;
}
template<typename _T>
void write( _T x ){
if( x < 0 ){ putchar( '-' ); x = -x; }
if( x > 9 ) write( x / 10 );
putchar( x % 10 + '0' );
}
template<typename _T, typename _P>
_T qkpow( _T bas, _T one, _P fur ){
_T res = one;
while( fur != 0 ){
if( fur % 2 == ( _P )1 ) res = bas * res;
bas = bas * bas;
fur /= 2;
}
return res;
}
template<typename _T>
_T add( _T x, _T y ){ if( y >= mod ) y %= mod; x += y; if( x >= mod ) x -= mod; return x; }
struct matrixS{
int mat[2][2];
matrixS( int x = 0 ){ memset( mat, x, sizeof( mat ) ); }
matrixS operator * ( const matrixS &another ) const{
matrixS res;
for( int i = 0; i < 2; ++ i ){
for( int j = 0; j < 2; ++ j ){
for( int k = 0; k < 2; ++ k ) res.mat[i][j] = add( ( LL )res.mat[i][j], ( LL )mat[i][k] * another.mat[k][j] );
}
}
return res;
}
} unit, erng;
LL N;
void progressBaseInformation( ){
int unitS[2][2] = { { 1, 0 }, { 0, 1 } };
memcpy( unit.mat, unitS, sizeof( unitS ) );
int erngS[2][2] = { { 2, 1 }, { 1, 0 } };
memcpy( erng.mat, erngS, sizeof( erngS ) );
}
signed main( ){
progressBaseInformation( );
read( N ); write( qkpow( erng, unit, N ).mat[1][0] ), putchar( '\n' );
return 0;
}
Solution -「洛谷 P4451」「国家集训队」整数的 lqp 拆分的更多相关文章
- 【洛谷】1852:[国家集训队]跳跳棋【LCA】【倍增?】
P1852 [国家集训队]跳跳棋 题目背景 原<奇怪的字符串>请前往 P2543 题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个 ...
- 【洛谷】1494:[国家集训队]小Z的袜子【莫队】
P1494 [国家集训队]小Z的袜子 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… ...
- Solution -「国家集训队」「洛谷 P4451」整数的 lqp 拆分
\(\mathcal{Description}\) Link. 求 \[\sum_{m>0\\a_{1..m}>0\\a_1+\cdots+a_m=n}\prod_{i=1}^mf ...
- 洛谷P1903 数颜色 [国家集训队] 莫队
正解:带修莫队 解题报告: 可以理解为引入时间参数,然后就是有了仨参数,关于这个修改同样的是,如果时间是相同的,不用搞,如果时间不相同做一下时光倒流/时光推移就成嘛 但是肯定既然这样的话,按照原来的s ...
- 题解 洛谷P1903/BZOJ2120【[国家集训队]数颜色 / 维护队列】
对于不会树套树.主席树的本蒟蒻,还是老老实实的用莫队做吧.... 其实这题跟普通莫队差不了多远,无非就是有了一个时间,当我们按正常流程排完序后,按照基本的莫队来,做莫队时每次循环对于这一次操作,我们在 ...
- 题解 洛谷P1501/BZOJ2631【[国家集训队]Tree II】
Link-Cut-Tree 的懒标记下传正确食用方法. 我们来逐步分析每一个操作. 1:+ u v c:将u到v的路径上的点的权值都加上自然数c; 解决方法: 很显然,我们可以 split(u,v) ...
- 洛谷 P1505 BZOJ 2157 [国家集训队]旅游
bzoj题面 Time limit 10000 ms Memory limit 265216 kB OS Linux 吐槽 又浪费一个下午--区间乘-1之后,最大值和最小值更新有坑.新的最大值是原来最 ...
- 「区间DP」「洛谷P1043」数字游戏
「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...
- 「 洛谷 」P2768 珍珠项链
珍珠项链 题目限制 内存限制:125.00MB 时间限制:1.00s 标准输入输出 题目知识点 动态规划 \(dp\) 矩阵 矩阵乘法 矩阵加速 矩阵快速幂 题目来源 「 洛谷 」P2768 珍珠项链 ...
- 「 洛谷 」P4539 [SCOI2006]zh_tree
小兔的话 推荐 小兔的CSDN [SCOI2006]zh_tree 题目限制 内存限制:250.00MB 时间限制:1.00s 标准输入输出 题目知识点 思维 动态规划 \(dp\) 区间\(dp\) ...
随机推荐
- Request类源码分析、序列化组件介绍、序列化类的基本使用、常用字段类和参数、反序列化之校验、反序列化之保存、APIVIew+序列化类+Response写的五个接口代码、序列化高级用法之source、序列化高级用法之定制字段的两种方式、多表关联反序列化保存、反序列化字段校验其他、ModelSerializer使用
目录 一.Request类源码分析 二.序列化组件介绍 三.序列化类的基本使用 查询所有和查询单条 四.常用字段类和参数(了解) 常用字段类 字段参数(校验数据来用的) 五.反序列化之校验 六.反序列 ...
- C#使用HtmlAgilityPack解析Html 爬取图片和视频
HtmlAgilityPack简介 HtmlAgilityPack是.net下的一个HTML解析类库.支持用XPath来解析HTML. 问题来了,有人就会问为什么要使用能XPath呢? 小编答:因为对 ...
- ImageIO的应用
ImageIO的应用 一.关于IO流 在讲imageio之前,我们先来复习一下IO流的使用. 这里我建立一个Java类,用来实现读取文档中的内容,并且能够识别换行,话不多说,上代码: package ...
- C# 图片转PDF,PDF增加水印文字
好久没写博客了,今天给大家分享一个图片转PDF的相关操作,也算是一次总结吧. 首先需要准备动态库itextsharp.dll,这个dll去网上下载,都可以下载到,C#对PDF的操作都是基于这个类库来实 ...
- 4. Mybatis的增删改查(CRUD)
1.新增 <!--int insertUser();--> <insert id="insertUser"> insert into t_user va ...
- PRF评价
PRF评价指标: 精确率P:预测结果正类数量占全部结果的比率: P= $\frac{TP}{TP+FP}$ TP:预测为真且实际为真,FP为预测真实际为假. 召回率R:在所有正类样本中,能回想到的比例 ...
- Blazor前后端框架Known-V1.2.4
V1.2.4 Known是基于C#和Blazor开发的前后端分离快速开发框架,开箱即用,跨平台,一处代码,多处运行. Gitee: https://gitee.com/known/Known Gith ...
- 解决连接MySQL,报错10061,系统错误5
mysql登录不上去,报错10061,百度后得,mysql服务未启动.. 方法一.选择dos窗口命令行打开mysql 输入代码 net start mysql 报错,如图所示.系统错误 5 解决办法: ...
- Java扩展Nginx之五:五大handler(系列最核心)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<Java扩展Nginx> ...
- 【转载】Linux虚拟化KVM-Qemu分析(八)之virtio初探
原文信息 作者:LoyenWang 出处:https://www.cnblogs.com/LoyenWang/ 公众号:LoyenWang 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者 ...