当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
VS2017编译Tars测试工程
MySQL 8.0.29 set and modify the default password
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
reindex win10
2022/07/30 学习笔记 (day20) 面试题积累
网安学习-内网渗透3
When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'
IDEA 连接 数据库
随机推荐
d违反常了吗
正则表达式语法及使用
Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
第十九周进度(了解物联网基础知识)
cnpm安装步骤
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
连号区间数
The most powerful and most commonly used SQL statements in history
ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
Gxlcms audio novel system/novel listening system source code
mysql remove duplicate data
vulnhub靶机AI-Web-1.0渗透笔记
使用LVS和Keepalived搭建高可用负载均衡服务器集群
Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
IDEA 连接 数据库
Abstract classes and interfaces (study notes)
科技的成就(三十一)
反转链表-就地逆置法
关于XML的学习(一)