第十四届蓝桥杯三月真题刷题训练——第 14 天
发布日期:2024-04-20 22:46:53
浏览次数:1
分类:技术文章
本文共 2330 字,大约阅读时间需要 7 分钟。
目录
等差数列
原题链接
问题描述
解题思路
对输入的数据进行排序,找出最小的差值即为这个数列的公差。(注意公差可能为 0)
等差数列求项数(末项 - 首项) / 公差 + 1
参考代码
import java.util.*;import java.math.*;import java.io.*;public class Main { static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static int N = (int)1e5 + 10, n; static int[] a = new int[N]; public static void main(String[] args) throws Exception { n = Integer.parseInt(in.readLine()); String[] s = in.readLine().split(" "); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(s[i]); } Arrays.sort(a, 0, n); long mn = a[0], mx = a[n - 1], d = mx - mn; if (d == 0) { out.println(n); } else { for (int i = 1; i < n; i++) { d = Math.min(d, a[i] - a[i - 1]); } out.println((mx - mn) / d + 1); } out.flush(); in.close(); } }
波动数列
原题链接
问题描述
解题思路
- 状态定义:定义
f[i][j]
为前 i 次操作后数列和为 j 的方案数 - 初始化:
f[0][0] = 1
- 状态转移:设
f[i][j]
为当前状态,那么上一个状态可以是当前状态-a
或者+b
得到,具体-a/+b
的项数和当前的 i 有关。f[i][j] = f[i - 1][j - i * a] + f[i - 1][j + i * b]
取模后得到f[i][j] = (f[i - 1][(j - i * a) % n] + f[i - 1][(j + i * b) % n]) % MOD
- 得到最终的答案需要
n - 1
次操作
由于 Python 对正负数取模都是正数,Java 则不是,为了防止下标为负数,需要进行双重取模:(x % n + n) % n
参考代码
import java.util.*;import java.math.*;import java.io.*;public class Main { static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static int MOD = (int)1e8 + 7; static int N = 1010, n, s, a, b; static int[][] f = new int[N][N]; public static int getMOD(int x) { return (x % n + n) % n; } public static void main(String[] args) throws Exception { String[] ss = in.readLine().split(" "); n = Integer.parseInt(ss[0]); s = Integer.parseInt(ss[1]); a = Integer.parseInt(ss[2]); b = Integer.parseInt(ss[3]); f[0][0] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < n; j++) { f[i][j] = (f[i - 1][getMOD(j + i * b)] + f[i - 1][getMOD(j - i * a)]) % MOD; } } out.println(f[n - 1][getMOD(s)]); out.flush(); in.close(); } }
转载地址:https://blog.csdn.net/qq_53655497/article/details/129613822 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月16日 08时37分17秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
用MATLAB实现m序列的生成(MATLAB 2021a适用)
2019-04-30
MATLAB函数备忘(定期更新)
2019-04-30
13行MATLAB代码实现网络爬虫 爬取NASA画廊星图
2019-04-30
MATLAB指定路径保存图片方法
2019-04-30
Python一键获取微信推送封面图
2019-04-30
油猴脚本:微信推送浏览功能拓展
2019-04-30
JavaScript DOM对象操作详解
2019-04-30
JavaScript 表单操作与MD5加密
2019-04-30
CppWeekly 06 structured binding
2019-04-30
CppWeekly 08 constexpr
2019-04-30
使gazebo_ros能够找到其他package的资源文件
2019-04-30
右键打开 visual studio developer command prompt
2019-04-30
利用AirSim在Unreal Engine上获取全景图像
2019-04-30
神奇的c++等号重载
2019-04-30
利用uWSGI和Nginx部署Django
2019-04-30
Linux下修改^M换行符
2019-04-30
笔记-有关于Vim
2019-04-30
vnc, vncserver, ssh的locale问题
2019-04-30