P1939,但是绿题橙解
那么,可以降橙了
本文要介绍的并不是常规解法(而是分段打表),常规解法请参考其他题解
分段打表介绍
分段打表是一种空间和代码长度更优秀的打表。
:::info[适用条件]{open}
- 答案可以进行递推,而且递推变量较少。\ 最经典的例子就是形如
的形式。
当然,如本题一样的,不需要很多变量的递推式(包括本题、斐波那契数列、以及递推项数少的递推式)也是可以的。
- 答案可以在合理的复杂度内计算完成。
注意这里的合理是指本机可以在可接受(如
- 值域不能过大。
| 比如这道题的 |
|---|
具体实现方法
先说
这种形式的话,假设可以在
我们每
比如你的
然后,类似分块的,你就可以将查询的巨大数字的前面很大一段查表,然后剩下来的一小段(小于
对于本题的递推,你可以类似的存下来当前状态,然后就只需要递推很小的一部分。
复杂度:
本机时间复杂度
评测机时间复杂度
代码空间复杂度(字面意思,就是代码有多长)
本题解法
我们发现有
本题代码
注意代码中将数列略微向前移了几项。
打表部分(本机运行 11 秒)
#include<bits/stdc++.h>
using namespace std;
//#define int long long
vector<int> A,B,C;
signed main()
{
freopen("dabiao.txt","w",stdout);
ios::sync_with_stdio(false);cin.tie(0);
constexpr int N=2000000000;
constexpr int mod=1000000007;
constexpr int M=1200000;
int a=1,b=1,c=1,cnt=0;
for(;cnt<=N;cnt++)
{
if(cnt%M==0)
A.push_back(a),B.push_back(b),C.push_back(c);
int d=(a+c)%mod;
a=b,b=c,c=d;
}
cout<<"int A[]={";
for(auto x:A)cout<<x<<',';
cout<<"};\n";
cout<<"int B[]={";
for(auto x:B)cout<<x<<',';
cout<<"};\n";
cout<<"int C[]={";
for(auto x:C)cout<<x<<',';
cout<<"};\n";
return 0;
}
提交部分(48KB )
#include<bits/stdc++.h>
using namespace std;
//#define int long long
//A,B,C 数组从上一个程序的输出那里拿
signed main()
{
constexpr int mod=1000000007;
constexpr int M=1200000;
ios::sync_with_stdio(false);cin.tie(0);
int t;cin>>t;
while(t--){
int n;cin>>n;
n-=3;
if(n<0)
{
cout<<"1\n";
continue;
}
int a=A[n/M],b=B[n/M],c=C[n/M];
n%=M;
while(n--)
{
int d=(a+c)%mod;
a=b,b=c,c=d;
}
cout<<c<<'\n';
}
return 0;
}