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\) ...
随机推荐
- 多个commit合并为一个
在进行多个commit合并成一个博客编写的过程中,你可以使用以下代码示例作为参考: # 合并多个commit git rebase -i HEAD~N # N代表需要合并的commit数目,例如合并最 ...
- Django自身提供测试类、工具-调研
Django自身提供测试类.工具 django.test.Client 他的作用是模拟客户端.提供一系列的方法,例如get.post.delete.login等其中login是用django自身的验证 ...
- Pycharm里Python运行窗口显示乱码���的解决方法
当你的Python程序运行后,会在运行窗口中显示乱码 ��� 等字样,如下 原因是 Pycharm中默认设置只显示UTF-8编码的格式,需要修改支持显示中文支持. 解决方法: 菜单中选择 File S ...
- Hugging News #0710: 体验 MusicGen、Diffusers 库发布一周年、我们的内容政策更新
每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...
- 零基础如何自学C#?
前言 本文来源于知乎的一个提问,提问的是一个大一软件工程专业的学生,他想要自学C#但是不知道该怎么去学,这让他感到很迷茫,希望有人能给他一些建议和提供一些学习方向. 个人建议 确认目标:自学C#首先你 ...
- Cookie相关基础
1) 创建Cookie对象, public Cookie(java.lang.String name, java.lang.String value) 参数1:表示cookie名称 参数2:表示coo ...
- 在HTML中引入React和JSX
前言 Vue 可以非常方便地与 Pure HTML 结合,代替 jQuery 的功能,有一次遇到类似的场景时,我就想 React 能不能也以这种方式接入 HTML 网页,从而提高开发效率. 结果当然是 ...
- 2023年icpc大学生程序设计竞赛-nhr
icpc的省赛是在洛阳举办,第一次出省,还是两天,第一次离开郑州去别的城市比赛,心情更多的是激动,非常感谢老师给了这次机会,第一天20号,打完热身赛之后回寝室,和队友一起看了一下去年省赛的题,感觉还是 ...
- 语音合成技术汇总1:Glow-TTS:通过单调对齐实现文本到语音的生成流
今天开始开一期语音合成经典论文的翻译 Glow-TTS:通过单调对齐实现文本到语音的生成流 摘要: 最近,文本到语音(Text-to-Speech,TTS)模型,如FastSpeech和ParaNet ...
- <学习笔记> 关于错排列
1 是做排列计数的时候了解到这个东西: 一开始想的是用容斥原理,先加上全排列,再减去不满足的,再加上重复的,再减去不满足的...... 后来发现还涉及到杨辉三角,麻烦死了,时空复杂度也过不去,然后就知 ...