当前位置:网站首页>2021GDCPC广东省大学生程序设计竞赛 B.Byfibonacci
2021GDCPC广东省大学生程序设计竞赛 B.Byfibonacci
2022-07-30 22:48:00 【_Kiwi_Berry_】
题意:
给一个数x,问有多少个 Fibonacci 子集其和为x
思路:
如果 n 的范围较小,那就是01背包,但 n<=1e7 就不能以值域作为状态,但我们可以发现看题解 :设 v1 , v2 为小于 x 的 Fibonacci 最大值和次大值,那么符合条件的子集有且仅有他们中的一个。
找到这个规律后就可以DP了,我们令
- dp[0][i] 表示 i 选取最大的那个斐波那契数的乘积和
- dp[1][i] 表示 i 选取次大的那个斐波那契数的乘积和
namo 转移方程则为
- dp[0][i]=(dp[0][i-v1]+dp[1][i-v1])*v1
- dp[1][i]=(dp[0][i-v2]+dp[1][i-v2])*v2; i-v2<v2
- dp[1][i]=(dp[1][i-v2])*v2; i-v2>=v2
Code:
#include <bits/stdc++.h>
// #define debug freopen("_in.txt", "r", stdin);
#define debug freopen("_in.txt", "r", stdin), freopen("_out.txt", "w", stdout);
typedef long long ll;
typedef unsigned long long ull;
typedef struct Node *bintree;
using namespace std;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e7 + 10;
const ll maxm = 2e6 + 10;
const ll mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
ll T, n, m, q, d, kase = 0;
priority_queue<pair<ll, ll>> que;
ll arr[100];
ll dp[3][maxn];
void solve()
{
scanf("%lld", &n);
printf("%lld\n",(dp[0][n]+dp[1][n])%mod);
}
signed main()
{
// debug;
scanf("%lld", &T);
// T = 1;
arr[1] = arr[2] = 1;
for (ll i = 2; i <= 50; i++)
{
arr[i] = arr[i - 1] + arr[i - 2];
}
dp[0][0] = 1;
dp[0][1] = 1;
dp[1][1] = 1;
ll now = 0;
for (ll i = 2; i <= 1e7; i++)
{
if (i >= arr[now])
{
while (i >= arr[now])
{
now++;
}
now--;
}
ll v1=arr[now],v2=arr[now-1];
dp[0][i]=(dp[0][i-v1]+dp[1][i-v1])*v1;
dp[0][i]%=mod;
if(i-v2<v2)
{
dp[1][i]=(dp[0][i-v2]+dp[1][i-v2])*v2;
}
else
{
dp[1][i]=(dp[1][i-v2])*v2;
}
dp[1][i]%=mod;
}
while (T--)
{
solve();
}
}
边栏推荐
- MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
- The problem of sticky packets in tcp protocol transmission
- 二进制序列
- #yyds干货盘点# 面试必刷TOP101:判断链表中是否有环
- VS2017编译Tars测试工程
- go版本升级
- Gxlcms audio novel system/novel listening system source code
- PS基础学习(一)
- 打动中产精英群体,全新红旗H5用产品力跑赢需求
- 【Untitled】
猜你喜欢

MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)

Navicat connection MySQL error: 1045 - Access denied for user 'root'@'localhost' (using password YES)

Apache Doris系列之:安装与部署详细步骤

#yyds干货盘点# 面试必刷TOP101:判断链表中是否有环

【科研】文献下载神器方式汇总

【MySQL】Mysql事务以及权限管理

Alibaba Cloud video on demand + project combat

TCP 连接 三次握手 四次挥手

MySQL连接时出现2003错误

EasyExcel comprehensive course combat
随机推荐
matlab标量场作图
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
设备树的引入与体验
Navicat connection MySQL error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
d违反常了吗
Jetson AGX Orin 平台关于c240000 I2C总线和GMSL ses地址冲突问题
Go1.18升级功能 - 泛型 从零开始Go语言
MySQL 8.0.29 set and modify the default password
PyTorch模型导出到ONNX文件示例(LeNet-5)
MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
proxy反向代理
2022.7.28
【科研】文献下载神器方式汇总
A detailed explanation: SRv6 Policy model, calculation and drainage
The Road to Ad Monetization for Uni-app Mini Program Apps: Rewarded Video Ads
详解操作符
mysql获取近7天,7周,7月,7年日期,根据当前时间获取近7天,7周,7月,7年日期
[MySQL] Related operations on databases and tables in MySQL
Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)